我正在使用express和apollo-express以及mongodb(mongoose)制作博客服务 .
我做了一些变异查询,但是我没有成功获得变异查询的args .
现在我要问我应该如何构建我的变异查询以使事情有效 . 谢谢 .
错误:
“消息”:“博客验证失败: Headers :路径 Headers 是必需的 . ,slug:路径slug是必需的 . ”
查询:
mutation ($input: BlogInput) {
newBlog(input: $input) {
title
slug
}
}
查询变量:
{
"input": {
"title": "ABC",
"slug": "abc"
}
}
我的graphql架构的一部分:
type Blog {
id: ID!
title: String!
slug: String!
description: String
users: [User]!
posts: [Post]!
}
input BlogInput {
title: String!
slug: String!
description: String
}
extend type Mutation {
newBlog(input: BlogInput): Blog
}
我的解析器的一部分:
import Blog from './blog.model'
export const blogs = async () => {
const data = await Blog.find().exec()
return data
}
export const newBlog = async (_, args) => {
const data = await Blog.create({ title: args.title, slug: args.slug })
return data
}
我的数据库架构(mongoose)的一部分:
import mongoose from 'mongoose'
const Schema = mongoose.Schema
const blogSchema = Schema({
title: {
type: String,
required: true
},
slug: {
type: String,
required: true,
unique: true
},
description: {
type: String
},
users: {
type: [Schema.Types.ObjectId],
ref: 'User'
},
posts: {
type: [Schema.Types.ObjectId],
ref: 'Post'
}
})
export default mongoose.model('Blog', blogSchema)
1 回答
您已将
newBlog
变异定义为接受名为input
的单个参数 . 据我所知,你正确地使用变量将该参数传递给变异 . 您的解析器会收到传递给正在解析的字段的参数的映射 . 这意味着您可以像这样访问input
对象的各个属性:注意,您可能希望使
input
不可为空(即将类型设置为BlogInput!
),否则您的解析器将需要处理args.input
返回undefined的可能性 .