GraphQL Args错误:参数类型必须是输入类型但得到:函数GraphQLObjectType(config){ [英] GraphQL Args error: argument type must be Input Type but got: function GraphQLObjectType(config) {

查看:135
本文介绍了GraphQL Args错误:参数类型必须是输入类型但得到:函数GraphQLObjectType(config){的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务器启动时( node index.js )我的GraphQL NodeJS服务器出现以下错误:

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

错误:Query.payment(data :)参数类型必须是输入类型但得到:
函数GraphQLObjectType(config){
_classCallCheck(this,GraphQLObjectType);

Error: Query.payment(data:) argument type must be Input Type but got: function GraphQLObjectType(config) { _classCallCheck(this, GraphQLObjectType);

当我从字符串中更改原始参数时发生此错误

This error happened when I changed my original args from a string

        args: {
            data: { type: graphQL.GraphQLString }
        },

对象类型:

        args: {
            data: { type: graphQL.GraphQLObjectType }
        },

我需要一个对象类型,因为我需要发送几个字段如同参数。

I need an object type as I need to send several fields as params.

GraphQL服务器:

var Query = new graphQL.GraphQLObjectType({
    name: 'Query',
    fields: {
        payment: {
            type: graphQL.GraphQLString,
            args: {
                data: { type: graphQL.GraphQLObjectType }
            },
            resolve: function (_, args) {
                // There will be more data here, 
                // but ultimately I want to return a string
                return 'success!';
            }
        }
    }
});

我如何允许它接受一个对象?

How can I allow it to accept an object?

前端(如果需要。但错误发生在我发送之前):

Frontend (if needed. But the error is happening before I even send this):

var userQuery = encodeURIComponent('{ payment ( data: { user : "test" } )}');

$.get('http://localhost:4000/graphql?query=' + userQuery, function (res) {
       //stuff
});


推荐答案

如果你想使用Object作为参数,你应该使用 GraphQLInputObjectType 而不是 GraphQLObjectType 。请记住,GraphQL是基于强类型的,因此不允许使用泛型 GraphQLObjectType 作为arg类型,然后动态查询args。您必须在此输入对象中明确定义所有可能的字段(并选择哪些字段是强制性的,哪些不是强制字段)

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)

尝试使用此方法:

// 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!';
            }
        }
    }
});

这篇关于GraphQL Args错误:参数类型必须是输入类型但得到:函数GraphQLObjectType(config){的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆