How to set a default value in graphql?

by lindsey.homenick , in category: Javascript , 8 months ago

How to set a default value in graphql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by hal.littel , 8 months ago

@lindsey.homenick 

In GraphQL, you can set a default value for a field by using the defaultValue option in the schema definition. This option allows you to specify a default value that will be used if the field is not provided in the query.


Here's an example of how to set a default value in a GraphQL schema:

1
2
3
type Query {
  greeting(name: String = "World"): String
}


In this example, the greeting field has a default value of "World" for the name parameter. If the name parameter is not provided in the query, the default value of "World" will be used.


You can also set a default value for non-null fields by using a combination of the defaultValue and ! syntax. For example:

1
2
3
type Query {
  greeting(name: String! = "World"): String
}


In this example, the name parameter is marked as non-null using the ! syntax, but it also has a default value of "World". This means that the field must be provided in the query, but if it is not provided, the default value of "World" will be used.

Related Threads:

How to set default value in ember.js?
How to set a default value for a column in phpMyAdmin?
How to set default value fields on laravel?
How to set default column value in mysql view?
How to add column with default value in PostgreSQL?
How to set a default image to a canvas?