Graphql-js: Can Types have default values?

Created on 5 Apr 2016  路  6Comments  路  Source: graphql/graphql-js

For example: GraphQLBoolean. Can it default to false as it does in JavaScript? I know query variables can have default values, but I can't find any info for default types. GraphQLBooleans appear to default to null, as other GraphQL types.

Most helpful comment

Types themselves never have default values, no, as that would presume too much about each contextual use.

Instead, default values are declared next to where they're desired, query variables being a good example.

All 6 comments

Types themselves never have default values, no, as that would presume too much about each contextual use.

Instead, default values are declared next to where they're desired, query variables being a good example.

Instead, default values are declared next to where they're desired

Can you give a non-query variable example?

The other example, sort of related to variables, is field/directive arguments and input object fields. Arguments and input object fields can declare default values in the case that they're not provided: http://facebook.github.io/graphql/#sec-The-__InputValue-Type

Feel free to reopen if you wish to discuss further

Thanks. The one thread I did want to tug at: I've not seen any examples of defining queries with query variables in schemas.

Both https://learngraphql.com/basics/query-variables/5
and https://medium.com/the-graphqlhub/graphql-tour-variables-58c6abd10f56#.pus2p7wmz only reference GraphiQL and how _not_ to write them.

I'm assuming the format is this? https://facebook.github.io/graphql/#sec-Language.Variables

Yes the spec has a few examples of them in use as well as explainers for their use, but in general we need better documentation still.

Was this page helpful?
0 / 5 - 0 ratings