【问题标题】:How to construct apollo client mutation with custom input and variables如何使用自定义输入和变量构建 apollo 客户端突变
【发布时间】:2019-08-09 08:47:12
【问题描述】:

我有适用于 graphiQL 的突变:

mutation ADDRELEASE{
  createRelease(release: {
    title: "Release Title"
    releaseType: "Album"

  }) {
    title
    id
  }
} 

我正在尝试将 apollo 客户端graphql-tag 一起使用,并像这样设置突变变量:

 const CREATE_RELEASE = gql`
  mutation($release: {
    $title: String 
    $releaseType: String
  }){
    createRelease(release: {
      title: $title 
      releaseType: $releaseType

    }){
    id
   }
  }
`

我想这是语法问题。无法弄清楚如何使它工作。

【问题讨论】:

    标签: javascript graphql react-apollo apollo-client graphql-js


    【解决方案1】:

    如果您只打算使用$title$releaseType,则无需为$release 其他两个变量定义一个变量。你可以这样做:

    # Replace ReleaseInput with the appropriate type based on the schema
    mutation($release: ReleaseInput) {
      createRelease(release: $release) {
        id
      }
    }
    

    或者...

    mutation(
      $title: String 
      $releaseType: String
    ) {
      createRelease(release: {
        title: $title 
        releaseType: $releaseType
      }) {
        id
      }
    }
    

    不管怎样,您还可以在 GraphiQL 中使用变量测试查询。您可以点击页面左下角的QUERY VARIABLES打开变量编辑器。

    【讨论】:

      猜你喜欢
      • 2018-10-15
      • 2019-11-11
      • 2017-09-06
      • 2017-05-01
      • 2021-09-03
      • 2018-12-19
      • 2021-04-13
      • 2017-09-14
      • 2018-09-03
      相关资源
      最近更新 更多