如何在以编程方式定义的模式中为参数设置默认值?
How to set a default value to an argument in a programatically defined schema?
我似乎无法在官方 GraphQL 文档中找到如何操作。
一个(非常)简单的例子:
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
}
}
}
}
}
});
来自docs:
type GraphQLArgumentConfig = {
type: GraphQLInputType;
defaultValue?: any;
description?: ?string;
}
因此,除了 type
之外,只需为该参数定义一个 defaultValue
。
我似乎无法在官方 GraphQL 文档中找到如何操作。
一个(非常)简单的例子:
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
}
}
}
}
}
});
来自docs:
type GraphQLArgumentConfig = {
type: GraphQLInputType;
defaultValue?: any;
description?: ?string;
}
因此,除了 type
之外,只需为该参数定义一个 defaultValue
。