graphql-rust / graphql-client

Typed, correct GraphQL requests and responses in Rust
Apache License 2.0
1.12k stars 152 forks source link

Support codegen from query string #470

Closed dphm closed 3 months ago

dphm commented 6 months ago

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

tomhoule commented 6 months ago

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 commented 6 months ago

@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 commented 6 months ago

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

tomhoule commented 6 months ago

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.

dphm commented 5 months ago

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

dphm commented 5 months ago

depending on the graphql_client_codegen crate directly

Is this something we want to do?

tomhoule commented 5 months ago

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 commented 5 months ago

https://github.com/graphql-rust/graphql-client/blob/a7283cb25fa332e4fb46f0247db1b173913d629c/graphql_client_codegen/src/lib.rs#L5-L7

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

dphm commented 5 months ago

~🤔 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.

tomhoule commented 5 months ago

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 commented 4 months ago

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