如何进行变异查询以在GraphQL中插入(Array)字段列表 [英] How to make a mutation query for inserting a list of (Array) fields in GraphQL
问题描述
最近我开始研究GraphQL,我能够在平面模式中插入数据而没有任何问题,但是当涉及到数据数组时,我会遇到类似的错误
recently I started working on GraphQL, I am able to insert data in flat schema without any problem but when it comes to an Array of data I am getting an error like
{ "errors": [ { "message": "Must be input type" } ]}
我正在使用邮递员测试我的查询,我的突变查询是
I am testing my query using postman, my mutation query is
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.',
}
})
});
猫鼬模式:
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
}
}]
})
突变类型:
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);
});
})
});
}
}
推荐答案
您的问题是,定义突变时,所有类型都必须是输入类型,因此会出现错误"Must be input type"
.所以在这里(来自您的突变):
Your issue is that when you define mutations, all types must be input types, hence the error you get "Must be input type"
. So in here (from your mutation):
media:{
type:new GraphQLList(mediaType),
description:'List of media',
},
location:{
type:new GraphQLList(locationType),
description:'List of media',
},
GraphQLList
,mediaType
和locationType
必须是输入类型.
GraphQLList
, mediaType
and locationType
must be input types.
GraphQLList
已经是输入类型(请参见此处 https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82 以查看被视为输入类型的GraphQL类型列表)
GraphQLList
is already an input type (see here https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82 to see the list of GraphQL types considered as input types).
但是您的类型mediaType
和locationType
是GraphQLObjectType
类型,它不是输入类型,但是如果您再次查看输入类型列表:
However your types mediaType
and locationType
are of GraphQLObjectType
type, which is not an input type but if you look at the list of input types again: https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82, you'll find GraphQLInputObjectType
which is an object input type, so, what you need to do is to replace mediaType
and locationType
by their "input" version.
我建议要做的是创建与mediaType
和locationType
具有相同字段结构但使用new GraphQLInputObjectType({...
而不是new GraphQLObjectType({...
创建的mediaInputType
和locationInputType
,并在突变中使用它们
What I suggest to do is to create mediaInputType
and locationInputType
which would have the same field structure as mediaType
and locationType
but created with new GraphQLInputObjectType({...
instead of new GraphQLObjectType({...
and use them in your mutation.
我遇到了同样的问题,我就这样解决了,如果您有任何疑问,请随时发表评论.
I ran into the same issue and I solved it like that, feel free to comment if you have any question.
这篇关于如何进行变异查询以在GraphQL中插入(Array)字段列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!