Skip to content

Commit

Permalink
Unique execution context with the correct request object (#3237)
Browse files Browse the repository at this point in the history
* Unique execution context with the correct request object

* Changeset

* chore(dependencies): updated changesets for modified dependencies

* Prettier

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ardatan and github-actions[bot] authored Apr 15, 2024
1 parent 58d3512 commit 3324bba
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 17 deletions.
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`.
60 changes: 59 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 { OnExecuteHook } from '@envelop/core';
import { Request } from '@whatwg-node/fetch';
import { createSchema, createYoga, YogaInitialContext } from '../src';

describe('requests', () => {
const schema = createSchema({
Expand Down Expand Up @@ -453,4 +455,60 @@ 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
17 changes: 9 additions & 8 deletions packages/graphql-yoga/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,14 +463,15 @@ export class YogaServer<
}

if (result == null) {
const additionalContext = args[0]?.request
? {
params,
}
: {
request,
params,
};
const additionalContext =
args[0]?.request === request
? {
params,
}
: {
request,
params,
};

const initialContext = args[0]
? batched
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.

0 comments on commit 3324bba

Please sign in to comment.