This is done with the defaultValue property. But this is not possible for the GraphQLObjectType as you show.
const UserType = new GraphQLObjectType({
name: 'User',
description: 'User type',
fields: () => ({
id: { type: GraphQLID },
username: { type: GraphQLString, defaultValue: 'default string' },
}),
});
Object literal may only specify known properties, and 'defaultValue' does not exist in type 'GraphQLFieldConfig<any, any, { [argName: string]: any; }>'
So GraphQLObjectType has no default Value property.
You need to solve this in a different place, not here. For example, when using data, if the value you want is empty, you can use default instead.
...
...
data.username ?? 'default string'
...
...
But where does this defaultValue property work? It works with GraphQLInputObjectType.
For example:
const filter = new GraphQLInputObjectType({
name: "Filter",
fields: () => ({
min: { type: new GraphQLNonNull(graphql.GraphQLInt) },
max: { type: graphql.GraphQLBoolean, defaultValue: 100 },
}),
});
and we can use it like this:
...
query: {
products: {
type: new GraphQLList(productTypes),
args: { filter: { type: new GraphQLNonNull(filter) } }, // <-----
resolve: allProducts,
},
},
...