Seu Madruga
Seu Madruga

Reputation: 323

How to set a default value to an argument in a programatically defined schema?

I can't seem to find how to do it in the official GraphQL documentation.

A (very) simple example:

let PostType = new GraphQLObjectType({
    name: 'post',
    description: 'Represents a blog post',
    fields() {
       return {
           title: {
               type: GraphQLString,
               args: {
                   id: {
                      // HOW CAN I MAKE THIS ARG (id) OPTIONAL
                      // OR SET A DEFAULT VALUE TO IT?
                      type: GraphQLString
                    }
               }
           }
       }
    }
});

Upvotes: 2

Views: 6225

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84677

From the docs:

type GraphQLArgumentConfig = {
  type: GraphQLInputType;
  defaultValue?: any;
  description?: ?string;
}

So in addition to a type, just define a defaultValue for that argument.

Upvotes: 2

Related Questions