All posts

How to set default values in Graphql Schemas

I think setting default GQL values is best illustrated through two annotated examples:

1) Setting a default for a non-nested type:

In the below example the CoffeeOrder type has a field of size which sets a default value for the CupSize type of SMALL.

type CoffeeOrder {
  name: String!
  size: CupSize = SMALL
}

Annotated spec:

field: FieldType = defaultValue

2) Setting a default for a nested type:

The below example is a different implementation of the CoffeeOrder type. This time there is a field cup that contains a field size, which has a default value of SMALL:

type CoffeeOrder {
  name: String!
  cup(size: CupSize = SMALL): Cup
}

Annotated spec:

field(subfield: SubFieldType = defaultValue: FieldType

For your continued enjoyment: