Skip to content

Commit

Permalink
fix: context properties are shared between batched requests (#3491)
Browse files Browse the repository at this point in the history
* test: context properties are shared between

* try alpha

* feat: update to latest version

* changeset

* smol refactor

* docs format

* chore(dependencies): updated changesets for modified dependencies

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
n1ru4l and github-actions[bot] authored Nov 14, 2024
1 parent 909ea1d commit 7a413bc
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 53 deletions.
7 changes: 7 additions & 0 deletions .changeset/graphql-yoga-3491-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'graphql-yoga': patch
---
dependencies updates:
- Updated dependency [`@whatwg-node/server@^0.9.55`
↗︎](https://www.npmjs.com/package/@whatwg-node/server/v/0.9.55) (from `^0.9.54`, in
`dependencies`)
9 changes: 9 additions & 0 deletions .changeset/pink-snails-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'graphql-yoga': patch
---

Fix issue where context values being shared between batched requests.

A bug within `@whatwg-node/server` caused properties assigned to a batched requests context to be
propagated to all other batched requests contexts. It is resolved by updating the dependency of
`@whatwg-node/server` to `0.9.55`.
47 changes: 47 additions & 0 deletions packages/graphql-yoga/__tests__/batching.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,51 @@ describe('Batching', () => {
],
});
});

it('batching context have different identity and properties are assignments are not shared', async () => {
let i = 0;
type Context = {
i?: number;
};
const contexts = [] as Array<Context>;
const yoga = createYoga({
schema: createSchema({ typeDefs: `type Query { _: ID }` }),
batching: true,
plugins: [
{
onParams(ctx) {
contexts.push(ctx.context);
},
},
],
context() {
i = i + 1;
return { i };
},
});

const response = await yoga.fetch('http://yoga/graphql', {
method: 'POST',
headers: {
accept: 'application/graphql-response+json',
'Content-Type': 'application/json',
},
body: JSON.stringify([{ query: `query A {__typename}` }, { query: `query B {__typename}` }]),
});
expect(response.status).toBe(200);
const result = await response.json();
expect(result).toEqual([
{
data: { __typename: 'Query' },
},
{
data: { __typename: 'Query' },
},
]);

expect(contexts.length).toEqual(2);
expect(contexts[0]).not.toBe(contexts[1]);
expect(contexts[0].i).toEqual(1);
expect(contexts[1].i).toEqual(2);
});
});
2 changes: 1 addition & 1 deletion packages/graphql-yoga/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"@graphql-yoga/logger": "workspace:^",
"@graphql-yoga/subscription": "workspace:^",
"@whatwg-node/fetch": "^0.10.1",
"@whatwg-node/server": "^0.9.54",
"@whatwg-node/server": "^0.9.55",
"dset": "^3.1.1",
"lru-cache": "^10.0.0",
"tslib": "^2.5.2"
Expand Down
14 changes: 4 additions & 10 deletions packages/graphql-yoga/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,17 +462,13 @@ export class YogaServer<
{
params,
request,
batched,
}: {
params: GraphQLParams;
request: Request;
batched: boolean;
},
serverContext: TServerContext,
context: TServerContext,
) {
let result: ExecutionResult | AsyncIterable<ExecutionResult> | undefined;
const context: TServerContext & YogaInitialContext =
batched === true ? Object.create(serverContext) : serverContext;

try {
for (const onParamsHook of this.onParamsHooks) {
Expand All @@ -492,7 +488,7 @@ export class YogaServer<

if (result == null) {
const additionalContext =
serverContext.request === request
context.request === request
? {
params,
}
Expand Down Expand Up @@ -549,7 +545,7 @@ export class YogaServer<
result = newResult;
},
request,
context,
context: context as TServerContext & YogaInitialContext,
});
}
return result;
Expand Down Expand Up @@ -610,17 +606,15 @@ export class YogaServer<
{
params,
request,
batched: true,
},
serverContext,
Object.create(serverContext),
),
),
)
: this.getResultForParams(
{
params: requestParserResult,
request,
batched: false,
},
serverContext,
))) as ResultProcessorInput;
Expand Down
52 changes: 26 additions & 26 deletions pnpm-lock.yaml

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

17 changes: 7 additions & 10 deletions website/src/pages/docs/features/envelop-plugins.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,20 @@ tokens etc.
import { Plugin } from 'graphql-yoga'

function useAuth(): Plugin {
return {
onRequest({ request, fetchAPI, endResponse }) {
if (!request.headers.get('authorization')) {
endResponse(
new fetchAPI.Response(
null,
{
return {
onRequest({ request, fetchAPI, endResponse }) {
if (!request.headers.get('authorization')) {
endResponse(
new fetchAPI.Response(null, {
status: 401,
headers: {
'Content-Type': 'application/json'
}
}
})
)
)
}
}
}
}
}
```

Expand Down
Loading

0 comments on commit 7a413bc

Please sign in to comment.