graphql - 在突变中使用查询 - 创建嵌套对象

Mar*_*aud 5 nested mutation graphql graphql-js

我有一个非常简单的模型post,嵌入了几个comments

我想知道我应该怎么做一个突变来添加一个新commentpost

由于我已经定义了一个查询以获取post给定的查询id,我想尝试使用以下变异语法

mutation {
  post(id: "57334cdcb6d7fb172d4680bb") {
    addComment(data: {
      text: "test comment"
    })
  }
}
Run Code Online (Sandbox Code Playgroud)

但我似乎无法找到一种方法让它发挥作用.即使我处于变异状态,输出类型是一个postaddComment也被认为是一个字段post应该有的.

你们有什么想法吗?

谢谢

小智 0

我可能是错的,但你可能只想创建一个单独的updatePost突变,接受帖子 id 作为参数

type Post {
   comments: [ Comment ]
}

input PostInput {
   comments: [ CommentInput ]
}

type Mutation {
   updatePost( id: ID!, input: PostInput ): Post
}
Run Code Online (Sandbox Code Playgroud)

这里的 updatePost 突变将帖子的 id 和更新后的帖子对象作为参数,并返回 Post 类型。所以我会像这样使用它:

mutation {
  updatePost( id: '1234', input: { comments: []) {
    id
  }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!