Skip to content

Commit d249f25

Browse files
committed
feat: variable batching
1 parent c3d545d commit d249f25

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

.changeset/quiet-owls-occur.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'graphql-yoga': minor
3+
---
4+
5+
Support variable batching

packages/graphql-yoga/__tests__/batching.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ describe('Batching', () => {
77
type Query {
88
hello: String
99
bye: String
10+
greetings(name: String!): String!
1011
}
1112
`,
1213
resolvers: {
1314
Query: {
1415
hello: () => 'hello',
1516
bye: () => 'bye',
17+
greetings: (_root, { name }) => `hello, ${name}`,
1618
},
1719
},
1820
});
@@ -352,4 +354,32 @@ describe('Batching', () => {
352354
expect(contexts[0]!.i).toEqual(1);
353355
expect(contexts[1]!.i).toEqual(2);
354356
});
357+
it('variable batching', async () => {
358+
const yoga = createYoga({
359+
schema,
360+
batching: {},
361+
});
362+
const query = /* GraphQL */ `
363+
query ($name: String!) {
364+
greetings(name: $name)
365+
}
366+
`;
367+
const res = await yoga.fetch('http://yoga/graphql', {
368+
method: 'POST',
369+
headers: {
370+
accept: 'application/graphql-response+json',
371+
'Content-Type': 'application/json',
372+
},
373+
body: JSON.stringify({
374+
query,
375+
variables: [{ name: 'Alice' }, { name: 'Bob' }, { name: 'Charlie' }],
376+
}),
377+
});
378+
const result = await res.json();
379+
expect(result).toEqual([
380+
{ data: { greetings: 'hello, Alice' } },
381+
{ data: { greetings: 'hello, Bob' } },
382+
{ data: { greetings: 'hello, Charlie' } },
383+
]);
384+
});
355385
});

packages/graphql-yoga/src/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import {
8383
YogaMaskedErrorOpts,
8484
} from './types.js';
8585
import { maskError } from './utils/mask-error.js';
86+
import { processBatchedParams } from './utils/process-batched-params.js';
8687

8788
/**
8889
* Configuration options for the server
@@ -666,6 +667,7 @@ export class YogaServer<
666667
if (response) {
667668
return response;
668669
}
670+
requestParserResult = processBatchedParams(requestParserResult!);
669671
const getResultForParams = this.instrumentation?.operation
670672
? (payload: { request: Request; params: GraphQLParams }, context: any) => {
671673
const instrumented = getInstrumented({ context, request: payload.request });
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { GraphQLParams } from '../types.js';
2+
3+
export function processBatchedParams(
4+
params: GraphQLParams | GraphQLParams[],
5+
): GraphQLParams | GraphQLParams[] {
6+
if (Array.isArray(params)) {
7+
return params.flatMap(param => processBatchedParams(param));
8+
}
9+
if (Array.isArray(params.variables)) {
10+
return params.variables.map(variables => ({
11+
...params,
12+
variables,
13+
}));
14+
}
15+
return params;
16+
}

0 commit comments

Comments
 (0)