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

查看:28
本文介绍了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天全站免登陆