最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

定义在graphql瑜伽的突变说法

IT培训 admin 6浏览 0评论

定义在graphql瑜伽的突变说法

如何创建与在graphql-yoga定义为解析参数的突变:

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 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
          }
        ]
      }
    ]
  }
}
回答如下:

根据您的类型定义:

  1. createProject突变不希望任何参数:
type Mutation {
  createProject: ProjectMutationResult
}
  1. ProjectMutationResult类型不具有id场也不是name领域:
type ProjectMutationResult implements MutationResult {
  statusCode: Int
  message: String
  project: Project
}

所以,当你运行突变:

mutation CreateProject($name: String!) {
  createProject(name: $name) {
    id
    name
  }
}

你有你喂你GraphQL服务器,什么它实际上是期待什么之间的差异齐全。

所以首先,如果你希望能够到name设置为当你创建你的项目,你需要你的createProject定义修订本:

type Mutation {
  createProject(name: String!): ProjectMutationResult
}

(如果你想的命名是可选的,设置名称为类型String而非String!的)

然后,假设你想要从你的突变新创建的项目编号和名称,更改突变本身:

mutation CreateProject($name: String!) {
  createProject(name: $name) {
    project {
      id
      name
    }
  }
}

你需要做的,是因为你的createProject突变返回它本身包含类型ProjectMutationResult,这是一个定义projectProject领域的id领域name

定义在graphql瑜伽的突变说法

如何创建与在graphql-yoga定义为解析参数的突变:

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 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
          }
        ]
      }
    ]
  }
}
回答如下:

根据您的类型定义:

  1. createProject突变不希望任何参数:
type Mutation {
  createProject: ProjectMutationResult
}
  1. ProjectMutationResult类型不具有id场也不是name领域:
type ProjectMutationResult implements MutationResult {
  statusCode: Int
  message: String
  project: Project
}

所以,当你运行突变:

mutation CreateProject($name: String!) {
  createProject(name: $name) {
    id
    name
  }
}

你有你喂你GraphQL服务器,什么它实际上是期待什么之间的差异齐全。

所以首先,如果你希望能够到name设置为当你创建你的项目,你需要你的createProject定义修订本:

type Mutation {
  createProject(name: String!): ProjectMutationResult
}

(如果你想的命名是可选的,设置名称为类型String而非String!的)

然后,假设你想要从你的突变新创建的项目编号和名称,更改突变本身:

mutation CreateProject($name: String!) {
  createProject(name: $name) {
    project {
      id
      name
    }
  }
}

你需要做的,是因为你的createProject突变返回它本身包含类型ProjectMutationResult,这是一个定义projectProject领域的id领域name

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论