Skip to content

Commit f081ce4

Browse files
n1ru4lardatangithub-actions[bot]
committed
alpha release testing (#3208)
* alpha release testing * Try the new alpha fastify * fastify subscription cleanup test * update integration guides * use proper version * chore(dependencies): updated changesets for modified dependencies --------- Co-authored-by: Arda TANRIKULU <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 730a80e commit f081ce4

File tree

14 files changed

+111
-24
lines changed

14 files changed

+111
-24
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'graphql-yoga': patch
3+
---
4+
dependencies updates:
5+
- Updated dependency
6+
[`@whatwg-node/server@0.9.32-alpha-20240325115716-84ad025b793c368e6cfd30cdc6fce6bc76b862d3`
7+
↗︎](https://www.npmjs.com/package/@whatwg-node/server/v/0.9.32) (from `^0.9.31`, in
8+
`dependencies`)

examples/fastify-modules/src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function createGraphQLHandler(): RouteHandlerMethod & {
2323
});
2424

2525
const handler = async (req, reply) => {
26-
const response = await graphQLServer.handleNodeRequest(req, {
26+
const response = await graphQLServer.handleNodeRequestAndResponse(req, reply, {
2727
req,
2828
reply,
2929
});

examples/fastify/__integration-tests__/fastify.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import request from 'supertest';
2+
import { eventStream } from '../../../packages/graphql-yoga/__tests__/utilities.js';
23
import { buildApp } from '../src/app.js';
34

45
describe('fastify example integration', () => {
@@ -180,6 +181,7 @@ event: complete
180181
data"
181182
`);
182183
});
184+
183185
it('handles subscription operations via POST', async () => {
184186
const [app] = buildApp(false);
185187
await app.ready();
@@ -235,6 +237,7 @@ event: complete
235237
data"
236238
`);
237239
});
240+
238241
it('should handle file uploads', async () => {
239242
const [app] = buildApp(false);
240243
await app.ready();
@@ -259,6 +262,7 @@ data"
259262
},
260263
});
261264
});
265+
262266
it('request cancelation', async () => {
263267
const [app] = buildApp(false);
264268
await app.ready();
@@ -308,6 +312,55 @@ data"
308312
app.log.info = info;
309313
}
310314
});
315+
316+
it('subscription cancelation', async () => {
317+
const [app] = buildApp(false);
318+
await app.ready();
319+
const cancelationIsLoggedPromise = createDeferred();
320+
const address = await app.listen({
321+
port: 0,
322+
});
323+
324+
// we work with logger statements to detect when the subscription source is cleaned up.
325+
const loggerOverwrite = (part: unknown) => {
326+
if (part === 'countdown aborted') {
327+
cancelationIsLoggedPromise.resolve();
328+
}
329+
};
330+
331+
const info = app.log.info;
332+
app.log.info = loggerOverwrite;
333+
334+
try {
335+
const abortController = new AbortController();
336+
const url = new URL(`${address}/graphql`);
337+
url.searchParams.set(
338+
'query',
339+
/* GraphQL */ `
340+
subscription {
341+
countdown(from: 10, interval: 5)
342+
}
343+
`,
344+
);
345+
const response = await fetch(url, {
346+
method: 'GET',
347+
headers: {
348+
'content-type': 'application/json',
349+
accept: 'text/event-stream',
350+
},
351+
signal: abortController.signal,
352+
});
353+
354+
const iterator = eventStream(response.body!);
355+
const next = await iterator.next();
356+
expect(next.value).toEqual({ data: { countdown: 10 } });
357+
abortController.abort();
358+
await expect(iterator.next()).rejects.toMatchInlineSnapshot(`DOMException {}`);
359+
await cancelationIsLoggedPromise.promise;
360+
} finally {
361+
app.log.info = info;
362+
}
363+
});
311364
});
312365

313366
type Deferred<T = void> = {

examples/fastify/src/app.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fastify, { FastifyReply, FastifyRequest } from 'fastify';
2-
import { createSchema, createYoga } from 'graphql-yoga';
2+
import { createSchema, createYoga, Repeater } from 'graphql-yoga';
33

44
export function buildApp(logging = true) {
55
const app = fastify({
@@ -64,11 +64,26 @@ export function buildApp(logging = true) {
6464
},
6565
Subscription: {
6666
countdown: {
67-
async *subscribe(_, { from, interval }) {
68-
for (let i = from; i >= 0; i--) {
69-
await new Promise(resolve => setTimeout(resolve, interval ?? 1000));
70-
yield { countdown: i };
71-
}
67+
async subscribe(_, { from, interval }, { request }) {
68+
return new Repeater(async (push, stop) => {
69+
const timeout = setInterval(() => {
70+
push({ countdown: from });
71+
from--;
72+
if (from < 0) {
73+
stop();
74+
return;
75+
}
76+
}, interval);
77+
78+
stop.then(() => {
79+
clearInterval(timeout);
80+
});
81+
82+
request.signal.addEventListener('abort', () => {
83+
app.log.info('countdown aborted');
84+
stop();
85+
});
86+
});
7287
},
7388
},
7489
},
@@ -102,7 +117,7 @@ export function buildApp(logging = true) {
102117
url: graphQLServer.graphqlEndpoint,
103118
method: ['GET', 'POST', 'OPTIONS'],
104119
handler: async (req, reply) => {
105-
const response = await graphQLServer.handleNodeRequest(req, {
120+
const response = await graphQLServer.handleNodeRequestAndResponse(req, reply, {
106121
req,
107122
reply,
108123
});

examples/hapi/src/app.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ export async function startApp(port: number) {
5353
},
5454
},
5555
handler: async (req, h) => {
56-
const { status, headers, body } = await yoga.handleNodeRequest(req.raw.req, { req, h });
56+
const { status, headers, body } = await yoga.handleNodeRequestAndResponse(
57+
req.raw.req,
58+
req.raw.res,
59+
{ req, h },
60+
);
5761

5862
const res = h.response(
5963
Readable.from(body, {

examples/koa/src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function buildApp() {
3636
});
3737

3838
app.use(async ctx => {
39-
const response = await yoga.handleNodeRequest(ctx.req, ctx);
39+
const response = await yoga.handleNodeRequestAndResponse(ctx.req, ctx.res, ctx);
4040

4141
// Set status code
4242
ctx.status = response.status;

packages/graphql-yoga/__tests__/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function eventStream<TType = unknown>(source: ReadableStream<Uint8Array>)
1515
break;
1616
}
1717

18-
const values = result.value.toString().split('\n').filter(Boolean);
18+
const values = Buffer.from(result.value).toString('utf-8').split('\n').filter(Boolean);
1919
for (const value of values) {
2020
if (!value.startsWith('data: ')) {
2121
continue;

packages/graphql-yoga/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"@graphql-yoga/logger": "^2.0.0",
5757
"@graphql-yoga/subscription": "^5.0.0",
5858
"@whatwg-node/fetch": "^0.9.17",
59-
"@whatwg-node/server": "^0.9.31",
59+
"@whatwg-node/server": "^0.9.32",
6060
"dset": "^3.1.1",
6161
"lru-cache": "^10.0.0",
6262
"tslib": "^2.5.2"

packages/nestjs/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export abstract class AbstractYogaDriver<
162162
this.yoga = yoga as YogaDriverServerInstance<Platform>;
163163

164164
app.all(yoga.graphqlEndpoint, async (req, reply) => {
165-
const response = await yoga.handleNodeRequest(req, {
165+
const response = await yoga.handleNodeRequestAndResponse(req, reply, {
166166
req,
167167
reply,
168168
});

pnpm-lock.yaml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)