dillonkearns / elm-graphql

Autogenerate type-safe GraphQL queries in Elm.

Home Page:https://package.elm-lang.org/packages/dillonkearns/elm-graphql/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for multiple queries/mutations in a single request

yonigibbs opened this issue · comments

In GraphQL it is possible for one HTTP request to contain multiple queries and/or mutations (for example see here, here and here). A nice addition to elm-graphql would be the ability to do this, i.e. build up a single request which contains multiple queries and/or mutations.

Maybe I'm misunderstanding, but I think you're talking about this:

type TwoQueryResultStruct =
  { firstResult : Something
  , secondResult : SomethingElse
  }

multipleQueries : SelectionSet TwoQueryResultStruct Graphql.Operation.RootQuery
multipleQueries =
    SelectionSet.map2 TwoQueryResultStruct
        (Api.Query.firstSelection params)
        (Api.Query.secondSelection params)

-- Pseudocode, I can't remember the `send` function call off the top of my head. But it's just the regular one, nothing special.
sendQuery = Graphql.Http.sendQuery multipleQueries MsgType

Ah, interesting, thanks @Jayshua. This issue came from a discussion on Slack with @dillonkearns (https://elmlang.slack.com/archives/C0RSQNQ92/p1598619526056600). It's quite possible I've misunderstood as well, but I thought there Dillon said it wasn't possible to do this. Let's wait for the man himself to confirm :-)

Oh, I see. You're talking about this:

query QueryOne {
  a: personById(3) { firstName }
  b: personById(4) { firstName }
}

query AnotherQuery {
  c: personById(5) { firstName }
}

The code I gave can produce the first top-level query, (which does have multiple queries on the root object, something I do quite often to run multiple queries in a single request) but not append the second in that way to the entire document.

From https://graphql.org/graphql-js/graphql/#graphql

operationName, which allows the caller to specify which operation in requestString will be run, in cases where requestString contains multiple top-level operations.

From this I gather operationName is used to send a single document to the server containing lots of different queries, and choose which of those you want to execute using the operationName parameter. Seems you can only choose one, and the other queries are ignored. So running multiple queries in this way still requires multiple HTTP request.

I'm going to close this issue coz it sounds like what I personally would need to do already can be done, as you explain above. Thanks @Jayshua! There was obviously some confusion somewhere. I suspect it's to do with terminology: when I was asking about "multiple queries" I meant your first example:

query QueryOne {
  a: personById(3) { firstName }
  b: personById(4) { firstName }
}

I guess when Dillon said on Slack that "multiple queries wasn't supported" he was talking about your second example:

query QueryOne {
  a: personById(3) { firstName }
  b: personById(4) { firstName }
}

query AnotherQuery {
  c: personById(5) { firstName }
}

There was someone else involved in the Slack chat also looking for "multiple query support" so I tried to contact them to see what exactly they meant by "multiple queries", but couldn't get an answer.

The terminology seems a bit confusing because at https://graphql.org/learn/queries/#multiple-fields-in-mutations this is referred to, for example, as "Multiple fields in mutations/queries", whereas at https://hasura.io/docs/1.0/graphql/core/queries/multiple-queries.html#run-multiple-top-level-queries-in-the-same-request they talk about "Multiple top-level queries in same request" (when what they mean is a single query JSON field, with multiple top-level fields in it, each of which is conceptually a "query" against some data source).