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

Root level limit with validate #3240

Open
wants to merge 4 commits into
base: root-level-limit-with-validate
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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`.
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createSchema, createYoga } from 'graphql-yoga';
import { rootLevelQueryLimit } from '../src/index.js';
import { rootLevelQueryLimit } from '../src/index';

describe('root-level-limitation', () => {
const schema = createSchema({
Expand Down Expand Up @@ -87,7 +87,9 @@ describe('root-level-limitation', () => {
'Content-Type': 'application/json',
},
});
})

expect(res.status).toBe(400);
});

it('should allow requests with max root level query in comments', async () => {
const res = await yoga.fetch('http://yoga/graphql', {
Expand All @@ -110,5 +112,5 @@ describe('root-level-limitation', () => {
});

expect(res.status).toBe(200);
})
});
});
Loading
Loading