Reference
Validation

Validation

Validation refers to the process of validating a GraphQL document against a GraphQL schema and a set of rules to ensure that it is unambiguous and mistake‐free.

See the validation specification to learn more.

Custom validation function

Benzene allows custom validation function like below:

const GQL = new Benzene({
  validateFn(schema, ast, rules) {
    // Return an array of errors in case of validation failures or an empty one otherwise
  },
});

If defined, this will be used instead of the validate function from graphql-js.

Custom validation rules

We can also provide a set of custom validation rules that would be used in the validation function.

const customValidationRules = [depthLimit, queryComplexity];
const GQL = new Benzene({
  validationRules: customValidationRules,
});
⚠️

Setting custom validation rules can make our implementation incompatible with the GraphQL specs. Therefore, it is best to add it in addition to the specifiedRules from graphql-js.

We should always include specifiedRules from graphql-js in addition to our custom rules to be spec-compliant.

import { specifiedRules } from "graphql";
 
const GQL = new Benzene({
  validationRules: [...specifiedRules, ...customValidationRules],
});