【问题标题】:Why doesn't GraphQL accept this scalar argument type, instead complaining "argument type must be Input Type but got: undefined"?GraphQL 错误 - 参数类型必须是输入类型但得到:未定义
【发布时间】:2016-10-14 07:03:28
【问题描述】:

我有

export const getPicture = {
    type: GraphPicture,
    args: {
        type: new GraphQLNonNull(GraphQLInt)
    },
    resolve(_, args) {
        return Picture.findByPrimary(args.id);
    }
};

export const getPictureList = {
    type: new GraphQLList(GraphPicture),
    resolve(_, __, session) {
        return Picture.findAll({where: {owner: session.userId}});
    }
};

const query = new GraphQLObjectType({
    name: 'Queries',
    fields: () => {
        return {
            getPictureList: getPictureList,
            getPicture: getPicture
        }
    }
});

const schema = new GraphQLSchema({
    query: query,
    mutation: mutation
});

哪个抛出:

Error: Queries.getPicture(type:) argument type must be Input Type but got: undefined.

getPictureList 工作正常,getPicture 失败。

我已尝试将 getPicture 设为 GraphQLLIst,但没有帮助。

我尝试将 args 类型设置为输入类型,但没有帮助。

关于这里发生了什么的任何想法

堆栈跟踪是:

Error: Queries.getPicture(type:) argument type must be Input Type but got: undefined.
at invariant (/home/jrootham/dev/trytheseon/node_modules/graphql/jsutils/invariant.js:19:11)
at /home/jrootham/dev/trytheseon/node_modules/graphql/type/definition.js:307:33
at Array.map (native)
at /home/jrootham/dev/trytheseon/node_modules/graphql/type/definition.js:304:52
at Array.forEach (native)
at defineFieldMap (/home/jrootham/dev/trytheseon/node_modules/graphql/type/definition.js:293:14)
at GraphQLObjectType.getFields (/home/jrootham/dev/trytheseon/node_modules/graphql/type/definition.js:250:46)
at /home/jrootham/dev/trytheseon/node_modules/graphql/type/schema.js:224:27
at typeMapReducer (/home/jrootham/dev/trytheseon/node_modules/graphql/type/schema.js:236:7)
at Array.reduce (native)
at new GraphQLSchema (/home/jrootham/dev/trytheseon/node_modules/graphql/type/schema.js:104:34)
at Object.<anonymous> (/home/jrootham/dev/trytheseon/server/graphQL.js:45:16)
at Module._compile (module.js:399:26)
at normalLoader (/usr/lib/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:160:5)
at Object.require.extensions.(anonymous function) [as .js] (/usr/lib/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:173:7)
at Module.load (module.js:345:32)
at Function.Module._load (module.js:302:12)
at Module.require (module.js:355:17)
at require (internal/module.js:13:17)
at Object.<anonymous> (/home/jrootham/dev/trytheseon/devServer.js:14:44)
at Module._compile (module.js:399:26)
at normalLoader (/usr/lib/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:160:5)

【问题讨论】:

  • 不确定是否相关,但您只指定了类型,似乎缺少参数名称 (id)。
  • @OP,在根查询中参数化getPicture。另一方面,您可以考虑将picture 命名为getPicture。这更像是一种约定。
  • @AhmadFerdous 传递到架构定义的查询不需要参数化,它们是通过查询调用本身传递的参数。
  • @BradDecker,谢谢!我不知道。

标签: graphql


【解决方案1】:

仅供参考,我在这里搜索了同样的错误,但不是用undefined,而是用我自己的Product类型:

... argument * must be Input Type but got Product

事实证明,在 GraphQL 中传递给 Mutation 的内容应该是标量类型(字符串、整数等)或用户定义的 input 类型,而不是 type 类型。

http://graphql.org/graphql-js/mutations-and-input-types/

input ProductInput {
  id: ID
  name: String
}

我试图传递我的产品type

type Product {
  id: ID
  name: String
}

这感觉有点多余,但猜猜,我会很高兴稍后将它分开。

【讨论】:

  • 更具体地说,您需要将输入类型定义为来自graphqlnew GraphQLObjectType,就像这个答案解释的那样:stackoverflow.com/a/35883106/4430640
  • 我遇到了同样的问题,这对我帮助很大。不过,您拥有的链接似乎已失效,不确定正确的链接应该是什么。
  • @gkri 最终是的。我正在使用 GraphQL 模式语言 定义我的模式并将其传递给 Apollo 的 makeExecutableSchema 更多:apollographql.com/docs/graphql-tools/generate-schema.html
【解决方案2】:

问题不在于查询的返回类型,而是您为参数指定的类型。

export const getPicture = {
    type: GraphPicture,
    args: {
        type: new GraphQLNonNull(GraphQLInt)
    },
    resolve(_, args) {
        return Picture.findByPrimary(args.id);
    }
};

假设您打算有一个名为“type”的参数,它应该是:

export const getPicture = {
    type: GraphPicture,
    args: {
        type: {
             type: new GraphQLNonNull(GraphQLInt)
        }
    },
    resolve(_, args) {
        return Picture.findByPrimary(args.id);
    }
};

这是来自 graphql-js 存储库的示例:

hero: {
  type: characterInterface,
  args: {
    episode: {
      description: 'If omitted, returns the hero of the whole saga. If ' +
                   'provided, returns the hero of that particular episode.',
      type: episodeEnum
    }
  },
  resolve: (root, { episode }) => getHero(episode),
},

在此处查看来自 graphql-js 存储库的完整架构:https://github.com/graphql/graphql-js/blob/master/src/tests/starWarsSchema.js

【讨论】:

    猜你喜欢
    • 2019-09-19
    • 2018-06-30
    • 2018-01-30
    • 2016-12-31
    • 2019-12-23
    • 2018-12-18
    • 2017-08-16
    • 2017-09-03
    • 2018-06-25
    相关资源
    最近更新 更多