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

Add path of GraphQL node being evaluated to estimator args #83

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ type ComplexityEstimatorArgs = {
// The GraphQL node that is being evaluated
node: FieldNode;

// The path of the GraphQL node that is being evaluated
path: string[];

// The input arguments of the field
args: { [key: string]: any };

Expand Down
29 changes: 21 additions & 8 deletions src/QueryComplexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type ComplexityEstimatorArgs = {
node: FieldNode;
args: { [key: string]: any };
childComplexity: number;
path: string[];
context?: Record<string, any>;
};

Expand Down Expand Up @@ -191,19 +192,22 @@ export default class QueryComplexity {
case 'query':
this.complexity += this.nodeComplexity(
operation,
this.context.getSchema().getQueryType()
this.context.getSchema().getQueryType(),
[]
);
break;
case 'mutation':
this.complexity += this.nodeComplexity(
operation,
this.context.getSchema().getMutationType()
this.context.getSchema().getMutationType(),
[]
);
break;
case 'subscription':
this.complexity += this.nodeComplexity(
operation,
this.context.getSchema().getSubscriptionType()
this.context.getSchema().getSubscriptionType(),
[]
);
break;
default:
Expand Down Expand Up @@ -238,7 +242,8 @@ export default class QueryComplexity {
| FragmentDefinitionNode
| InlineFragmentNode
| OperationDefinitionNode,
typeDef: GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType
typeDef: GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType,
currentPath: string[]
): number {
if (node.selectionSet) {
let fields: GraphQLFieldMap<any, any> = {};
Expand Down Expand Up @@ -327,10 +332,15 @@ export default class QueryComplexity {
return complexities;
}

const childPath = [...currentPath, childNode.name.value];
// Check if we have child complexity
let childComplexity = 0;
if (isCompositeType(fieldType)) {
childComplexity = this.nodeComplexity(childNode, fieldType);
childComplexity = this.nodeComplexity(
childNode,
fieldType,
childPath
);
}

// Run estimators one after another and return first valid complexity
Expand All @@ -339,6 +349,7 @@ export default class QueryComplexity {
childComplexity,
args,
field,
path: childPath,
node: childNode,
type: typeDef,
context: this.requestContext,
Expand Down Expand Up @@ -386,7 +397,8 @@ export default class QueryComplexity {
}
const nodeComplexity = this.nodeComplexity(
fragment,
fragmentType
fragmentType,
currentPath
);
if (isAbstractType(fragmentType)) {
// Add fragment complexity for all possible types
Expand Down Expand Up @@ -421,7 +433,8 @@ export default class QueryComplexity {

const nodeComplexity = this.nodeComplexity(
childNode,
inlineFragmentType
inlineFragmentType,
currentPath
);
if (isAbstractType(inlineFragmentType)) {
// Add fragment complexity for all possible types
Expand All @@ -445,7 +458,7 @@ export default class QueryComplexity {
}
default: {
innerComplexities = addComplexities(
this.nodeComplexity(childNode, typeDef),
this.nodeComplexity(childNode, typeDef, currentPath),
complexities,
possibleTypeNames
);
Expand Down
42 changes: 42 additions & 0 deletions src/__tests__/QueryComplexity-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,48 @@ describe('QueryComplexity analysis', () => {
expect(complexity).to.equal(30); // 3 fields on nonNullItem * 10
});

it('should include the current path in the estimator args', () => {
const ast = parse(`
query {
nonNullItem {
scalar
complexScalar
variableScalar(count: 10)
}
}
`);

const pathEstimator: ComplexityEstimator = ({
field,
path,
childComplexity,
}) => {
switch (field.name) {
case 'variableScalar':
expect(path).to.deep.equal(['nonNullItem', 'variableScalar']);
return 1 + childComplexity;
case 'complexScalar':
expect(path).to.deep.equal(['nonNullItem', 'complexScalar']);
return 2 + childComplexity;
case 'scalar':
expect(path).to.deep.equal(['nonNullItem', 'scalar']);
return 4 + childComplexity;
case 'nonNullItem':
expect(path).to.deep.equal(['nonNullItem']);
return 8 + childComplexity;
default:
return 0;
}
};

const complexity = getComplexity({
estimators: [pathEstimator],
schema,
query: ast,
});
expect(complexity).to.equal(8 + 4 + 2 + 1);
});

it('should handle invalid argument values for multiple query fields', () => {
const ast = parse(`
query {
Expand Down