简体   繁体   English

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

[英]GraphQL error - argument type must be Input Type but got: undefined

I have 我有

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

and

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

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

Which throws: 哪个投掷:

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

getPictureList works fine, getPicture fails. getPictureList工作正常,getPicture失败。

I have tried making getPicture a GraphQLLIst, doesn't help. 我试过让getPicture成为GraphQLLIst,但没有帮助。

I have tried making the args type an Input Type, doesn't help. 我试过让args类型为输入类型,但没有帮助。

Any ideas about what is going on here 关于这里发生了什么的任何想法

The stack trace is: 堆栈跟踪是:

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)

Just for reference, I got here searching the same error just not with undefined , but with my own Product type: 仅供参考,我在这里搜索相同的错误,只是没有使用undefined ,但使用我自己的Product类型:

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

Turned out that in GraphQL what you pass to a Mutation should be either scalar type (String, Integer, ...) or user defined input type, not type type. 事实证明,在GraphQL中,传递给Mutation的内容应该是标量类型(String,Integer,...)或用户定义的input类型,而不是type类型。

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

input ProductInput {
  id: ID
  name: String
}

I was trying to pass in my Product type : 我试图传递我的产品type

type Product {
  id: ID
  name: String
}

This feels a bit redundant, but guess, I'll be glad for having it separated later. 这感觉有点多余,但猜测,我会很高兴以后将它分开。

The problem isn't the return type of the query but the type you have specified to your args. 问题不是查询的返回类型,而是您为args指定的类型。

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

Assuming you meant to have an argument named "type" it should be: 假设你打算有一个名为“type”的参数,它应该是:

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

here is an example from the graphql-js repo: 这是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),
},

See the full schema from the graphql-js repo here: https://github.com/graphql/graphql-js/blob/master/src/ tests /starWarsSchema.js 请参阅graphql-js repo中的完整模式: https//github.com/graphql/graphql-js/blob/master/src/ tests /starWarsSchema.js

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GraphQL:必须为输出类型,但得到:未定义错误 - GraphQL : must be Output Type but got: undefined Error GraphQL Args错误:参数类型必须是输入类型但得到:函数GraphQLObjectType(config){ - GraphQL Args error: argument type must be Input Type but got: function GraphQLObjectType(config) { GraphQL 错误字段类型必须是输入类型,但得到: - GraphQL Error field type must be Input Type but got: GraphQL] […]的类型必须是Output Type,但得到:未定义 - GraphQL] The type of […] must be Output Type but got: undefined graphql猫鼬必须为Output Type,但得到:undefined - graphql mongoose must be Output Type but got: undefined 具有Keystone的GraphQL希望字段类型必须为Output Type,但得到:未定义 - GraphQL with Keystone wants field type must be Output Type but got: undefined GraphQL嵌套类型:“…的类型必须是输出类型,但是得到:未定义。” - GraphQL nested types: “The type of … must be Output Type but got: undefined.” Graphql 日期字段类型必须是输出类型但得到:未定义 - Graphql date field type must be Output Type but got: undefined 使用thunk的GraphQL循环引用-错误:字段类型必须为Output Type,但得到:undefined - GraphQL cyclical reference using thunks - Error: field type must be Output Type but got: undefined 为什么我的Graphql项目抛出错误:字段类型必须为Output Type但得到:undefined - Why is my Graphql project throwing Error: field type must be Output Type but got: undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM