【问题标题】:GraphQL Args error: argument type must be Input Type but got: function GraphQLObjectType(config) {GraphQL Args 错误:参数类型必须是输入类型,但得到:函数 GraphQLObjectType(config) {
【发布时间】:2016-12-31 01:32:15
【问题描述】:

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

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

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

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

到一个对象类型:

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

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

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

我怎样才能让它接受一个对象?


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

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

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

【问题讨论】:

    标签: javascript node.js graphql


    【解决方案1】:

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

    尝试使用这种方法:

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

    【讨论】:

    • 感谢戴夫勋爵。我最终将其更改为 mutation 并指定字段。顺便说一句,还有两个问题:1) 有没有比encodeURIComponent('{ payment ( data: { user : "test" } )}'); 更好的方法来编写我的 POST 查询?如果对象很大,手动指定每个字段会很麻烦。 2) GraphQL 似乎需要 Query,尽管我只需要 Mutation。我是否需要始终进行查询,或者在某处有选项?
    • 1) 我假设您正在使用 jquery ajax 根据您给定的示例进行 graphql 调用。不幸的是,我无法为您提供帮助,因为在我的项目中,我在 ReactJS 前端使用了 Relay,它负责此操作。 2) 显然,架构中始终需要Query。看看graphql-jscode 我希望我至少对你有所帮助
    • 谢谢!!!自从这篇文章两年后,文档仍然没有说清楚。我浪费了一整天,直到我看到这个帖子。感谢@lorddave 的解决方案!
    猜你喜欢
    • 2016-10-14
    • 2018-01-30
    • 2019-09-19
    • 2018-06-30
    • 2019-03-15
    • 2023-01-17
    • 2018-12-18
    • 2019-12-23
    相关资源
    最近更新 更多