Skip to content

Commit

Permalink
Merge pull request #936 from arthursimas1/complexity-zero
Browse files Browse the repository at this point in the history
plugin-complexity: fix bug when limits are zero
  • Loading branch information
hayes committed Jun 15, 2023
2 parents f1faf84 + 2f23a1a commit 1458c42
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-swans-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@pothos/plugin-complexity': minor
---

fix bug when limits are zero
6 changes: 3 additions & 3 deletions packages/plugin-complexity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ export class PothosComplexityPlugin<Types extends SchemaTypes> extends BasePlugi

let errorKind: ComplexityErrorKind | null = null;

if (max.depth && max.depth < depth) {
if (typeof max.depth === 'number' && max.depth < depth) {
errorKind = ComplexityErrorKind.Depth;
} else if (max.breadth && max.breadth < breadth) {
} else if (typeof max.breadth === 'number' && max.breadth < breadth) {
errorKind = ComplexityErrorKind.Breadth;
} else if (max.complexity && max.complexity < complexity) {
} else if (typeof max.complexity === 'number' && max.complexity < complexity) {
errorKind = ComplexityErrorKind.Complexity;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-complexity/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function createComplexityRule({
reportError(error);
});
} else {
if (maxComplexity && state.complexity > maxComplexity) {
if (typeof maxComplexity === 'number' && state.complexity > maxComplexity) {
reportError(
new GraphQLError(
`Query complexity of ${state.complexity} exceeds max complexity of ${maxComplexity}`,
Expand All @@ -95,7 +95,7 @@ export function createComplexityRule({
);
}

if (maxDepth && state.depth > maxDepth) {
if (typeof maxDepth === 'number' && state.depth > maxDepth) {
reportError(
new GraphQLError(`Query depth of ${state.depth} exceeds max depth of ${maxDepth}`, {
extensions: {
Expand All @@ -109,7 +109,7 @@ export function createComplexityRule({
);
}

if (maxBreadth && state.breadth > maxBreadth) {
if (typeof maxBreadth === 'number' && state.breadth > maxBreadth) {
reportError(
new GraphQLError(
`Query breadth of ${state.breadth} exceeds max breadth of ${maxBreadth}`,
Expand Down
70 changes: 70 additions & 0 deletions packages/plugin-complexity/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`createComplexityRule > negative limits 1`] = `
[
[GraphQLError: Query complexity of 2 exceeds max complexity of -1],
[GraphQLError: Query depth of 2 exceeds max depth of -1],
[GraphQLError: Query breadth of 2 exceeds max breadth of -1],
]
`;

exports[`createComplexityRule > zero limits 1`] = `
[
[GraphQLError: Query complexity of 2 exceeds max complexity of 0],
[GraphQLError: Query depth of 2 exceeds max depth of 0],
[GraphQLError: Query breadth of 2 exceeds max breadth of 0],
]
`;

exports[`simple objects example schema > complexity from query > as string 1`] = `
{
"breadth": 7,
Expand Down Expand Up @@ -101,6 +117,33 @@ type Query {
}"
`;

exports[`simple objects example schema > negative limits > negative breadth 1`] = `
{
"data": null,
"errors": [
[GraphQLError: Query exceeds maximum breadth (breadth: 2, max: -1)],
],
}
`;

exports[`simple objects example schema > negative limits > negative complexity 1`] = `
{
"data": null,
"errors": [
[GraphQLError: Query exceeds maximum complexity (complexity: 2, max: -1)],
],
}
`;

exports[`simple objects example schema > negative limits > negative depth 1`] = `
{
"data": null,
"errors": [
[GraphQLError: Query exceeds maximum depth (depth: 2, max: -1)],
],
}
`;

exports[`simple objects example schema > queries > complexity based on args 1`] = `
{
"data": {
Expand Down Expand Up @@ -419,3 +462,30 @@ exports[`simple objects example schema > queries > valid query 1`] = `
},
}
`;

exports[`simple objects example schema > zero limits > zero breadth 1`] = `
{
"data": null,
"errors": [
[GraphQLError: Query exceeds maximum breadth (breadth: 2, max: 0)],
],
}
`;

exports[`simple objects example schema > zero limits > zero complexity 1`] = `
{
"data": null,
"errors": [
[GraphQLError: Query exceeds maximum complexity (complexity: 2, max: 0)],
],
}
`;

exports[`simple objects example schema > zero limits > zero depth 1`] = `
{
"data": null,
"errors": [
[GraphQLError: Query exceeds maximum depth (depth: 2, max: 0)],
],
}
`;
108 changes: 108 additions & 0 deletions packages/plugin-complexity/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,66 @@ describe('simple objects example schema', () => {
expect(result).toMatchSnapshot();
});
});

describe('negative limits', () => {
const limitTypes = ['depth', 'breadth', 'complexity'] as const;

limitTypes.map((limit) => {
it(`negative ${limit}`, async () => {
const query = gql`
query {
hero(episode: EMPIRE) {
name
}
}
`;

const result = await execute({
schema: exampleSchema,
document: query,
contextValue: {
complexity: {
depth: limit === 'depth' ? -1 : 10,
breadth: limit === 'breadth' ? -1 : 10,
complexity: limit === 'complexity' ? -1 : 10,
},
},
});

expect(result).toMatchSnapshot();
});
});
});

describe('zero limits', () => {
const limitTypes = ['depth', 'breadth', 'complexity'] as const;

limitTypes.map((limit) => {
it(`zero ${limit}`, async () => {
const query = gql`
query {
hero(episode: EMPIRE) {
name
}
}
`;

const result = await execute({
schema: exampleSchema,
document: query,
contextValue: {
complexity: {
depth: limit === 'depth' ? 0 : 10,
breadth: limit === 'breadth' ? 0 : 10,
complexity: limit === 'complexity' ? 0 : 10,
},
},
});

expect(result).toMatchSnapshot();
});
});
});
});

describe('createComplexityRule', () => {
Expand Down Expand Up @@ -310,4 +370,52 @@ describe('createComplexityRule', () => {
]
`);
});

it('negative limits', async () => {
const results = validate(
exampleSchema,
gql`
query {
hero(episode: EMPIRE) {
name
}
}
`,
[
createComplexityRule({
maxDepth: -1,
maxBreadth: -1,
maxComplexity: -1,
variableValues: {},
context: {},
}),
],
);

expect(results).toMatchSnapshot();
});

it('zero limits', async () => {
const results = validate(
exampleSchema,
gql`
query {
hero(episode: EMPIRE) {
name
}
}
`,
[
createComplexityRule({
maxDepth: 0,
maxBreadth: 0,
maxComplexity: 0,
variableValues: {},
context: {},
}),
],
);

expect(results).toMatchSnapshot();
});
});

1 comment on commit 1458c42

@vercel
Copy link

@vercel vercel bot commented on 1458c42 Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.