Skip to content
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 @@ -352,4 +354,32 @@ describe('Batching', () => {
expect(contexts[0]!.i).toEqual(1);
expect(contexts[1]!.i).toEqual(2);
});
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' } },
]);
});
});
2 changes: 2 additions & 0 deletions packages/graphql-yoga/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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 @@ -666,6 +667,7 @@ export class YogaServer<
if (response) {
return response;
}
requestParserResult = processBatchedParams(requestParserResult!);
const getResultForParams = this.instrumentation?.operation
? (payload: { request: Request; params: GraphQLParams }, context: any) => {
const instrumented = getInstrumented({ context, request: payload.request });
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.flatMap(param => processBatchedParams(param));
}
if (Array.isArray(params.variables)) {
return params.variables.map(variables => ({
...params,
variables,
}));
}
return params;
}
Loading