| 
3 | 3 | import { expect } from 'chai';  | 
4 | 4 | import { describe, it } from 'mocha';  | 
5 | 5 | 
 
  | 
 | 6 | +import { GraphQLError } from '../../error/GraphQLError';  | 
 | 7 | + | 
6 | 8 | import { parse } from '../../language/parser';  | 
 | 9 | + | 
7 | 10 | import { TypeInfo } from '../../utilities/TypeInfo';  | 
 | 11 | +import { buildSchema } from '../../utilities/buildASTSchema';  | 
8 | 12 | 
 
  | 
9 | 13 | import { validate } from '../validate';  | 
10 | 14 | 
 
  | 
@@ -78,6 +82,43 @@ describe('Validate: Supports full validation', () => {  | 
78 | 82 |       'Cannot query field "isHouseTrained" on type "Dog". Did you mean "isHouseTrained"?',  | 
79 | 83 |     ]);  | 
80 | 84 |   });  | 
 | 85 | + | 
 | 86 | +  it('validates using a custom rule', () => {  | 
 | 87 | +    const schema = buildSchema(`  | 
 | 88 | +      directive @custom(arg: String) on FIELD  | 
 | 89 | +
  | 
 | 90 | +      type Query {  | 
 | 91 | +        foo: String  | 
 | 92 | +      }  | 
 | 93 | +    `);  | 
 | 94 | + | 
 | 95 | +    const doc = parse(`  | 
 | 96 | +      query {  | 
 | 97 | +        name @custom  | 
 | 98 | +      }  | 
 | 99 | +    `);  | 
 | 100 | + | 
 | 101 | +    function customRule(context) {  | 
 | 102 | +      return {  | 
 | 103 | +        Directive(node) {  | 
 | 104 | +          const directiveDef = context.getDirective();  | 
 | 105 | +          const error = new GraphQLError(  | 
 | 106 | +            'Reporting directive: ' + String(directiveDef),  | 
 | 107 | +            node,  | 
 | 108 | +          );  | 
 | 109 | +          context.reportError(error);  | 
 | 110 | +        },  | 
 | 111 | +      };  | 
 | 112 | +    }  | 
 | 113 | + | 
 | 114 | +    const errors = validate(schema, doc, [customRule]);  | 
 | 115 | +    expect(errors).to.deep.equal([  | 
 | 116 | +      {  | 
 | 117 | +        message: 'Reporting directive: @custom',  | 
 | 118 | +        locations: [{ line: 3, column: 14 }],  | 
 | 119 | +      },  | 
 | 120 | +    ]);  | 
 | 121 | +  });  | 
81 | 122 | });  | 
82 | 123 | 
 
  | 
83 | 124 | describe('Validate: Limit maximum number of validation errors', () => {  | 
 | 
0 commit comments