Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a pretty debug implementation to overcome escaping of strings #519

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

edmondop
Copy link

@edmondop edmondop commented Feb 5, 2025

As described in #518 , parsing a string and include it in code generation generates code like so:

            pub mod repository_with_pull_requests {
                #![allow(dead_code)]
                use std::result::Result;
                pub const OPERATION_NAME: &str = "RepositoryWithPullRequests";
                pub const QUERY: &str = "query RepositoryWithPullRequests(\n  $repositoryName: String!\n  $repositoryOwner: String!\n  $count: Int!\n  $pullRequestStates: [PullRequestState!]!\n) {\n  repository(name: $repositoryName, owner: $repositoryOwner) {\n    owner {\n      __typename\n      login\n    }\n    name\n    defaultBranchRef {\n      name\n    }\n    pullRequests(first: $count, states: $pullRequestStates) {\n      pageInfo {\n        hasNextPage\n        endCursor\n      }\n\n      totalCount\n      nodes {\n        author {\n          __typename\n          login\n        }\n        id\n        url\n        title\n        state\n        createdAt\n        updatedAt\n        closedAt\n        ## uh-oh not very safe but it's ok\n        labels(first: 100) {\n          edges {\n            node {\n              id\n              name\n            }\n          }\n        }\n      }\n    }\n  }\n}\n";
                const __QUERY_WORKAROUND: &str = "query RepositoryWithPullRequests(\n  $repositoryName: String!\n  $repositoryOwner: String!\n  $count: Int!\n  $pullRequestStates: [PullRequestState!]!\n) {\n  repository(name: $repositoryName, owner: $repositoryOwner) {\n    owner {\n      __typename\n      login\n    }\n    name\n    defaultBranchRef {\n      name\n    }\n    pullRequests(first: $count, states: $pullRequestStates) {\n      pageInfo {\n        hasNextPage\n        endCursor\n      }\n\n      totalCount\n      nodes {\n        author {\n          __typename\n          login\n        }\n        id\n        url\n        title\n        state\n        createdAt\n        updatedAt\n        closedAt\n        ## uh-oh not very safe but it\'s ok\n        labels(first: 100) {\n          edges {\n            node {\n              id\n              name\n            }\n          }\n        }\n      }\n    }\n  }\n}\n";

instead of a multi-line string. A solution would be to reduce reusability in macros:

pub fn generate_module_token_stream(
    query_path: std::path::PathBuf,
    schema_path: &std::path::Path,
    options: GraphQLClientCodegenOptions,
) -> Result<TokenStream, BoxError> {
    let query = get_set_query_from_file(query_path.as_path());
    let schema = get_set_schema_from_file(schema_path);

    generate_module_token_stream_inner(&query, &schema, options)
}

/// Generates Rust code given a query string, a path to a schema file, and options.
pub fn generate_module_token_stream_from_string(
    query_string: &str,
    schema_path: &std::path::Path,
    options: GraphQLClientCodegenOptions,
) -> Result<TokenStream, BoxError> {
    let query = (query_string.to_string(), query_document(query_string)?);
    let schema = get_set_schema_from_file(schema_path);

    generate_module_token_stream_inner(&query, &schema, options)
}

so that we propagate the path of the file and we use include_str! macro in code-generation if we have something from file, which would require us to modify

/// This struct contains the parameters necessary to generate code for a given operation.
pub(crate) struct GeneratedModule<'a> {
    pub operation: &'a str,
    pub query_string: &'a str,
    pub resolved_query: &'a crate::query::Query,
    pub schema: &'a crate::schema::Schema,
    pub options: &'a crate::GraphQLClientCodegenOptions,
}

to support a query that's either a String or a PathBuf and then at macro time doing either an include or a simple quoting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant