33 */
44
55import {
6+ GraphQLSchema ,
67 parse ,
8+ print ,
9+ printSchema ,
710 TypeInfo ,
811 visit ,
912 visitWithTypeInfo ,
13+ buildSchema
1014} from 'graphql' ;
1115
1216import { expect } from 'chai' ;
1317
1418import schema from './fixtures/schema' ;
1519
1620import ComplexityVisitor from '../../../QueryComplexity' ;
17- import directiveEstimator from '../index' ;
21+ import directiveEstimator , { createComplexityDirective } from '../index' ;
1822import { CompatibleValidationContext } from '../../../__tests__/fixtures/CompatibleValidationContext' ;
1923
2024describe ( 'directiveEstimator analysis' , ( ) => {
@@ -224,7 +228,7 @@ describe('directiveEstimator analysis', () => {
224228 expect ( visitor . complexity ) . to . equal ( 10 ) ;
225229 } ) ;
226230
227- it ( 'ignores fields without compexity directive' , ( ) => {
231+ it ( 'ignores fields without complexity directive' , ( ) => {
228232 const ast = parse ( `
229233 query {
230234 noDirective
@@ -245,4 +249,38 @@ describe('directiveEstimator analysis', () => {
245249 'No complexity could be calculated for field Query.noDirective' ,
246250 ) ;
247251 } ) ;
252+
253+ it ( 'should create complexity directive that can be used to generate directive definition' , ( ) => {
254+ const complexityDirective = createComplexityDirective ( ) ;
255+ const codeFirstSchema = new GraphQLSchema ( {
256+ directives : [ complexityDirective ]
257+ } ) ;
258+
259+ // rebuilding code first schema
260+ // graphql-js <= 14 prints descriptions in different ways printSchema(schema) vs print(astNode)
261+ // and directive from code first schema has no astNode
262+ const builtCodeFirstSchema = buildSchema ( printSchema ( codeFirstSchema ) ) ;
263+
264+ const printedSchemaFirstDirective = print ( schema . getDirective ( 'complexity' ) . astNode ) ;
265+ const printedCodeFirstDirective = print ( builtCodeFirstSchema . getDirective ( 'complexity' ) . astNode ) ;
266+
267+ expect ( printedSchemaFirstDirective ) . to . equal ( printedCodeFirstDirective ) ;
268+ } ) ;
269+
270+ it ( 'should create complexity directive with configured name' , ( ) => {
271+ const complexityDirective = createComplexityDirective ( { name : 'cost' } ) ;
272+ const codeFirstSchema = new GraphQLSchema ( {
273+ directives : [ complexityDirective ]
274+ } ) ;
275+
276+ // rebuilding code first schema
277+ // graphql-js <= 14 prints descriptions in different ways printSchema(schema) vs print(astNode)
278+ // and directive from code first schema has no astNode
279+ const builtCodeFirstSchema = buildSchema ( printSchema ( codeFirstSchema ) ) ;
280+
281+ const printedSchemaFirstDirective = print ( schema . getDirective ( 'cost' ) . astNode ) ;
282+ const printedCodeFirstDirective = print ( builtCodeFirstSchema . getDirective ( 'cost' ) . astNode ) ;
283+
284+ expect ( printedSchemaFirstDirective ) . to . equal ( printedCodeFirstDirective ) ;
285+ } ) ;
248286} ) ;
0 commit comments