graphql-request icon indicating copy to clipboard operation
graphql-request copied to clipboard

Destructuring mutation response

Open tettoffensive opened this issue 5 years ago • 1 comments

With GraphQL mutations, the data object which comes back is then wrapped in the name of my mutation createItemInDatabase.


client:

const mutation = (`
    mutation createNewItem($title: String!) {
       createItemInDatabase(title: $title) {
           name
           description
       }
   }
`);
request(url, mutation).then(({ createItemInDatabase }) => console.log(createItemInDatabase));

apollo-micro-server:

type Mutation {
    createItemInDatabase(
      title: String!
    ): Item
}

Is there someway to get the name of the mutation in javascript rather than relying on matching what is essentially part of the substring?

(I'm using 2.0.0 btw)

tettoffensive avatar Aug 13 '20 15:08 tettoffensive

Hi @tettoffensive I'm not part of the graphql-request team, but I believe the behaviour you're seeing is part of the GraphQL spec and not necessarily specific to graphql-request.

jperasmus avatar Aug 17 '20 08:08 jperasmus

Closing this, as this is expected behavior. The data structure is supposed to match the query you provide.

jonkoops avatar Nov 05 '23 10:11 jonkoops