13

I have an operation getFoo that requires that the user is authenticated in order to access the resource.

User authenticates using a mutation authenticate, e.g.

mutation {
  authenticate (email: "foo", password: "bar") {
    id
  }
}

When user is authenticated, two things happen:

  1. The request context is enriched with the authentication details
  2. A cookie is created

However, I would like to combine authentication and getFoo method invocation into a single request, e.g.

mutation {
  authenticate (email: "foo", password: "bar") {
    id
  }
}
query  {
  getFoo {
    id
  }
}

The latter produces a syntax error.

Is there a way to combine a mutation with a query?

Gajus
  • 55,791
  • 58
  • 236
  • 384

1 Answers1

10

There's no way to send a mutation and a query in one request according to the GraphQL specification.

However, you can add any fields to the mutation payload. So if there are only a handful of queries that you need to support for the authenticate mutation, you could to this, for example:

mutation {
  authenticate (email: "foo", password: "bar") {
    id
    getFoo {
      id
    }
  }
}

At the end of the day, it might be better to keep the mutation and query separate though. It gets hairy very quickly if you want to include many queries in many mutations like this. I don't see a problem with the overhead of an additional request here.

marktani
  • 6,520
  • 5
  • 33
  • 59