0
votes

Right now, after a successful mutation I send another request for the updated data. I'd like to eliminate this second request.

According to the Apollo docs, if I implement object IDs I can get cache updates for free. This relies on the graphql server returning a response (with the updated data) that can be normalized by Apollo.

Can the mutation response only include some of the fields for my object? For example I have a Hero object that defines a Hero's name and age. But the UI only allows the user to change the name. Can I return just the Hero's name in the response to leave the age untouched?

1

1 Answers

0
votes

Yes, you can query any field you want from the result of mutation in the same way as for query. You just have to return your object after updating it.

For example:

mutation {
  updateHero(newHero: { name: "John Doe" }) {
    name
  }
}

One more thing. Although it is recommended to return the updated object from the mutation, on the other side, it is also recommended to design your GraphQL server as per your UI needs. So if you think you won't need the whole object after updating, you can always return only name.

In this schema your schema will look like the following:

Mutation {
  updateHero(newHero: UpdateHeroInput!): String!
}

And the corresponding query will be:

mutation {
  updateHero(newHero: { name: "John Doe" })
}

Note, there's no selection of fields as it returns a single scalar value (String).