Graphql-js: "Must provide operation name if query contains multiple operations"

Created on 7 Dec 2015  路  7Comments  路  Source: graphql/graphql-js

Hi. I am trying to put a mutation and a query inside one request. I have found an example from express-graphql test . However, when I implement this in my code, I got error: "Must provide operation name if query contains multiple operations"

My POST request's body:

mutation login {
  createSession(email:"[email protected]", password: "foobar"){
    session
  }
}

query me {
  deserializeUser{
    id,
    email,
    name
  }
}

And I have set up schema RootQuery and RootMutation with fields - createSession and deserializeUser already. The session is passed to deserializeUser via req.

Please give me some clues. Thanks in advance.

Most helpful comment

I'm trying to separate HTTP from graphql when I say 'executions'. Since this is the graphql-js issue list I want to avoid talking about any specific HTTP library :).

So by executions I mean the calling of the graphql function. e.g. graphql(schema, query, root), etc.

If your query has multiple operations and you want to evaluate all of them you'd have to call the graphql function multiple times -- once per operation name.

  • graphql(schema, query, root, vars, 'login')
  • graphql(schema, query, root, vars, 'me')

You would now have two responses.

The test on express-graphql is an example of having multiple operations -- TestMutation and TestQuery -- and specifying which one you want to execute via operation name. In the example the operation name is 'TestQuery' so it is the one that executes. You could remove TestMutation from that query entirely and it would be the same result.

I imagine most HTTP libraries map a request to a single graphql execution so in order to execute both operations it would require multiple requests. This is not a requirement though and there is nothing preventing an HTTP library from accepting an array of queries per request -- graphql doesn't care.

All 7 comments

Operation name is the fifth argument to the graphql function. Only one operation will execute at a time but its possible (like your example) to have multiple operations per document. So you could supply this query to graphql and specify 'me' or 'login' as the operation name.

What I want is a bash with a mix of mutation and query within one http request. I still can't find any document that shows me how to do this properly. I'm using koa-graphql (or 'graphqlHttp'). Would you please give some examples or a link to documentation please

Just guessing but your use case can probably be accomplished by having the mutation include the data it mutates as a fields:

mutation login {
  createSession(email: "[email protected]", password: "foobar") {
    user {
      id
      email
      name
    }
  }
}

If you want to perform a mutation and query from the same document they'd be two separate executions each with their own response.

I would still want to discuss further about 'perform a mutation and query from the same document'. Do we have documentation for this?

When you say "they'd be two separate executions each with their own response", do you mean this one HTTP request will generate two JSON responses to the client asynchronously?

And in express-graphql test's line 299 ~ 300,

  • why do we set operationName to query's name, but not mutation's?
  • how does the mechanism of "they'd be two separate executions each with their own response" affect the output of highlighted document? From the test, the output looks similar to usual response:
        expect(JSON.parse(response.text)).to.deep.equal({
          data: {
            test: 'Hello World'
          }
        });

Thanks =]

I'm trying to separate HTTP from graphql when I say 'executions'. Since this is the graphql-js issue list I want to avoid talking about any specific HTTP library :).

So by executions I mean the calling of the graphql function. e.g. graphql(schema, query, root), etc.

If your query has multiple operations and you want to evaluate all of them you'd have to call the graphql function multiple times -- once per operation name.

  • graphql(schema, query, root, vars, 'login')
  • graphql(schema, query, root, vars, 'me')

You would now have two responses.

The test on express-graphql is an example of having multiple operations -- TestMutation and TestQuery -- and specifying which one you want to execute via operation name. In the example the operation name is 'TestQuery' so it is the one that executes. You could remove TestMutation from that query entirely and it would be the same result.

I imagine most HTTP libraries map a request to a single graphql execution so in order to execute both operations it would require multiple requests. This is not a requirement though and there is nothing preventing an HTTP library from accepting an array of queries per request -- graphql doesn't care.

Thanks for the awesome explanation Huey! I realise myself am going quite off-topic here. I will probably shoot more questions in express-graphql instead.

@ivawzh did you find a solution for this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

closertb picture closertb  路  3Comments

sudheerj picture sudheerj  路  3Comments

gjuchault picture gjuchault  路  4Comments

ablbol picture ablbol  路  4Comments

itajaja picture itajaja  路  3Comments