graphql-js icon indicating copy to clipboard operation
graphql-js copied to clipboard

Feature request: batchOperations

Open reggi opened this issue 8 years ago • 14 comments

From Lee's talk at React Eurpoe batch was mentioned as an experimental feature.

screen shot 2016-08-11 at 4 21 30 pm

I'm curious when / if it's ever going to land in this project or if there's a plugin available today.

reggi avatar Aug 11 '16 20:08 reggi

Sounds like all of this stuff would be implemented very easily in the web server itself, except for the @export directive. Although, you could relatively easily use the tools in graphql-js to traverse the AST after your get the result to extract the relevant fields.

So this is something someone could do as a plugin for either express-graphql or apollo-server with a little bit of work, to test it out - I'd say that would be the right way to start rather than adding it to the core implementation here right away.

If you're interested in working on this, I can help you find the right tools to implement it.

stubailo avatar Aug 11 '16 22:08 stubailo

@reggi Apollo Server already has a way to send a batch of queries, the only thing it's missing is the @export directive. If you're interested in doing some work in the direction of adding that feature, I'd be more than happy to help you with it!

helfer avatar Aug 12 '16 00:08 helfer

This style of dependent batched query isn't supported yet and is not yet a priority relative to some of the other work happening now.

I'm happy to leave this issue open for future consideration though.

leebyron avatar Aug 16 '16 06:08 leebyron

Thanks All! I'm excited for this feature, but It's not something I need right now. Would be super nice for testing.

reggi avatar Aug 16 '16 21:08 reggi

Would it be feasible to request

feed {
  stories {
    id
    actor
    message
  }
}

and

feed {
  stories {
    id
    comments {
      actor
      message
    }
  }
}

in parallel and then merge client-side based on story id? The server could cache the work done for feed { stories { id } } briefly and reuse it in the other request. It would be two parallel requests so we wouldn't have to wait for two full round-trips. With HTTP/2 it's also only one connection.

Just a quick thought - felt it's worthwhile to think about a solution that does not require API/language changes.

morris avatar Dec 25 '16 14:12 morris

Another possible use case is for mutations. Take the output of one mutation to create an object A and pass the id allocated into a field for object B. Then you can create multiple objects in a single call, linking them together. The same @export annotation could be used and mutations are already sequential, so sounds not too hard...

alankent avatar May 23 '17 17:05 alankent

Is this a priority yet? I see a PR here: https://github.com/graphql-python/graphql-core/pull/99

Seems like a natural extension to allow sequential processing that is dependent on previous queries / mutation.

My use case, and I suspect anyone who has ever dealt with a relational db (ie. a huge use case) is that people who want to create multiple dependent relationships in one query:

mutation createClientAndAccount($clientDetails: ClientInput, $accountDetails: AccountInput ){
    createClient(input: $clientDetails) {
        id: (@export as clientId)
    },
    // account is a table that has a foreign key to client, ie a clientId
    createAccount(input: $accountDetails, clientId: $clientId) {
        ....
    }
}

Currently this type of functionality is handled in a resolve function. However that seems totally unnecessary if the language can handle this type of sequential processing. We already know mutations happen sequentially, so theoretically under the hood, even before createAccount is called, we know that somewhere graphql already has the clientId. Ergo, it makes sense to allow a sequential query to be called with the response data there.

terencechow avatar Jun 09 '17 09:06 terencechow

Has anyone been working on @helfer 's suggestion? ie. Implementing this as a standalone directive? I could spare some time for it if nobody else is up for the challenge.

aecepoglu avatar Sep 07 '17 12:09 aecepoglu

Any updates? I want to do the same thing but i seems the current release doesn't support this feature.

shtse8 avatar Oct 29 '17 07:10 shtse8

FYI, recently I added batch executor in Sangria:

http://sangria-graphql.org/learn/#batch-executor

It implements all of the functionality described in the talk, including @export directive and (optional) variable inference. Turns out that @export directive is quite non-trivial to implement, mostly because one needs to construct the full dependency graph between queries, analyze cyclic dependencies, etc.

Still collecting the feedback from the users. Hopefully, at some point, I will have enough info to share an update (pros/cons based on the user feedback).

OlegIlyenko avatar Oct 30 '17 18:10 OlegIlyenko

Any update on this?

Fintasys avatar Mar 17 '21 14:03 Fintasys

This is what we use in schema stitching

https://github.com/ardatan/graphql-tools/blob/33abcf5c83934c2b78c46764e66c1b493e47b435/packages/batch-execute/src/getBatchingExecutor.ts#L7

it’s based on gatsby batch execution, transforms query to combine root fields wo conflicts, rather than send in array of operations

no ‘export’ directive

yaacovCR avatar Mar 17 '21 15:03 yaacovCR

Any updates on this? :)

jBernavaPrah avatar Jul 29 '23 14:07 jBernavaPrah

Any updates on this? :)

mauriau avatar Aug 09 '24 08:08 mauriau