Default value for custom scalar type
Hi,
I have the following endpoint in my schema
users(limit: Int = 20): [User]
I use the limit option in many requests and the value has to be between 1 and 20.
To avoid writing a check in every resolvers, I have created a custom scalar Limit.
Now, I have this endpoint :
users(limit: Limit): [User]
It works perfect but I cannot set a default value to have this :
users(limit: Limit = 20): [User]
Do you have any idea ?
What errors do you see when you try to do this? Are you writing your schema using the schema language? What package are you using to do so?
I am using apollo-server
I have no error but if I do not set limit in my query then in the resolver limit is false.
Have you tried with Limit = "20"?
same result, limit is false :(
I would suggest against a custom scalar for this purpose, it usually leads to more confusing type systems.
Nevertheless, this seems like a potential bug. Thanks for the report
Could you share the code you're using for the definition of Limit? One guess I have is that you've either not defined or misdefined its parseLiteral function.
Limit: {
__description: 'The Limit scalar type is an integer between 1 and 20 and represents the maximum items you can retrieve.',
__parseValue(value) {
return value;
},
__serialize(value) {
return value;
},
__parseLiteral(ast) {
if (ast.kind !== Kind.INT) {
throw new GraphQLError(`Query error: Can only parse Int got a: ${ast.kind}.`, [ast]);
}
const parsedLimit = parseInt(ast.value, 10);
const min = config.get('query.limit.min');
const max = config.get('query.limit.max');
if (parsedLimit < min) {
throw new GraphQLError(`Minimum value for "limit" is ${min}.`, [ast]);
}
if (parsedLimit > max) {
throw new GraphQLError(`Maximum value for "limit" is ${max}.`, [ast]);
}
return parsedLimit;
},
}
A few things come to mind here based on this code:
The parseValue function is not doing the same checks as parseLiteral. That means if someone provides a variable to your function that it will not be checked in the same way as if a literal is provided.
Similarly with serialize, though it seems you're only using this scalar to accept inputs and not to return outputs, so it may be the case that you can omit that altogether.
Here is the place in the graphql-js library where that default value will be parsed and coerced:
https://github.com/graphql/graphql-js/blob/master/src/execution/values.js#L104 https://github.com/graphql/graphql-js/blob/master/src/utilities/valueFromAST.js#L113
I can't get a reproduction of what you're describing by writing something similar in a test, but I also notice that you're defining these functions with __ before them. Graphql-js Scalars do not accept functions like that, so it could be that you're using another library which is causing this issue?
Thanks @leebyron for your answer. I am using apollo-server developed by @helfer
Finally, if you don't get a reproduction, maybe the problem comes from apollo-server :(
@denneulin could you file the same issue on graphql-tools with a minimal reproduction? I don't think this is caused by graphql-tools, but if it is I'd like to get to the bottom of it. Thanks!
graphql-tools issue 138 created ;)
hi @leebyron
After taking a deeper look into this one, i've found out that the defaultValue is set to false by parser. so the following:
import { parse, buildASTSchema } from 'graphql';
const testDef = `
scalar Limit
type Query {
testLimit(limit: Limit = 20): String
}
`
const testSchema = buildASTSchema(parse(testDef));
console.log(testSchema.getQueryType().getFields()['testLimit']['args'][0])
results in:
{
"name":"limit","description":"",
"type":{
"name":"Limit",
"description":"",
"_scalarConfig":{"name":"Limit","description":""}
},
"defaultValue":false
}
as we can see, defaultValue is set to false by parser, and not to 20.. so we do think it is related to graphql-js parser and not to graphql-tools..
I'm hitting this bug too.
@mxstbr Bug that @DxCx reported was fixed in 714ee980aa17a4de42013d49a9839d43493d109e and released in v0.12.0.
See test case:
https://github.com/graphql/graphql-js/blob/86d33b49a92eda05ee9ca7b3a6fe3a449a21de4c/src/utilities/tests/buildASTSchema-test.js#L520-L530
Do you still experience the same issue or did you meant another scenario? What version of GraphQL do you use?
Ohh thanks @IvanGoncharov! I'm still on v0.11 so that's why I'm experiencing that bug. Let's close this issue then 👌
@denneulin Can you please test if your problem was fixed in v0.12.0?
Fixed in 714ee980aa17a4de42013d49a9839d43493d109e and released in v0.12.0