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

example: variable inlining #1007

Closed
wants to merge 1 commit into from
Closed

example: variable inlining #1007

wants to merge 1 commit into from

Conversation

obmarg
Copy link
Owner

@obmarg obmarg commented Aug 21, 2024

TODO:

  • Commit message
  • Document in the book
  • Possibly build an actual shopify example
  • Use to_input_literal to make it a bit smarter
  • Possibly add functionality to the QueryVariables derive that makes to_input_literal easier to use? Not sure.

Copy link

netlify bot commented Aug 21, 2024

Deploy Preview for cynic-querygen-web canceled.

Name Link
🔨 Latest commit d1cdc40
🔍 Latest deploy log https://app.netlify.com/sites/cynic-querygen-web/deploys/66c65dabc96a700008bcb740

@Bobrokus
Copy link
Contributor

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.

@Bobrokus
Copy link
Contributor

Bobrokus commented Aug 22, 2024

In this article they call it "Inline Argument". I guess it doesn't really matter.

@obmarg
Copy link
Owner Author

obmarg commented Aug 22, 2024

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.

obmarg added a commit that referenced this pull request Aug 23, 2024
#### 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
@obmarg
Copy link
Owner Author

obmarg commented Aug 23, 2024

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.

@obmarg obmarg closed this Aug 23, 2024
obmarg added a commit that referenced this pull request Aug 23, 2024
#### 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.
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.

2 participants