Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique execution context with the correct request object #3237

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/graphql-yoga-3237-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.33`
↗︎](https://www.npmjs.com/package/@whatwg-node/server/v/0.9.33) (from `^0.9.32`, in
`dependencies`)
6 changes: 6 additions & 0 deletions .changeset/smart-melons-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'graphql-yoga': patch
---

In such environments like CloudFlare Workers, the `request` object in the context always has the initial request object, so it was impossible to access the actual `Request` object from the execution context.
Now Yoga ensures that the `request` in the context is the same with the actual `Request`.
58 changes: 57 additions & 1 deletion packages/graphql-yoga/__tests__/requests.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { createSchema, createYoga } from '../src';
import { Request } from '@whatwg-node/fetch';
import { YogaInitialContext, createSchema, createYoga } from '../src';
import { OnExecuteHook } from '@envelop/core';

describe('requests', () => {
const schema = createSchema({
Expand Down Expand Up @@ -453,4 +455,58 @@ describe('requests', () => {
const body = await response.text();
expect(body).toBeFalsy();
});

it('contains the correct request object in the unique execution context', async () => {
const onExecuteFn = jest.fn((() => { }) as OnExecuteHook<YogaInitialContext>);
const yoga = createYoga({
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
greetings: String
}
`,
resolvers: {
Query: {
greetings: (_, __, ctx) => {
return `Hello world!`;
},
},
}
}),
plugins: [
{
onExecute: onExecuteFn,
}
]
});
const env = {};
const extraCtx = {};
const firstReq = new Request('http://yoga/graphql', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({ query: '{ greetings }' }),
});
const firstRes = await yoga.fetch(firstReq, env, extraCtx);
expect(firstRes.status).toBe(200);
const firstResBody = await firstRes.json();
expect(firstResBody.data.greetings).toBe('Hello world!');
expect(onExecuteFn).toHaveBeenCalledTimes(1);
expect(onExecuteFn.mock.calls[0][0].args.contextValue.request).toBe(firstReq);
const secondReq = new Request('http://yoga/graphql', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({ query: '{ greetings }' }),
});
const secondRes = await yoga.fetch(secondReq, env, extraCtx);
expect(secondRes.status).toBe(200);
const secondResBody = await secondRes.json();
expect(secondResBody.data.greetings).toBe('Hello world!');
expect(onExecuteFn).toHaveBeenCalledTimes(2);
expect(onExecuteFn.mock.calls[1][0].args.contextValue.request).toBe(secondReq);
expect(onExecuteFn.mock.calls[1][0].args.contextValue).not.toBe(onExecuteFn.mock.calls[0][0].args.contextValue);
})
});
2 changes: 1 addition & 1 deletion packages/graphql-yoga/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@graphql-yoga/logger": "^2.0.0",
"@graphql-yoga/subscription": "^5.0.0",
"@whatwg-node/fetch": "^0.9.17",
"@whatwg-node/server": "^0.9.32",
"@whatwg-node/server": "^0.9.33",
"dset": "^3.1.1",
"lru-cache": "^10.0.0",
"tslib": "^2.5.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-yoga/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export class YogaServer<
}

if (result == null) {
const additionalContext = args[0]?.request
const additionalContext = args[0]?.request === request
? {
params,
}
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

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

Loading