首页 文章

GraphQL-js变异可选参数

提问于
浏览
2

如何在nodeJS中实现具有可选参数的GraphQL Mutation?

我当前的变异有一个args字段,但所有参数都是强制性的 . 由于我在文档中找不到任何内容,我不知道这是怎么回事 .

这是我目前的代码:

const fakeDB = {
    count: 0
};

const schema = new GraphQLSchema({
    query: //...
    mutation: new GraphQLObjectType({
        name: 'adsF',
        fields: {
            updateCount: {
                type: GraphQLInt,
                args: {
                    count: { type: GraphQLInt } // I want to make this argument optional
                },
                resolve: (value, { count }) => {
                    // Catch if count is null or undefined
                    if (count == null) {
                        // If so just update with default
                        fakeDB.count = 5;
                    } else {
                        fakeDB.count = count
                    }
                    return fakeDB.count;
                })
            }
        }
    })
});

谢谢你的帮助!

1 回答

  • 4

    默认情况下,GraphQL中的类型可以为空 . 这意味着您此刻指定突变的方式使计数可选 . 如果您希望字段是必填字段,则需要将其标记为non null

相关问题