graphql-js icon indicating copy to clipboard operation
graphql-js copied to clipboard

Default value for custom scalar type

Open denneulin opened this issue 10 years ago • 17 comments

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 ?

denneulin avatar Aug 11 '16 16:08 denneulin

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?

stubailo avatar Aug 11 '16 22:08 stubailo

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.

denneulin avatar Aug 12 '16 07:08 denneulin

Have you tried with Limit = "20"?

helfer avatar Aug 12 '16 20:08 helfer

same result, limit is false :(

denneulin avatar Aug 13 '16 09:08 denneulin

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

leebyron avatar Aug 16 '16 06:08 leebyron

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.

leebyron avatar Aug 16 '16 06:08 leebyron

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;
    },
  }

denneulin avatar Aug 17 '16 15:08 denneulin

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?

leebyron avatar Aug 24 '16 22:08 leebyron

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 avatar Sep 13 '16 07:09 denneulin

@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!

helfer avatar Sep 13 '16 07:09 helfer

graphql-tools issue 138 created ;)

denneulin avatar Sep 13 '16 09:09 denneulin

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..

DxCx avatar Sep 14 '16 18:09 DxCx

I'm hitting this bug too.

mxstbr avatar Feb 20 '18 10:02 mxstbr

@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?

IvanGoncharov avatar Feb 20 '18 12:02 IvanGoncharov

Ohh thanks @IvanGoncharov! I'm still on v0.11 so that's why I'm experiencing that bug. Let's close this issue then 👌

mxstbr avatar Feb 20 '18 12:02 mxstbr

@denneulin Can you please test if your problem was fixed in v0.12.0?

IvanGoncharov avatar Feb 20 '18 15:02 IvanGoncharov

Fixed in 714ee980aa17a4de42013d49a9839d43493d109e and released in v0.12.0

IvanGoncharov avatar May 23 '18 21:05 IvanGoncharov