Skip to content

Commit cb82ade

Browse files
committed
Export complexity directive
1 parent 1fee982 commit cb82ade

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

src/estimators/directive/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,19 @@ input Filter {
6363
```
6464

6565
The multipliers can be combined. Configured multipliers that don't have a value or `NULL` are ignored.
66+
67+
Code first approach can use the `createComplexityDirective` function to generate directive definition:
68+
69+
```typescript
70+
import {createComplexityDirective} from 'graphql-query-complexity';
71+
72+
const schema = new GraphQLSchema({
73+
directives: [
74+
createComplexityDirective({
75+
// Optionally change the name of the directive here... Default value is `complexity`
76+
name: 'complexity'
77+
})
78+
]
79+
// ... other config
80+
});
81+
```

src/estimators/directive/__tests__/directiveEstimator-test.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
*/
44

55
import {
6+
GraphQLSchema,
67
parse,
8+
print,
9+
printSchema,
710
TypeInfo,
811
visit,
912
visitWithTypeInfo,
@@ -14,7 +17,7 @@ import {expect} from 'chai';
1417
import schema from './fixtures/schema';
1518

1619
import ComplexityVisitor from '../../../QueryComplexity';
17-
import directiveEstimator from '../index';
20+
import directiveEstimator, {createComplexityDirective} from '../index';
1821
import { CompatibleValidationContext } from '../../../__tests__/fixtures/CompatibleValidationContext';
1922

2023
describe('directiveEstimator analysis', () => {
@@ -224,7 +227,7 @@ describe('directiveEstimator analysis', () => {
224227
expect(visitor.complexity).to.equal(10);
225228
});
226229

227-
it('ignores fields without compexity directive', () => {
230+
it('ignores fields without complexity directive', () => {
228231
const ast = parse(`
229232
query {
230233
noDirective
@@ -245,4 +248,28 @@ describe('directiveEstimator analysis', () => {
245248
'No complexity could be calculated for field Query.noDirective',
246249
);
247250
});
251+
252+
it('should create complexity directive that can be used to generate directive definition', () => {
253+
const complexityDirective = createComplexityDirective();
254+
const codeFirstSchema = new GraphQLSchema({
255+
directives: [complexityDirective]
256+
});
257+
258+
const printedCodeFirstSchema = printSchema(codeFirstSchema).trim();
259+
const printedComplexityDirective = print(schema.getDirective('complexity').astNode).trim();
260+
261+
expect(printedCodeFirstSchema).to.equal(printedComplexityDirective);
262+
});
263+
264+
it('should create complexity directive with configured name', () => {
265+
const complexityDirective = createComplexityDirective({name: 'cost'});
266+
const codeFirstSchema = new GraphQLSchema({
267+
directives: [complexityDirective]
268+
});
269+
270+
const printedCodeFirstSchema = printSchema(codeFirstSchema).trim();
271+
const printedComplexityDirective = print(schema.getDirective('cost').astNode).trim();
272+
273+
expect(printedCodeFirstSchema).to.equal(printedComplexityDirective);
274+
});
248275
});

src/estimators/directive/__tests__/fixtures/schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import {
77
} from 'graphql';
88

99
export default buildSchema(`
10+
"""Define a relation between the field and other nodes"""
1011
directive @cost(
12+
"""The complexity value for the field"""
1113
value: Int!,
1214
multipliers: [String!]
1315
) on FIELD_DEFINITION
1416
17+
"""Define a relation between the field and other nodes"""
1518
directive @complexity(
19+
"""The complexity value for the field"""
1620
value: Int!,
1721
multipliers: [String!]
1822
) on FIELD_DEFINITION

src/estimators/directive/index.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@ import { GraphQLDirective } from 'graphql/type/directives';
44
import { DirectiveLocation } from 'graphql/language/directiveLocation';
55
import get from 'lodash.get';
66

7-
export function complexityDirective(name: string = 'complexity') {
7+
export type ComplexityDirectiveOptions = {
8+
name?: string
9+
}
10+
11+
export function createComplexityDirective(options: ComplexityDirectiveOptions = {}) {
12+
const mergedOptions = {
13+
name: 'complexity',
14+
...(options || {})
15+
};
16+
817
return new GraphQLDirective({
9-
name,
18+
name: mergedOptions.name,
1019
description: 'Define a relation between the field and other nodes',
1120
locations: [
12-
DirectiveLocation.FIELD,
21+
DirectiveLocation.FIELD_DEFINITION,
1322
],
1423
args: {
1524
value: {
@@ -23,8 +32,8 @@ export function complexityDirective(name: string = 'complexity') {
2332
});
2433
}
2534

26-
export default function (options: { name?: string } = {}): ComplexityEstimator {
27-
const directive = complexityDirective(options.name);
35+
export default function (options: ComplexityDirectiveOptions = {}): ComplexityEstimator {
36+
const directive = createComplexityDirective(options);
2837

2938
return (args: ComplexityEstimatorArgs) => {
3039
// Ignore if astNode is undefined

src/estimators/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export {default as simpleEstimator} from './simple';
2-
export {default as directiveEstimator, complexityDirective} from './directive';
2+
export {default as directiveEstimator, ComplexityDirectiveOptions, createComplexityDirective} from './directive';
33
export {default as fieldExtensionsEstimator} from './fieldExtensions';

0 commit comments

Comments
 (0)