Mah*_*esh 22 node.js mongodb-query graphql
最近我开始研究GraphQL,我能够在没有任何问题的情况下在平面模式中插入数据但是当涉及数据数组时,我收到的错误就像
{ "errors": [ { "message": "Must be input type" } ]}
Run Code Online (Sandbox Code Playgroud)
我正在使用邮递员测试我的查询,我的变异查询是
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}}
Run Code Online (Sandbox Code Playgroud)
这是我的事件架构:
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.',
}
})
});
Run Code Online (Sandbox Code Playgroud)
猫鼬模式:
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
}
}]
})
Run Code Online (Sandbox Code Playgroud)
突变类型:
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);
});
})
});
}
}
Run Code Online (Sandbox Code Playgroud)
adr*_*ine 22
您的问题是,当您定义突变时,所有类型都必须是输入类型,因此您会得到错误"Must be input type"
.所以在这里(来自你的变异):
media:{
type:new GraphQLList(mediaType),
description:'List of media',
},
location:{
type:new GraphQLList(locationType),
description:'List of media',
},
Run Code Online (Sandbox Code Playgroud)
GraphQLList
,mediaType
并且locationType
必须是输入类型.
GraphQLList
已经是输入类型(请参阅https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82以查看被视为输入类型的GraphQL类型列表).
但是你的类型mediaType
和locationType
是的GraphQLObjectType
类型,这是不是一种输入类型,但如果你看看输入类型列表中再次:https://github.com/graphql/graphql-js/blob/master/src/type/definition. JS#L74-L82,你会发现GraphQLInputObjectType
这是一个对象输入类型,因此,你需要做的是更换mediaType
并locationType
通过他们的"输入"的版本.
我的建议做的是创造mediaInputType
和locationInputType
这将有相同的字段结构mediaType
和locationType
,但与创造new GraphQLInputObjectType({...
,而不是new GraphQLObjectType({...
在你的突变使用它们.
我遇到了同样的问题,我就这样解决了,如果你有任何问题,请随时发表评论.
我遇到了同样的问题 - 我不知道如何在输入定义中指定对象数组。因此,对于那些想要查看“文本”模式解决方案的人:
type Book {
title: String!
}
Run Code Online (Sandbox Code Playgroud)
在您的输入类型中有一个 Books 数组
input AuthorInput {
name: String!
age: Int!
}
Run Code Online (Sandbox Code Playgroud)
您不能只books: [Book!]
在 input 语句中添加,您需要故意创建包含所需字段的输入类型(如果您愿意,可以复制):
input BookInput {
title: String!
}
Run Code Online (Sandbox Code Playgroud)
然后你可以:
input AuthorInput {
name: String!
age: Int!
books: [BookInput!]
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14386 次 |
最近记录: |