Skip to content

Commit 5a32954

Browse files
authored
fix: cost-limit ignore only Field Node named __schema (#772)
* fix: cost-limit ignore only Field Node named __schema * fix: linting
1 parent 8b7ed81 commit 5a32954

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

packages/plugins/cost-limit/src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ class CostLimitVisitor {
8585
node: FieldNode | FragmentDefinitionNode | InlineFragmentNode | OperationDefinitionNode | FragmentSpreadNode,
8686
depth = 0,
8787
): number {
88-
if (this.config.ignoreIntrospection && 'name' in node && node.name?.value === '__schema') {
88+
if (
89+
this.config.ignoreIntrospection &&
90+
'name' in node &&
91+
node.name?.value === '__schema' &&
92+
node.kind === Kind.FIELD
93+
) {
8994
return 0;
9095
}
9196

packages/plugins/cost-limit/test/index.spec.ts

+63
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,68 @@ describe('costLimitPlugin', () => {
8787
]);
8888
});
8989

90+
it('should limit cost for query named `__schema`', async () => {
91+
const testkit = createTestkit(
92+
[
93+
costLimitPlugin({
94+
maxCost: 10,
95+
objectCost: 4,
96+
scalarCost: 2,
97+
depthCostFactor: 2,
98+
ignoreIntrospection: true,
99+
}),
100+
],
101+
schema,
102+
);
103+
const result = await testkit.execute(`
104+
query __schema {
105+
books {
106+
title
107+
author
108+
}
109+
}
110+
`);
111+
112+
assertSingleExecutionValue(result);
113+
expect(result.errors).toBeDefined();
114+
expect(result.errors?.map((error) => error.message)).toEqual([
115+
'Syntax Error: Query Cost limit of 10 exceeded, found 12.',
116+
]);
117+
});
118+
119+
it('should limit cost for fragment named `__schema`', async () => {
120+
const testkit = createTestkit(
121+
[
122+
costLimitPlugin({
123+
maxCost: 10,
124+
objectCost: 4,
125+
scalarCost: 2,
126+
depthCostFactor: 2,
127+
ignoreIntrospection: true,
128+
}),
129+
],
130+
schema,
131+
);
132+
const result = await testkit.execute(`
133+
query {
134+
...__schema
135+
}
136+
137+
fragment __schema on Query {
138+
books {
139+
title
140+
author
141+
}
142+
}
143+
`);
144+
145+
assertSingleExecutionValue(result);
146+
expect(result.errors).toBeDefined();
147+
expect(result.errors?.map((error) => error.message)).toEqual([
148+
'Syntax Error: Query Cost limit of 10 exceeded, found 58.',
149+
]);
150+
});
151+
90152
it('should allow introspection', async () => {
91153
const testkit = createTestkit(
92154
[
@@ -100,6 +162,7 @@ describe('costLimitPlugin', () => {
100162
],
101163
schema,
102164
);
165+
const x = getIntrospectionQuery();
103166
const result = await testkit.execute(getIntrospectionQuery());
104167

105168
assertSingleExecutionValue(result);

0 commit comments

Comments
 (0)