Skip to content

Commit

Permalink
refactor: use weak map
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Nov 7, 2024
1 parent ebc06c9 commit dfd0ca2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
29 changes: 10 additions & 19 deletions packages/graphql-yoga/__tests__/batching.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSchema, createYoga, Plugin } from '../src';
import { createSchema, createYoga, getBatchRequestIndexFromContext, Plugin } from '../src/index.js';

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / benchmarks

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / esm

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / leaks / nodejs v18 / graphql v15.8.0

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / integration / nodejs v22 / graphql v15.8.0

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / integration / nodejs v18 / graphql v16.6.0

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / leaks / nodejs v22 / graphql v15.8.0

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / leaks / nodejs v18 / graphql v16.6.0

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / integration / nodejs v20 / graphql v15.8.0

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / integration / nodejs v22 / graphql v16.6.0

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / integration / nodejs v20 / graphql v16.6.0

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / apollo-federation-compatibility (20, 15.8.0)

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / leaks / nodejs v22 / graphql v16.6.0

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / integration / nodejs v18 / graphql v15.8.0

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / apollo-federation-compatibility (20, 16.6.0)

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / apollo-federation-compatibility (22, 15.8.0)

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / apollo-federation-compatibility (22, 16.6.0)

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / apollo-federation-compatibility (18, 16.6.0)

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / apollo-federation-compatibility (18, 15.8.0)

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / unit / nodejs v18 / graphql v16.6.0

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / unit / nodejs v18 / graphql v15.8.0

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / e2e / aws-lambda

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / e2e / azure-function

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / e2e / docker-node

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / e2e / cf-modules

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / e2e / cf-worker

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

Check failure on line 1 in packages/graphql-yoga/__tests__/batching.spec.ts

View workflow job for this annotation

GitHub Actions / alpha / snapshot

Module '"../src/index.js"' has no exported member 'getBatchRequestIndexFromContext'.

describe('Batching', () => {
const schema = createSchema({
Expand Down Expand Up @@ -296,16 +296,13 @@ describe('Batching', () => {
expect(params.batchedRequestIndex).toEqual(0);
},
onParse(context) {
// @ts-expect-error not in types
expect(context.context[Symbol.for('yogaBatchedRequestIndex')]).toEqual(0);
expect(getBatchRequestIndexFromContext(context.context)).toEqual(0);
},
onValidate(context) {
// @ts-expect-error not in types
expect(context.context[Symbol.for('yogaBatchedRequestIndex')]).toEqual(0);
expect(getBatchRequestIndexFromContext(context.context)).toEqual(0);
},
onExecute(context) {
// @ts-expect-error not in types
expect(context.args.contextValue[Symbol.for('yogaBatchedRequestIndex')]).toEqual(0);
expect(getBatchRequestIndexFromContext(context.args.contextValue)).toEqual(0);
},
} satisfies Plugin,
],
Expand Down Expand Up @@ -338,31 +335,25 @@ describe('Batching', () => {
onParse(context) {
const params = JSON.stringify(context.params);
if (params === '{"source":"{hello}"}') {
// @ts-expect-error not in types
expect(context.context[Symbol.for('yogaBatchedRequestIndex')]).toEqual(0);
expect(getBatchRequestIndexFromContext(context.context)).toEqual(0);
} else if (params === '{"source":"{bye}"}') {
// @ts-expect-error not in types
expect(context.context[Symbol.for('yogaBatchedRequestIndex')]).toEqual(1);
expect(getBatchRequestIndexFromContext(context.context)).toEqual(1);
}
},
onValidate(context) {
const params = JSON.stringify(context.params);
if (params === '{"source":"{hello}"}') {
// @ts-expect-error not in types
expect(context.context[Symbol.for('yogaBatchedRequestIndex')]).toEqual(0);
expect(getBatchRequestIndexFromContext(context.context)).toEqual(0);
} else if (params === '{"source":"{bye}"}') {
// @ts-expect-error not in types
expect(context.context[Symbol.for('yogaBatchedRequestIndex')]).toEqual(1);
expect(getBatchRequestIndexFromContext(context.context)).toEqual(1);
}
},
onExecute(context) {
const params = JSON.stringify(context.args.contextValue.params);
if (params === '{"source":"{hello}"}') {
// @ts-expect-error not in types
expect(context.context[Symbol.for('yogaBatchedRequestIndex')]).toEqual(0);
expect(getBatchRequestIndexFromContext(context.args.contextValue)).toEqual(0);
} else if (params === '{"source":"{bye}"}') {
// @ts-expect-error not in types
expect(context.context[Symbol.for('yogaBatchedRequestIndex')]).toEqual(1);
expect(getBatchRequestIndexFromContext(context.args.contextValue)).toEqual(1);
}
},
} satisfies Plugin,
Expand Down
12 changes: 5 additions & 7 deletions packages/graphql-yoga/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import {
YogaInitialContext,
YogaMaskedErrorOpts,
} from './types.js';
import { batchRequestIndexMap } from './utils/batch-request-index.js';
import { maskError } from './utils/mask-error.js';

/**
Expand Down Expand Up @@ -498,18 +499,15 @@ export class YogaServer<
params,
};

let batchIndexPartial: object = {};

if (batchedRequestIndex !== undefined) {
batchIndexPartial = { [Symbol.for('yogaBatchedRequestIndex')]: batchedRequestIndex };
}

context = Object.assign(
batchedRequestIndex === undefined ? serverContext : Object.create(serverContext),
additionalContext,
batchIndexPartial,
);

if (batchedRequestIndex !== undefined) {
batchRequestIndexMap.set(context, batchedRequestIndex);
}

const enveloped = this.getEnveloped(context);

this.logger.debug(`Processing GraphQL Parameters`);
Expand Down
5 changes: 5 additions & 0 deletions packages/graphql-yoga/src/utils/batch-request-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const batchRequestIndexMap = new WeakMap<object, number>();

export function getBatchRequestIndexFromContext(context: object): number | null {
return batchRequestIndexMap.get(context) ?? null;
}

0 comments on commit dfd0ca2

Please sign in to comment.