Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Request: Batch Client Query #428

Open
verzil opened this issue Apr 13, 2018 · 3 comments
Open

Request: Batch Client Query #428

verzil opened this issue Apr 13, 2018 · 3 comments

Comments

@verzil
Copy link

verzil commented Apr 13, 2018

I am just wondering are we going to have a service that supports batching on the transportation layer? Like how apollo server does. Or do we have to batch on the query level.

@danielrearden
Copy link
Collaborator

For context, this is how Apollo Server does it. The request body can be an array of objects, in which case they are effectively processed as individual requests and then aggregated and sent back as an array of response objects.

@AndyHubert
Copy link

AndyHubert commented Sep 6, 2021

Here is a simple solution I wrote up until something official is available. Works so far, though I cannot promise I won't find any issues.

const runGraphql = graphqlHTTP({
  schema,
  rootValue,
})

app.use('/graphql', async (req, res, next) => {

  // handle batch queries
  if(req.body instanceof Array) {
    res.origSend = res.send
    const origReqBody = req.body
    const responseBodies = []
    for(let graphqlQuery of origReqBody) {
      await new Promise(resolve => {
        res.send = body => {
          responseBodies.push(body)
          resolve()
        }
        req.body = graphqlQuery
        runGraphql(req, res, next)
      })
    }
    res.origSend(`[${responseBodies.join(',')}]`)
    return
  }

  runGraphql(req, res, next)

})

@enisdenjo
Copy link
Member

This library has been deprecated and this repo will be archived soon. It has been superseded by graphql-http.

Furthermore, if you seek a fully-featured, well-maintained and performant server - I heavily recommend GraphQL Yoga!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants