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

feat: variable batching #3198

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-owls-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-yoga': minor
---

Support variable batching
30 changes: 30 additions & 0 deletions packages/graphql-yoga/__tests__/batching.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ describe('Batching', () => {
type Query {
hello: String
bye: String
greetings(name: String!): String!
}
`,
resolvers: {
Query: {
hello: () => 'hello',
bye: () => 'bye',
greetings: (_root, { name }) => `hello, ${name}`,
},
},
});
Expand Down Expand Up @@ -287,4 +289,32 @@ describe('Batching', () => {
],
});
});
it('variable batching', async () => {
const yoga = createYoga({
schema,
batching: {},
});
const query = /* GraphQL */ `
query ($name: String!) {
greetings(name: $name)
}
`;
const res = await yoga.fetch('http://yoga/graphql', {
method: 'POST',
headers: {
accept: 'application/graphql-response+json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
variables: [{ name: 'Alice' }, { name: 'Bob' }, { name: 'Charlie' }],
}),
});
const result = await res.json();
expect(result).toEqual([
{ data: { greetings: 'hello, Alice' } },
{ data: { greetings: 'hello, Bob' } },
{ data: { greetings: 'hello, Charlie' } },
]);
});
});
3 changes: 3 additions & 0 deletions packages/graphql-yoga/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
YogaMaskedErrorOpts,
} from './types.js';
import { maskError } from './utils/mask-error.js';
import { processBatchedParams } from './utils/process-batched-params.js';

/**
* Configuration options for the server
Expand Down Expand Up @@ -535,6 +536,8 @@ export class YogaServer<
});
}

requestParserResult = processBatchedParams(requestParserResult);

const result = (await (Array.isArray(requestParserResult)
? Promise.all(
requestParserResult.map(params =>
Expand Down
16 changes: 16 additions & 0 deletions packages/graphql-yoga/src/utils/process-batched-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { GraphQLParams } from '../types.js';

export function processBatchedParams(
params: GraphQLParams | GraphQLParams[],
): GraphQLParams | GraphQLParams[] {
if (Array.isArray(params)) {
return params.map(param => processBatchedParams(param)).flat();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use flatMap directly instead of chaining map and flat ?

}
if (Array.isArray(params.variables)) {
return params.variables.map(variables => ({
...params,
variables,
}));
}
return params;
}
Loading