15
votes

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
  }
}
1
There's an experimental batchOperations feature but I don't think it's landed in graphql-js yet. I opened a issue here. github.com/graphql/graphql-js/issues/462ThomasReggi

1 Answers

2
votes

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
      }
    }
  }
}