-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Using variables in scalars #1011
Comments
Hi @cutsoy , while not explicitly mentioned in the GraphQL spec, custom Scalars are responsible for resolving variables references themself. Almost all custom Scalars are simple enough that this is never an issue, but more complex one need to take care of resolving variables. For example you can see the logic resolving variable values for a JS JSON Scalar here: https://github.com/taion/graphql-type-json/blob/master/src/index.js#L39 or the same logic for a Java implementation here: https://github.com/graphql-java/graphql-java-extended-scalars/blob/master/src/main/java/graphql/scalars/object/ObjectScalar.java#L82 The reason why custom Scalars are responsible for resolving variables reference themself, is that it is actually up to the custom Scalars to define the semantics of a variable reference like Hope that helps. |
Ah, interesting! Does this also imply that scalar literals aren't checked for undefined variables? For example, would the following be valid (depending on the schema)? query Example {
hello(custom: { name: $name })
} (note that |
No, that wouldn't be valid because it fails https://spec.graphql.org/draft/#sec-All-Variable-Uses-Defined |
Got it, thanks! And also, are the variables already parsed before they are passed into the Specifically, this line suggests that the variables are already parsed (hence Example: scalar Foo
scalar Bar
query Example($foo: Foo!) {
greeting(bar: { foo: $foo })
} In this case, would const $foo = Foo.parseValue("example");
Bar.parseLiteral(
ast`{ foo: $foo }`, // ast
{ $foo }, // variables
) ... or like this? Bar.parseLiteral(
ast`{ foo: $foo }`, // ast
{ $foo: "example" }, // variables
) |
Hm, surprisingly, it looks like the graphql-js implementation calls
Why is |
I think you should redirect those questions to the |
I think it's worth a clarification in the spec? With the given example: query Example($name: String!) {
hello(custom: { name: $name })
}
But it's not really possible to know the type of the usage here. Maybe add an explicit case for custom scalars?
|
I agree a change to the spec may be necessary, perhaps you'd like to raise an RFC with your proposed edits and add it to an upcoming GraphQL Working Group? e.g. https://github.com/graphql/graphql-wg/blob/main/agendas/2023/03-Mar/02-wg-primary.md |
I would like some clarification on using variables in scalars.
Given the following schema:
We know that the following query could be valid (depending on
Custom
's wire format):Would the following example also be valid?
If so, does the executor first resolve
$name
and then call the scalar's deserializer with{ name: "Tim" }
?Can someone shed some light on this?
The text was updated successfully, but these errors were encountered: