Skip to content

Commit d9c4bf9

Browse files
committed
feat: variable batching
1 parent 6725f8e commit d9c4bf9

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-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
});
@@ -287,4 +289,32 @@ describe('Batching', () => {
287289
],
288290
});
289291
});
292+
it('variable batching', async () => {
293+
const yoga = createYoga({
294+
schema,
295+
batching: {},
296+
});
297+
const query = /* GraphQL */ `
298+
query ($name: String!) {
299+
greetings(name: $name)
300+
}
301+
`;
302+
const res = await yoga.fetch('http://yoga/graphql', {
303+
method: 'POST',
304+
headers: {
305+
accept: 'application/graphql-response+json',
306+
'Content-Type': 'application/json',
307+
},
308+
body: JSON.stringify({
309+
query,
310+
variables: [{ name: 'Alice' }, { name: 'Bob' }, { name: 'Charlie' }],
311+
}),
312+
});
313+
const result = await res.json();
314+
expect(result).toEqual([
315+
{ data: { greetings: 'hello, Alice' } },
316+
{ data: { greetings: 'hello, Bob' } },
317+
{ data: { greetings: 'hello, Charlie' } },
318+
]);
319+
});
290320
});

packages/graphql-yoga/src/server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import {
6767
YogaMaskedErrorOpts,
6868
} from './types.js';
6969
import { maskError } from './utils/mask-error.js';
70+
import { processBatchedParams } from './utils/process-batched-params.js';
7071

7172
/**
7273
* Configuration options for the server
@@ -535,6 +536,8 @@ export class YogaServer<
535536
});
536537
}
537538

539+
requestParserResult = processBatchedParams(requestParserResult);
540+
538541
const result = (await (Array.isArray(requestParserResult)
539542
? Promise.all(
540543
requestParserResult.map(params =>
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.map(param => processBatchedParams(param)).flat();
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)