Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take result from one query / mutation and pipe to another

Tags:

graphql

I'm wondering if there's a way in GraphiQL that will let me pipe the result from one query / mutation into another query / mutation. Here's an example where the login mutation is used to get a viewer and the viewer would be used to query the addresses for that user. Is this possible with GraphQL / GraphiQL.

mutation {
  login(credentials: {
    email: "[email protected]",
    password: "password123",
    passwordConfirmation: "password123"
  }) {
    viewer
  }
}

query {
  addresses(viewer:viewer) {
    city
  }
}
like image 501
ThomasReggi Avatar asked Aug 11 '16 19:08

ThomasReggi


1 Answers

The purpose of the selection set in the mutations is to be able to fetch data that has changed as a result of the mutation. But it also makes it possible to fetch related data, as long as you can access is through the mutation result type.

Let's assume we have following types:

type Address {
  city: String
}
type User {
  addresses: [Address]
}

If the result (payload) type of the login mutation includes a field viewer of type User that refers to the successfully logged in user, you can query any field of the User in the result of the mutation:

mutation {
  login(credentials: {
    email: "[email protected]",
    password: "password123",
    passwordConfirmation: "password123"
  }) {
    viewer {
      addresses {
        city
      }
    }
  }
}
like image 175
fson Avatar answered Nov 30 '22 06:11

fson