Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Batching Mutations #517

Closed
ricbermo opened this issue Apr 28, 2023 · 4 comments
Closed

Add Support for Batching Mutations #517

ricbermo opened this issue Apr 28, 2023 · 4 comments

Comments

@ricbermo
Copy link

Perceived Problem

I was trying to delete multiple records at once but I noticed my "documents" are sent as queries

const requests = uuids.map((uuid: string) => ({
  document: deleteRecordById,
    variables: {uuid},
  }));
await client.batchRequests(requests);

// export const deleteRecordById = `
mutation deleteRecordById($uuid: ID!) {
  deleteRecordById(id: $uuid) {
    id
  }
}`;

Ideas / Proposed Solution(s)

It'd be nice to let batchRequests work for mutations as well.
For now, this is what I did

const request = uuids.map((uuid: string) =>
  client.request(deleteChildAthleteById, {uuid}),
);
await Promise.all(request);

Thanks

@DanielAtCosmicDNA
Copy link

For the time being one can use:

const batchRequests = async (requests) => Promise.all(requests.map((request) => mutationClient.request(request.document, request.variables)))

@goleary
Copy link

goleary commented Jun 26, 2023

The downside with this workaround is that it's not really batching requests since multiple requests are sent albeit in parallel.

@DanielAtCosmicDNA
Copy link

The downside with this workaround is that it's not really batching requests since multiple requests are sent albeit in parallel.

The idea is to use this in the meanwhile there is no graphql-request working feature that contemplates batching mutations. Once graphql-request develops the feature, it is just the matter of using graphql-request's batchRequests as a drop-in replacement for this workaround.

@jasonkuhrt
Copy link
Owner

jasonkuhrt commented Aug 4, 2024

Graffle has batch support in the sense that graphQL does. For example you can do this in GraphQL:

  mutation {
    deleteUser1: deleteUser(id:"abc1")
    deleteUser2: deleteUser(id:"abc2")
    deleteUser3: deleteUser(id:"abc3")
    deleteUser4: deleteUser(id:"abc4")
    # ...
  }

This is run serially on the server which may not be what you want. Interestingly you could run them in parallel by using nested fields:

  mutation {
    delete: {
	    user1: user(id:"abc1")
	    user2: user(id:"abc2")
	    user3: user(id:"abc3")
	    user4: user(id:"abc4")
	    # ...
    }
  }

Now those deletes would be executed in parallel by the server.

I understand there is also the concept of HTTP Request batching wherein multiple GraphQL documents are sent in ONE HTTP body. My understanding is Apollo originally pioneered this approach or at least popularized it. Graffle should eventually support this but not immediately unless there is major user requests. The new feature is being tracked at #1017.

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

No branches or pull requests

4 participants