简体   繁体   English

GraphQL-js突变可选参数

[英]GraphQL-js mutation optional arguments

how would I achieve a GraphQL Mutation in nodeJS that has arguments which are optional? 如何在具有可选参数的nodeJS中实现GraphQL突变?

My current mutation has an args field, but all the arguments are mandatory. 我当前的变异有一个args字段,但是所有参数都是必需的。 Since I couldn't find anything in the documentation, I don't know how or if this is possible. 由于我在文档中找不到任何内容,因此我不知道如何或是否可行。

This is my current code: 这是我当前的代码:

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;
                })
            }
        }
    })
});

Thanks for Your help! 谢谢你的帮助!

Types in GraphQL are nullable by default. GraphQL中的类型默认情况下可以为空。 That means that the way you have specified the mutation at the moment makes the count optional. 这意味着您目前指定突变的方式使计数成为可选项。 If you wanted a field to be mandatory you need to mark it as non null 如果希望字段为必填字段,则需要将其标记为非null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM