如何在GraphQL中插入(Array)字段列表进行变异查询

最近我开始研究GraphQL,我能够插入平面模式的数据没有任何问题,但是当涉及到一个数组数组我得到一个错误

{ "errors": [ { "message": "Must be input type" } ]} 

我正在testing我的查询使用邮递员,我的变异查询是

 mutation M { AddEvent ( title: "Birthday event" description:"Welcome to all" media:[{url:"www.google.com", mediaType:"image" }] location:[{address:{state:"***", city:"****"}}] ) {title,description,media,location,created,_id}} 

这是我的事件架构:

 EventType = new GraphQLObjectType({ name: 'Event', description: 'A Event', fields: () => ({ _id: { type: GraphQLString, description: 'The id of the event.', }, id: { type: GraphQLString, description: 'The id of the event.', }, title: { type: GraphQLString, description: 'The title of the event.', }, description: { type: GraphQLString, description: 'The description of the event.', }, media:{ type:new GraphQLList(mediaType), description:'List of media', }, location:{ type:new GraphQLList(locationType), description:'List of location', } }) }); // Media Type export var mediaType = new GraphQLObjectType({ name: 'Media', description: 'A Media', fields: () => ({ _id: { type: GraphQLString, description: 'The id of the event.', }, url:{ type: GraphQLString, description: 'The url of the event.', }, mediaType:{ type: GraphQLString, description: 'The mediaTypa of the event.', } }) }); // Location Type export var locationType = new GraphQLObjectType({ name: 'Location', description: 'A location', fields: () => ({ _id: { type: GraphQLString, description: 'The id of the event.', }, address:{ type: GraphQLString, description: 'The address.', }, state:{ type: GraphQLString, description: 'The state.', }, city:{ type: GraphQLString, description: 'The city.', }, zip:{ type: GraphQLString, description: 'The zip code.', }, country:{ type: GraphQLString, description: 'The country.', } }) }); 

mongoose纲要:

 var EventSchema = new mongoose.Schema({ title: { required: true, type: String, trim: true, match: /^([\w ,.!?]{1,100})$/ }, description: { required: false, type: String, trim: true, match: /^([\w ,.!?]{1,100})$/ }, media: [{ url: { type: String, trim: true }, mediaType: { type: String, trim: true } }], location: [{ address: { type: String }, city: { type: String }, state: { type: String }, zip: { type: String }, country: { type: String } }] }) 

突变types:

  addEvent: { type: EventType, args: { _id: { type: GraphQLString, description: 'The id of the event.', }, title: { type: GraphQLString, description: 'The title of the event.', }, description: { type: GraphQLString, description: 'The description of the event.', }, media:{ type:new GraphQLList(mediaType), description:'List of media', }, location:{ type:new GraphQLList(locationType), description:'List of media', }, created: { type: GraphQLInt, description: 'The created of the user.', } }, resolve: (obj, {title,description,media,location,created,_id}) => { let toCreateEvent = { title, description, created:new Date(), start: new Date(), media, location, _id, }; return mongo() .then(db => { return new Promise( function(resolve,reject){ let collection = db.collection('events'); collection.insert(toCreateEvent, (err, result) => { db.close(); if (err) { reject(err); return; } resolve(result); }); }) }); } } 

你的问题是,当你定义突变时,所有types必须是inputtypes,因此你得到的错误"Must be input type" 。 所以在这里(从你的突变):

 media:{ type:new GraphQLList(mediaType), description:'List of media', }, location:{ type:new GraphQLList(locationType), description:'List of media', }, 

GraphQLListmediaTypelocationType必须是inputtypes。

GraphQLList已经是一个inputtypes(请参阅https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82来查看被视为inputtypes的GraphQLtypes列表) 。

但是,您的typesmediaTypelocationTypeGraphQLObjectTypetypes,它不是inputtypes,但是如果再次查看inputtypes的列表: https : //github.com/graphql/graphql-js/blob/master/src/type /definition.js#L74-L82 ,你会发现GraphQLInputObjectType是一个对象inputtypes,所以你需要做的是用他们的“input”版本replacemediaTypelocationType

我build议做的是创buildmediaInputTypelocationInputType ,它将具有与mediaTypelocationType相同的字段结构,但是使用new GraphQLInputObjectType({...而不是new GraphQLObjectType({...并在您的突变中使用它们。

我遇到了同样的问题,我解决了这个问题,如果您有任何问题,请随时发表评论。

我遇到了同样的问题 – 我不知道如何在input定义中指定对象的数组。 所以对于那些想看到“文本”模式解决scheme的人来说:

 type Book { title: String! } 

在您的inputtypes中拥有一组书籍

 input AuthorInput { name: String! age: Int! } 

你不能只是在input语句里面添加books: [Book!] ,你需要有意地创build包含需要的字段的inputtypes(如果你愿意的话):

 input BookInput { title: String! } 

然后你可以:

 input AuthorInput { name: String! age: Int! books: [BookInput!] }