GraphQL错误-参数类型必须是输入类型,但Get:undefined

GraphQL错误-参数类型必须是输入类型,但Get:undefined,graphql,Graphql,我有 及 它抛出: const query = new GraphQLObjectType({ name: 'Queries', fields: () => { return { getPictureList: getPictureList, getPicture: getPicture } } }); const schema = new GraphQLSchema({ qu

我有

它抛出:

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

const schema = new GraphQLSchema({
    query: query,
    mutation: mutation
});
getPictureList工作正常,getPicture失败

我试着让getPicture成为一个GraphQList,但没有帮助

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

你知道这是怎么回事吗

堆栈跟踪是:

Error: Queries.getPicture(type:) argument type must be Input Type but got: undefined.
错误:查询。getPicture(类型:)参数类型必须是输入类型,但Get:未定义。
at invariant(/home/jrootham/dev/trytheseon/node_modules/graphql/jsutils/invariant.js:19:11)
at/home/jrootham/dev/trytheson/node_modules/graphql/type/definition.js:307:33
at Array.map(本机)
at/home/jrootham/dev/trytheseon/node_modules/graphql/type/definition.js:304:52
at Array.forEach(本机)
在defineFieldMap(/home/jrootham/dev/trytheson/node_modules/graphql/type/definition.js:293:14)
在GraphQLObjectType.getFields(/home/jrootham/dev/trytheson/node_modules/graphql/type/definition.js:250:46)
at/home/jrootham/dev/trytheson/node_modules/graphql/type/schema.js:224:27
在typeMapReducer(/home/jrootham/dev/trytheseon/node_modules/graphql/type/schema.js:236:7)
at Array.reduce(本机)
在新的GraphQLSchema(/home/jrootham/dev/trytheson/node_modules/graphql/type/schema.js:104:34)
反对。(/home/jrootham/dev/trytheson/server/graphQL.js:45:16)
在模块处编译(Module.js:399:26)
在normalLoader(/usr/lib/node_modules/babel/node_modules/babel core/lib/babel/api/register/node.js:160:5)
at Object.require.extensions.(匿名函数)[as.js](/usr/lib/node_modules/babel/node_modules/babel core/lib/babel/api/register/node.js:173:7)
在Module.load(Module.js:345:32)
在Function.Module.\u加载(Module.js:302:12)
at Module.require(Module.js:355:17)
根据需要(内部/module.js:13:17)
反对。(/home/jrootham/dev/trytheson/devServer.js:14:44)
在模块处编译(Module.js:399:26)
在normalLoader(/usr/lib/node_modules/babel/node_modules/babel core/lib/babel/api/register/node.js:160:5)

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

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)
假设您想要一个名为“type”的参数,它应该是:

export const getPicture = {
    type: GraphPicture,
    args: {
        type: new GraphQLNonNull(GraphQLInt)
    },
    resolve(_, args) {
        return Picture.findByPrimary(args.id);
    }
};
以下是graphql js repo中的一个示例:

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

请参见此处graphql js repo中的完整模式:

仅供参考,我在此处搜索相同的错误,但不是使用
未定义的
,而是使用我自己的
产品
类型:

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中,传递给变异的应该是标量类型(字符串、整数等)或用户定义的
输入类型,而不是
类型

我试图通过我的产品
类型

input ProductInput {
  id: ID
  name: String
}

这感觉有点多余,但我想,我很高兴以后能把它分开

不确定它是否相关,但您只指定了类型,似乎缺少参数名(id)。@OP,在根查询中参数化
getPicture
。在不同的注释中,您可以考虑命名为代码>图片< /代码>,而不是<代码> GETFIGON/COD>。这更像是一种约定。@传入架构定义的AhmadFerdous查询不需要参数化,它们是通过查询调用本身传递的参数。@BradDecker,谢谢!我不知道。更具体地说,您需要从
graphql
中将您的输入类型定义为
new GraphQLObjectType
,就像这个答案解释的那样:我也遇到了同样的问题,这对我帮助很大。你的链接似乎已经死了,不确定正确的链接应该是什么。@gkri最终是的。我正在使用GraphQL模式语言定义模式,并将其传递给Apollo的
makeExecutableSchema
更多信息:
... argument * must be Input Type but got Product
input ProductInput {
  id: ID
  name: String
}
type Product {
  id: ID
  name: String
}