【发布时间】:2019-02-10 21:22:02
【问题描述】:
如何为 graphql-yoga 中定义的解析器创建带有参数的 Mutation:
const resolvers =
Mutation: {
createProject(root, args) {
const id = (Number(last(data.projects).id) + 1).toString()
const newProject = { ...args, id: id }
...
我尝试了以下方法:
mutation CreateProject($name: String!) {
createProject {
data: {
name: $name
}
}
}
和
mutation CreateProject($name: String!) {
createProject($name: name) {
statusCode
}
}
产生
以及其他各种结构均未成功。
在项目 README 或三个示例中的任何一个中似乎都没有提到 Mutation。
更新
我现在正在使用:
mutation CreateProject($name: String!) {
createProject(name: $name) {
id
name
}
}
这与我在网上看到的示例非常相似,我认为它必须是有效的并且语法没有被拒绝。
架构定义为:
scalar ID
type Project {
id: ID
type: ProjectType
name: String
}
interface MutationResult {
statusCode: Int
message: String
}
type ProjectMutationResult implements MutationResult {
statusCode: Int
message: String
project: Project
}
type Mutation {
createProject: ProjectMutationResult
}
但是在提交突变时,我收到:
{
"error": {
"errors": [
{
"message": "Unknown argument \"name\" on field \"createProject\" of type \"Mutation\".",
"locations": [
{
"line": 2,
"column": 17
}
]
},
{
"message": "Cannot query field \"id\" on type \"ProjectMutationResult\".",
"locations": [
{
"line": 3,
"column": 5
}
]
},
{
"message": "Cannot query field \"name\" on type \"ProjectMutationResult\".",
"locations": [
{
"line": 4,
"column": 5
}
]
}
]
}
}
【问题讨论】:
-
如果您看到错误,请将其包含在您的问题中。这不仅有助于解决您的问题,还可以帮助其他人在搜索时找到您的问题。
-
由于语法错误,图形页面不允许我提交查询。我会看看我是否可以捕获它产生的工具提示。