Skip to content

Commit

Permalink
Test if the server context is available in batched queries (#3343)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan authored Jul 23, 2024
1 parent 30baa7d commit a8bedca
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions packages/graphql-yoga/__integration-tests__/node-http.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createServer, IncomingMessage, ServerResponse, STATUS_CODES } from 'node:http';
import { AddressInfo } from 'node:net';
import { setTimeout as setTimeout$ } from 'node:timers/promises';
import { ExecutionResult } from 'graphql';
import { fetch } from '@whatwg-node/fetch';
import { createDeferred } from '../../testing-utils/create-deferred.js';
import {
Expand Down Expand Up @@ -226,4 +227,50 @@ describe('node-http', () => {
await new Promise<void>(resolve => server.close(() => resolve()));
}
});

it('`req: IncomingMessage` is available in batched requests', async () => {
expect.assertions(8);
const yoga = createYoga<{
req: IncomingMessage;
}>({
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
isNode: Boolean!
}
`,
resolvers: {
Query: {
isNode: (_, __, { req }) => req instanceof IncomingMessage,
},
},
}),
context: ({ req }) => ({ req }),
batching: {
limit: 3,
},
});
const server = createServer(yoga);
await new Promise<void>(resolve => server.listen(0, resolve));
const port = (server.address() as AddressInfo).port;

try {
const response = await fetch(`http://localhost:${port}/graphql`, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify([{ query: '{isNode}' }, { query: '{isNode}' }, { query: '{isNode}' }]),
});
expect(response.status).toBe(200);
const body: ExecutionResult[] = await response.json();
expect(body).toHaveLength(3);
for (const result of body) {
expect(result.errors).toBeUndefined();
expect(result.data?.isNode).toBe(true);
}
} finally {
await new Promise<void>(resolve => server.close(() => resolve()));
}
});
});

0 comments on commit a8bedca

Please sign in to comment.