Skip to content

Commit

Permalink
them leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Mar 29, 2024
1 parent 7233a02 commit a1c3467
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 44 deletions.
45 changes: 19 additions & 26 deletions examples/fastify/__integration-tests__/fastify.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import request from 'supertest';
import { fetch } from '@whatwg-node/fetch';
import { eventStream } from '../../../packages/graphql-yoga/__tests__/utilities.js';
import { buildApp } from '../src/app.js';

describe('fastify example integration', () => {
it('sends GraphiQL', async () => {
const [app] = buildApp(false);
let app: ReturnType<typeof buildApp>[0];

beforeEach(async () => {
[app] = buildApp(false);
await app.ready();
});

afterEach(async () => {
await app.close();
});

it('sends GraphiQL', async () => {
const response = await request(app.server).get('/graphql').set({
accept: 'text/html',
});
Expand All @@ -15,8 +25,6 @@ describe('fastify example integration', () => {
});

it('handles query operation via POST', async () => {
const [app] = buildApp(false);
await app.ready();
const response = await request(app.server)
.post('/graphql')
.set({ 'content-type': 'application/json' })
Expand All @@ -39,8 +47,6 @@ describe('fastify example integration', () => {
});

it("exposes fastify's request and reply objects", async () => {
const [app] = buildApp(false);
await app.ready();
const response = await request(app.server)
.post('/graphql')
.set({ 'content-type': 'application/json' })
Expand All @@ -63,8 +69,6 @@ describe('fastify example integration', () => {
});

it('handles query operation via GET', async () => {
const [app] = buildApp(false);
await app.ready();
const response = await request(app.server)
.get('/graphql')
.query({
Expand All @@ -84,8 +88,6 @@ describe('fastify example integration', () => {
});

it('handles mutation operation via POST', async () => {
const [app] = buildApp(false);
await app.ready();
const response = await request(app.server)
.post('/graphql')
.set({ 'content-type': 'application/json' })
Expand All @@ -108,8 +110,6 @@ describe('fastify example integration', () => {
});

it('rejects mutation operation via GET with an useful error message', async () => {
const [app] = buildApp(false);
await app.ready();
const response = await request(app.server)
.get('/graphql')
.query({
Expand All @@ -130,8 +130,6 @@ describe('fastify example integration', () => {
});

it('handles subscription operations via GET', async () => {
const [app] = buildApp(false);
await app.ready();
const response = await request(app.server)
.get('/graphql')
.set({ accept: 'text/event-stream' })
Expand Down Expand Up @@ -183,8 +181,6 @@ data"
});

it('handles subscription operations via POST', async () => {
const [app] = buildApp(false);
await app.ready();
const response = await request(app.server)
.post('/graphql')
.set({
Expand Down Expand Up @@ -239,8 +235,6 @@ data"
});

it('should handle file uploads', async () => {
const [app] = buildApp(false);
await app.ready();
const response = await request(app.server)
.post('/graphql')
.field(
Expand All @@ -264,8 +258,6 @@ data"
});

it('request cancelation', async () => {
const [app] = buildApp(false);
await app.ready();
const slowFieldResolverInvoked = createDeferred();
const slowFieldResolverCanceled = createDeferred();
const address = await app.listen({
Expand All @@ -284,6 +276,7 @@ data"

const info = app.log.info;
app.log.info = loggerOverwrite;
app.log.debug = loggerOverwrite;

try {
const abortController = new AbortController();
Expand All @@ -306,17 +299,16 @@ data"

await slowFieldResolverInvoked.promise;
abortController.abort();
await expect(response$).rejects.toMatchInlineSnapshot(`DOMException {}`);
await expect(response$).rejects.toMatchInlineSnapshot(
`[AbortError: The operation was aborted]`,
);
await slowFieldResolverCanceled.promise;
} finally {
app.log.info = info;
await app.close();
}
});

it('subscription cancelation', async () => {
const [app] = buildApp(false);
await app.ready();
const cancelationIsLoggedPromise = createDeferred();
const address = await app.listen({
port: 0,
Expand Down Expand Up @@ -356,11 +348,12 @@ data"
const next = await iterator.next();
expect(next.value).toEqual({ data: { countdown: 10 } });
abortController.abort();
await expect(iterator.next()).rejects.toMatchInlineSnapshot(`DOMException {}`);
await expect(iterator.next()).rejects.toMatchInlineSnapshot(
`[AbortError: The operation was aborted]`,
);
await cancelationIsLoggedPromise.promise;
} finally {
app.log.info = info;
await app.close();
}
});
});
Expand Down
1 change: 1 addition & 0 deletions examples/fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"start": "ts-node src/index.ts"
},
"dependencies": {
"@whatwg-node/fetch": "^0.9.17",
"fastify": "4.17.0",
"graphql-yoga": "5.2.0",
"pino-pretty": "10.0.0"
Expand Down
10 changes: 6 additions & 4 deletions examples/fastify/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fastify, { FastifyReply, FastifyRequest } from 'fastify';
import { createSchema, createYoga, Repeater } from 'graphql-yoga';
import { createSchema, createYoga, Repeater, useExecutionCancellation } from 'graphql-yoga';

export function buildApp(logging = true) {
const app = fastify({
Expand All @@ -15,6 +15,7 @@ export function buildApp(logging = true) {
req: FastifyRequest;
reply: FastifyReply;
}>({
plugins: [useExecutionCancellation()],
schema: createSchema({
typeDefs: /* GraphQL */ `
scalar File
Expand All @@ -41,8 +42,9 @@ export function buildApp(logging = true) {
hello: () => 'world',
isFastify: (_, __, context) => !!context.req && !!context.reply,
async slow(_, __, context) {
context.req.log.info('Slow resolver invoked resolved');
await new Promise<void>((res, reject) => {
await new Promise<void>((res, rej) => {
context.req.log.info('Slow resolver invoked');

const timeout = setTimeout(() => {
context.req.log.info('Slow field resolved');
res();
Expand All @@ -51,7 +53,7 @@ export function buildApp(logging = true) {
context.request.signal.addEventListener('abort', () => {
context.req.log.info('Slow field got cancelled');
clearTimeout(timeout);
reject(context.request.signal.reason);
rej(context.request.signal.reason);
});
});

Expand Down
20 changes: 6 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a1c3467

Please sign in to comment.