-
Notifications
You must be signed in to change notification settings - Fork 47
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
example: variable inlining #1007
Conversation
✅ Deploy Preview for cynic-querygen-web canceled.
|
Dang. You've put a lot of effort into this. Man, you don't even know how thankful I am. You even linked to the actual API reference. |
In this article they call it "Inline Argument". I guess it doesn't really matter. |
Yeah, I would say what they're doing in that article is putting arguments inline. But in cynic terms I think it's fair to call it variable inlining - since we're taking variables and inlining them into arguments. |
#### Why are we making this change? In #1000 & #1004 a user is wanting to build a query that doesn't make use of variables without hardcoding the values of arguments. This is to make use of [some shopify functionality][1] that requires you to submit a query without variables. My initial instinct was that this should be done externally to cynic, but I did plan on documenting how. #1007 was added to cater to this - it contained an example that used the `graphql-query` crate to post-process a cynic query and inline all of the variables into arguments in the query. However, when writing this I discovered it's not quite as smooth as I'd hoped: you can't just dump the variables into JSON and put those into the query because enums are serialised as strings in JSON but enum format in GraphQL. Being unable to use JSON means you're also unable to dynamically look up variables from their names - you have to hardcode every variable that you want to substitute. Which is pretty crap. #### What effects does this change have? This PR introduces a new `QueryVariableLiterals` trait & derive that aims to help with this: it exposes a single function `get` that can be passed the name of a variable and it will return the appropriate `InputLiteral` for that variable. This can be used to make a `graphql-query` based transform much nicer, but it should also make it easy enough to add support for variable inlining into cynic itself. [1]: https://shopify.dev/docs/api/admin-graphql/2024-07/mutations/bulkoperationrunquery
Writing out this example made me realise there were a few shortcomings with this approach (or more detail see the PR message of #1009). I've now fixed those shortcomings and added this as a built in feature in #1012. I want to keep this PR around as a record of this idea, so closing this in favour of #1013 which adds an example of the new approach. |
#### Why are we making this change? Now that a49710 is merged it's quite easy to extract an `InputLiteral` from a `QueryVariables` struct by the name of the variable. This makes it very easy to add variable inlining as a first class feature, rather than recommending post-processing as in #1007. #### What effects does this change have? Adds a new `build_with_variables_inlined` function to `OperationBuilder` that is available when `Variables` implements `QueryVariableLiterals`. This will return an `Operation` with no variables entry and all the variables added into the `query` string as arguments. I had wanted to do this as a setting inside the builder, but getting the types to work with that was tricky - so another build function seemed the best option.
TODO:
to_input_literal
to make it a bit smarterQueryVariables
derive that makesto_input_literal
easier to use? Not sure.