Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add default values to input arguments in graphql

I have this input type and I would like to add a default Value to one of the fields. I want to add 0 to the value field inside the ExampleInput.

type ExampleType {   value: Int   another: String }  type Mutation {   example(input: ExampleInput): ExampleType }  input ExampleInput {   value: Int   another: String } 

Any ideas?

like image 611
Adolfo Avatar asked Jul 27 '18 18:07

Adolfo


People also ask

How do I set default value in GraphQL?

In GraphQL you can assign Default values to the variables. It's done by by adding the default value after the type declaration in your query. If Default values are provided for all variables, you can call the query without passing any variables.

How do you pass an argument in GraphQL query?

When you're passing arguments in code, it's generally better to avoid constructing the whole query string yourself. Instead, you can use $ syntax to define variables in your query, and pass the variables as a separate map. . then(data => console.

What is __ Typename in GraphQL?

The __typename field returns the object type's name as a String (e.g., Book or Author ). GraphQL clients use an object's __typename for many purposes, such as to determine which type was returned by a field that can return multiple types (i.e., a union or interface).

How do you add variables in GraphiQL?

The GraphiQL client allows you to create variables for use in your queries. To add a query variable, click the Query Variables pane and enter a JSON object that defines your variable. To use a variable in your query, prepend the $ character to your variable name and use it to replace the desired value.


1 Answers

It looks like the grammar allows default values on input object types, so you can declare

input ExampleInput {   value: Int = 0   another: String   isAvailable: Boolean = false } 

The spec is clear that default values exist and explains how they get used (first bullet under "Input Coercion").

(Whether any specific tooling supports this is probably variable: graphql.org had an informal version of the IDL for quite a while before it was actually in the spec, and I have the impression some libraries haven't caught up to the released spec yet.)

like image 120
David Maze Avatar answered Oct 25 '22 07:10

David Maze