AnnaSm
AnnaSm

Reputation: 2300

Given the following mutation, how to return back data?

I have the following mutation

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

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

In my form onSubmit, I am using the above like so:

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

How can I update the above so I get values back like so:

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

Where result would contain user.id, user.title, user.photo_url?

Thanks

Upvotes: 0

Views: 213

Answers (1)

Locco0_0
Locco0_0

Reputation: 3500

The result of a mutation is defined within curly brackets in the mutation string. I your case it could look like:

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

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

When you call the mutation the result can be retrieved like so:

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

In this case the mutation itself on the server needs to deliver the correct data objects. So you also need to change the mutation on the server.

Upvotes: 1

Related Questions