【问题标题】:GraphQL mutation, parameters / variables objects (with dot notation)GraphQL 突变、参数/变量对象(带点符号)
【发布时间】:2018-07-14 13:08:24
【问题描述】:

我有一个这样的 Graphql 突变:

gql 查询

mutation UpdateMother($mother_id: ID!, $child_name: String!) {
  updateMother(
    mother: {
      id: $id
      child: {
        name: $child_name
      }
    }
  )
}

效果很好,但我需要使用$child.name 而不是$child_name。这可能吗?

【问题讨论】:

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


    【解决方案1】:

    你应该可以这样称呼这个突变

    mutate({
      variables: {
        mother_id: mother.id,
        child_name: child.name
      }
    })
      .then(() => {})
      .catch(() => {})
    

    另请注意,您将 $mother_id 定义为输入,并在第 4 行使用 $id。你应该使用类似的东西:

    mutation UpdateMother($mother_id: ID!, $child_name: String!) {
      updateMother(
        mother: {
          id: $mother_id
          child: {
            name: $child_name
          }
        }
      )
    }
    

    【讨论】:

    • 这就是我在变量中所做的。我想知道是否有一种方法可以直接在突变查询中而不是在变量中处理这个问题。我正在考虑表格,这样我可以使用:...data only。
    • 您可以做到这一点的唯一方法是,如果您的 data 变量由具有 .gql 中确切变量名称的键组成。在这种情况下,您必须拥有 data = { mother_id: '1', child_name: 2 } 才能使用 mutate({ variables: { ...data }})
    猜你喜欢
    • 2017-02-07
    • 2017-03-09
    • 2020-05-18
    • 2020-05-23
    • 2018-07-02
    • 2020-04-05
    • 2011-10-05
    • 2020-02-03
    • 2017-08-22
    相关资源
    最近更新 更多