在 Mutation 中使用 GraphQL Args 属性
Using The GraphQL Args Property In A Mutation
我正在使用 express 和 apollo-express 以及 mongodb (mongoose) 制作博客服务。
我进行了一些变异查询,但没有成功获取变异查询的参数。
现在我要问我应该如何构建我的突变查询才能使它正常工作。谢谢。
错误:
"message": "Blog validation failed: title: Path title
is required., slug: Path slug
is required."
查询:
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
}
我的数据库模式的一部分(猫鼬):
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)
您已将 newBlog
突变定义为接受名为 input
的单个参数。据我所知,您正在使用变量将该参数正确传递给突变。您的解析器收到传递给正在解析的字段的参数映射。这意味着您可以像这样访问 input
对象的各个属性:
export const newBlog = async (_, args) => {
const data = await Blog.create({ title: args.input.title, slug: args.input.slug })
return data
}
请注意,您可能希望 input
不可为 null(即将类型设置为 BlogInput!
),否则您的解析器将需要处理 args.input
返回未定义的可能性.
我正在使用 express 和 apollo-express 以及 mongodb (mongoose) 制作博客服务。
我进行了一些变异查询,但没有成功获取变异查询的参数。
现在我要问我应该如何构建我的突变查询才能使它正常工作。谢谢。
错误:
"message": "Blog validation failed: title: Path
title
is required., slug: Pathslug
is required."
查询:
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
}
我的数据库模式的一部分(猫鼬):
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)
您已将 newBlog
突变定义为接受名为 input
的单个参数。据我所知,您正在使用变量将该参数正确传递给突变。您的解析器收到传递给正在解析的字段的参数映射。这意味着您可以像这样访问 input
对象的各个属性:
export const newBlog = async (_, args) => {
const data = await Blog.create({ title: args.input.title, slug: args.input.slug })
return data
}
请注意,您可能希望 input
不可为 null(即将类型设置为 BlogInput!
),否则您的解析器将需要处理 args.input
返回未定义的可能性.