Description
The parseLiteral
method of scalar and enum types expects the variables
as second argument (see the definition of the GraphQLScalarLiteralParser type). This argument is passed in valueFromAST.
However, the standard scalar types provided with GraphQL.js and the enum type do not make use of this variables argument at all, it's simply ignored. Also, the default parseLiteral
function for scalars does not pass the variables. I don't see it being used in other libraries (graphql-iso-date, graphql-scalars, graphql-type-json) either. This functionality is currently also neither tested nor documented (see #2567).
Before adding tests and documentation, we need to clarify the purpose of this second argument to parseLiteral
and must come up with a reasonable use case. Otherwise the argument should be removed, since it makes the API more complicated, and causes problems (in languages where argument passing is handled more strictly than in JavaScript) when this parameter is not considered.
The variables argument was added in 714ee98 by @leebyron with the comment in the commit message that "supporting variable values within custom scalar literals can be valuable". Can somebody come up with a concrete example for that? @IvanGoncharov mentioned complex (composite) scalars types like JSON, but I would like to see more concretely how it would be used here.
Another problem with this parameter is that parseLiteral
is acutally called twice, once without variables (during validation with ValueOfCorrectType) and during execution, with variables. See also #383 where it is suggested to use memoization to solve this, but this would not work well with the additional variables argument. Note that there is also an UnusedVariables rule in validation which prevents the use of variables which are not used explicitly in the document (and would be used only implicitly via parseLiteral
).
Activity
GraphQLScalarType.parse_literal
is called with two arguments instead of one when Query variables are present. graphql-python/graphql-core#93andimarek commentedon Jun 29, 2020
@Cito As @IvanGoncharov mentioned it is essential for more complex Scalars like
JSON
. See https://github.com/taion/graphql-type-json/blob/master/src/index.js#L42 for a real life implementation.The general explanation is:
A custom Scalar can accept arbitrary Ast Elements and these elements can include variable references which need to be resolved by the custom scalar itself (it can't be done by graphql.js directly).
For example you could imagine a
Money
scalar which accepts{value: "12.0", currency: "EUR"}
as literal. This Scalar should also be able to process variable references.:This
parseLiteral
method will now receive{value: $euros, currency: "EUR"}
and needs to convert it to a real value: this is why it also needs access to the variables in order to resolve the$euros
reference to "12.0".I hope that helps.
Cito commentedon Jul 3, 2020
@andimarek - I have tried this out now and created the following test. I think something like that should be included in the test suite, to have better coverage of custom scalars. The test confirms that variables are passed properly to parseLiteral. It also reveals the problem that when the query is validated with ValuesOfCorrectTypeRule, the parseLiteral function is called without variables.
andimarek commentedon Jul 5, 2020
@Cito I am not involved in the maintenance for this project. @IvanGoncharov or others can give you feedback.
GraphQLScalarType: default 'parseLiteral' should handle variables
GraphQLScalarType: default 'parseLiteral' should handle variables (#2696
yaacovCR commentedon May 22, 2022
Can this be closed? Should be covered by #2696 and #3065 (comment)
yaacovCR commentedon Oct 7, 2024
Closed more definitively by #3812