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

Support codegen from query string #470

Merged
merged 2 commits into from
Mar 18, 2024
Merged

Conversation

dphm
Copy link
Contributor

@dphm dphm commented Jan 4, 2024

Context

Our crate generates the GraphQL query that uses the macro to generate Rust types. We want to avoid writing the generated query to a temporary file.

Proposal

  • Refactor graphql_client_codegen and expose a function to generate module token stream from a query string (instead of a query path)
  • Maintain backwards-compatibility with the existing generate_module_token_stream

@tomhoule
Copy link
Member

tomhoule commented Jan 5, 2024

I don't have context on the change but that made me think, now that #[graphql(query = include_str!("./my_query.graphql")] is possible (pseudo-code, and I haven't checked, but pretty sure it works now), couldn't we make this the one and only way to inject the query? It would be a breaking change, of course, but we can have a deprecation cycle.

@dphm
Copy link
Contributor Author

dphm commented Jan 5, 2024

@tomhoule Oh! I didn't know that existed. I agree that would be a better way to include the query from a file. Do you know if that would still work with all the file path logic (CARGO_MANIFEST_DIR)?

Seems like I could still make the changes to accept a query string instead of the file, right?

@dphm
Copy link
Contributor Author

dphm commented Jan 5, 2024

For some early 👀 and any thoughts! @surma @nickwesselman @andrewhassan @adampetro

@dphm dphm changed the title WIP Support query_string macro attribute [WIP] Support query string macro attribute Jan 5, 2024
graphql_query_derive/src/lib.rs Outdated Show resolved Hide resolved
@tomhoule
Copy link
Member

tomhoule commented Jan 6, 2024

Do you know if that would still work with all the file path logic (CARGO_MANIFEST_DIR)?

The logic is different: the paths in the proc macro are relative to CARGO_MANIFEST_DIR, but include_str!() takes a path relative to the file it is located in.

To address the questions in the PR description

Should backwards-compatibility be maintained, or is a breaking change acceptable?

It should be backwards compatible at first, unless we decide to make the new attribute argument the only way, but progressively, deprecating the old argument.

Should the schema use the same mechanism? Is the size concerning?

The requirement is new to me, so I don't know. I haven''t looked too deep into your use case, but since this is done at compile time, maybe depending on the graphql_client_codegen crate directly would be easier, since it wouldn't involve the derive macro at all, and you can pass the string directly in there, so you probably wouldn't need to make any change to graphql_cilent.

Copy link
Collaborator

@surma surma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff 🎉 Left some comments! Hope that’s helpful.

Would be great to also see a test for query_string.

graphql_client_codegen/src/lib.rs Outdated Show resolved Hide resolved
graphql_client_codegen/src/lib.rs Outdated Show resolved Hide resolved
graphql_client_codegen/src/lib.rs Outdated Show resolved Hide resolved
graphql_client_codegen/src/lib.rs Outdated Show resolved Hide resolved
graphql_client_codegen/src/lib.rs Outdated Show resolved Hide resolved
graphql_query_derive/src/lib.rs Outdated Show resolved Hide resolved
graphql_query_derive/src/lib.rs Outdated Show resolved Hide resolved
@dphm dphm force-pushed the query-string branch 5 times, most recently from 673017a to 0e04dca Compare January 8, 2024 20:49
@dphm
Copy link
Contributor Author

dphm commented Jan 8, 2024

Ah, I removed the changes to graphql_query_derive so I'll move the test out of graphql_client to graphql_client_codegen

@dphm
Copy link
Contributor Author

dphm commented Jan 8, 2024

depending on the graphql_client_codegen crate directly

Is this something we want to do?

@tomhoule
Copy link
Member

tomhoule commented Jan 9, 2024

Is this something we want to do?

I can't speak for shopify, but I think that if I had more time to work on this crate, I'd like it to become more modular. The core value proposition is the codegen, and that's just a fn(graphql_query: &str, graphql_schema: &str, config: Config) -> String function — we shouldn't force people to use a derive macro to take advantage of it. If it's hard to depend on graphql-client-codegen directly, we could make it easier.

@dphm dphm changed the title [WIP] Support query string macro attribute Support codegen from query string Jan 9, 2024
@dphm
Copy link
Contributor Author

dphm commented Jan 9, 2024

//! Crate for internal use by other graphql-client crates, for code generation.
//!
//! It is not meant to be used directly by users of the library.

Sounds like we should align on a public interface for using the codegen crate directly, and then I can update this comment 😆

@dphm
Copy link
Contributor Author

dphm commented Jan 9, 2024

🤔 We are using the derive though, and it would be nice to have the query string literal work. I could make that change in a separate PR.

Oh, I guess we would replace the entire quoted derive with the call to generate module tokens from graphql_client_codegen.

@dphm dphm marked this pull request as ready for review January 9, 2024 21:06
@dphm dphm requested a review from surma January 9, 2024 21:06
@tomhoule
Copy link
Member

Oh, I guess we would replace the entire quoted derive with the call to generate module tokens from graphql_client_codegen.

Yes, that's what I had in mind.

@andrewhassan
Copy link

Hey @tomhoule @surma! 👋 Would it be possible to get your 👀 on this PR again? Thank you!! 🙏

Add test for query string attribute

Handle non-utf8 paths and files without extensions

Co-authored-by: Surma <[email protected]>

Update crate doc comment

Use options: GraphQLClientCodegenOptions
Copy link
Collaborator

@surma surma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@surma surma merged commit ef53897 into graphql-rust:main Mar 18, 2024
9 checks passed
@dphm dphm deleted the query-string branch March 18, 2024 14:49
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.

4 participants