GraphQL-js 突变可选参数

Nic*_*mid 2 javascript node.js graphql-js

我将如何在具有可选参数的 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;
                })
            }
        }
    })
});
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助!

Chr*_*ing 5

GraphQL 中的类型默认可以为空。这意味着您目前指定突变的方式使计数成为可选的。如果您希望某个字段是强制性的,则需要将其标记为非空