【问题标题】:Ignore results from mutation query using apollo client?使用阿波罗客户端忽略突变查询的结果?
【发布时间】:2019-02-09 14:49:43
【问题描述】:

我想使用 graphql 在服务器上触发 addLicenseCodeOneTimeUsage 操作。我不知道服务器会响应什么,我也不需要任何响应。如您所见,我在addLicenseCodeOneTimeUsage{} 中没有列出任何属性。

const client = new ApolloClient({
    link: link,
    cache: new InMemoryCache(),
});

yield client.mutate({
    /* tslint:disable */
    mutation: gql`
        }
        mutation licensemutation($distributor: String!, $licenceType: String!, $duration: String!, $userId: String!) {
            addLicenseCodeOneTimeUsage(distributor: $distributor, licenseType: $licenseType, duration: $duration, userId: $userId) {
            }
        }
    `,
    /* tslint:enable */
    variables: {
        userId: username,
        distributor: distributor,
        licenseType: licenseType,
        duration: duration,
    },
});
  • 到目前为止,我所写的内容是否合理,或者我对 graphql 的工作原理或应如何使用有明显的误解?
  • 在 graphql 字符串中,我没有定义要接收什么信息。该语法是否有效,还是我需要明确声明我要忽略结果?

【问题讨论】:

    标签: graphql apollo-client


    【解决方案1】:

    突变的返回类型必须定义至少一个字段,并且您必须在查询中从中选择至少一个字段。 (见§2.3 of the spec。)

    如果您同时控制服务器和客户端并且突变确实没有返回任何内容,您可以定义一个人为的ok: Boolean!,这总是正确的。您也可能返回包含权利列表的用户对象之类的内容,在这种情况下,您只需选择(并忽略)用户 ID。

    【讨论】:

      【解决方案2】:

      我认为这可能是ignoreResults 的用途。

      ignoreResults - 如果为真,返回的数据属性将不会随着突变结果而更新。

      所以突变(使用此选项)看起来像:

      client.mutate({
          mutation: MutationDocument
          variables: {
              userId: username,
              distributor: distributor,
              licenseType: licenseType,
              duration: duration,
          },
          ignoreResults: true
      });
      

      【讨论】:

        猜你喜欢
        • 2018-09-12
        • 2020-07-26
        • 2019-04-19
        • 2021-01-19
        • 2018-01-13
        • 2020-10-28
        • 2019-04-02
        • 2017-03-23
        • 2017-07-21
        相关资源
        最近更新 更多