[英]GraphQL mutation create document which is array of object
我正在使用 GraphQL 制作服务器,这是我的架构:
const PostType = new GraphQLObjectType({
name: 'Post',
fields: () => ({
id: {
type: GraphQLID,
},
caption: {type: GraphQLString},
createdAt: {type: GraphQLString},
likeCount: {type: GraphQLInt},
displayName: {type: GraphQLString},
likedList: {
type: new GraphQLList(GraphQLString),
},
uid: {
type: GraphQLID,
},
uploadedMediaList: {
type: new GraphQLList(MediaListType),
resolve(parent, args) {
return _.filter(mediaListDB, {postId: parent.id});
},
},
}),
});
const MediaListType = new GraphQLObjectType({
name: 'MediaList',
fields: () => ({
id: {
type: GraphQLID,
},
mediaUrl: {type: GraphQLString},
type: {type: GraphQLString},
postId: {type: GraphQLID},
}),
});
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
createPost: {
type: PostType,
args: {
id: {type: GraphQLID},
caption: {type: new GraphQLNonNull(GraphQLString)},
createdAt: {type: new GraphQLNonNull(GraphQLString)},
likeCount: {type: new GraphQLNonNull(GraphQLInt)},
displayName: {type: new GraphQLNonNull(GraphQLString)},
likedList: {type: new GraphQLNonNull(new GraphQLList(GraphQLString))},
uid: {type: new GraphQLNonNull(GraphQLString)},
uploadedMediaList: {
type: new GraphQLNonNull(new GraphQLList(MediaListType)),
},
},
resolve(parent, args) {
const {
id,
caption,
createdAt,
likeCount,
displayName,
likedList,
uid,
uploadedMediaList,
} = args;
return postDB.unshift({
id,
caption,
createdAt,
likeCount,
displayName,
likedList,
uid,
uploadedMediaList,
});
},
},
},
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
post: {
type: PostType,
args: {
id: {type: GraphQLID},
},
resolve(parent, args) {
return _.find(postDB, {id: args.id});
},
},
posts: {
type: new GraphQLList(PostType),
resolve(parent, args) {
return postDB;
},
},
mediaList: {
type: MediaListType,
args: {
id: {type: GraphQLID},
},
resolve(parent, args) {
return _.find(mediaListDB, {id: args.id});
},
},
mediaLists: {
type: new GraphQLList(MediaListType),
resolve(parent, args) {
return mediaListDB;
},
},
},
});
module.exports = new GraphQLSchema({
query: RootQuery,
mutation: Mutation
});
如您所见,模式PostType
有一个属性是MediaListType
项的数组,每个项都是 object。 现在在Mutation
中,我得到了一个查询createPost
并定义了uploadedMediaList
是GraphQLList
的MediaList
,我希望我的查询像这样
mutation {
createPost(id: "101", createdAt: "1 25 13/03/2021", caption: "Hope it work", likeCount: 1000, displayName: "Connor McKnight", likedList: ["Nguoi thu nhat", "Ai do nua"], uid: "5y7325y4732", uploadedMediaList: [{id: "99", mediaUrl: "fdsnjakfndjksanfdjksanfjkdsanfkdsa", type: "dummy type", postId: "101"}]) {
likeCount
id
createdAt
}
}
这是我得到的错误:
{
"errors": [
{
"message": "The type of Mutation.createPost(uploadedMediaList:) must be Input Type but got: [MediaList]!."
}
]
}
请帮我解决它,非常感谢你,祝你有美好的一天
在您的代码中, MediaList
属于 object 类型,因此不能用作输入。 为了接收它作为输入,您需要创建另一个 GraphQL 类型:
const MediaListInputType = new GraphQLInputObjectType({
name: 'MediaListInput',
fields: () => ({
id: {
type: GraphQLID,
},
mediaUrl: {type: GraphQLString},
type: {type: GraphQLString},
postId: {type: GraphQLID},
}),
});
然后你需要改变你的突变来使用这种类型:
// ...rest of the code
uploadedMediaList: {
type: new GraphQLNonNull(new GraphQLList(MediaListInputType)),
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.