鉴于以下突变,如何返回数据?

问题描述 投票:0回答:1

我有以下突变

const SET_NAME_MUTATION = gql`
  mutation ($name: String!) {
    setName(name: $name)
  }
`;

const withSetFullNameMutation = graphql( SET_NAME_MUTATION, {
  name: 'setName',
});

在我的表单onSubmit中,我正如上所述:

await setName({ variables: "data is here" });

我怎样才能更新上面所以我得到的值如下:

const result = setName({ variables:"data is here });
console.log(result)

结果将包含user.id,user.title,user.photo_url?

谢谢

graphql apollo mutation
1个回答
1
投票

突变的结果在突变字符串的花括号内定义。我的情况看起来像:

const SET_NAME_MUTATION = gql `
  mutation ($name: String!) {
    setName(name: $name) {
      user {
        id
        title
        photo_url
      }
    }
  }
`;

const withSetFullNameMutation = graphql(SET_NAME_MUTATION, {
  name: 'setName',
});

当你调用变异时,可以像这样检索结果:

this.props.setName(name).then(response => {
  const user = response.data.setName.user;
}).catch(errors => {});

在这种情况下,服务器上的突变本身需要提供正确的数据对象。所以你还需要改变服务器上的变异。

© www.soinside.com 2019 - 2024. All rights reserved.