GraphQL Args error: argument type must be Input Type but got: function GraphQLObjectType(config) {

前端 未结 1 1831
耶瑟儿~
耶瑟儿~ 2021-02-07 22:17

On server start (node index.js) I am getting the following error with my GraphQL NodeJS server:

Error: Query.payment(data:) argument type mus

相关标签:
1条回答
  • 2021-02-07 23:16

    If you want use Object as an argument, you should use GraphQLInputObjectType instead of GraphQLObjectType. And keep in mind that GraphQL is strongly type based, so you're not allowed to use a generic GraphQLObjectType as arg type and then dynamically query args. You have to explicitly define all possible fields in this input object (and choose which of them would be mandatory and which not)

    Try use this approach:

    // your arg input object
    var inputType = new GraphQLInputObjectType({
        name: 'paymentInput',
        fields: {
            user: {
                type: new GraphQLNonNull(GraphQLString)
            },
            order: {
                type: GraphQLString
            },
            ...another fields
        }
    });
    
    var Query = new graphQL.GraphQLObjectType({
        name: 'Query',
        fields: {
            payment: {
                type: graphQL.GraphQLString,
                args: {
                    data: { type: new GraphQLNonNull(inputType) }
                },
                resolve: function (_, args) {
                    // There will be more data here,
                    // but ultimately I want to return a string
                    return 'success!';
                }
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题