graphql/validation/custom-rules

Functions

NoDeprecatedCustomRule()

No deprecated

A GraphQL document is only valid if all selected fields and all used enum values have not been deprecated.

Note: This rule is optional and is not part of the Validation section of the GraphQL Specification. The main purpose of this rule is detection of deprecated usages and not necessarily to forbid their use when querying a service.

Signature:

NoDeprecatedCustomRule(context: ValidationContext): ASTVisitor

Arguments

NameTypeDescription
contextValidationContextThe validation context used while checking the document.

Returns

TypeDescription
ASTVisitorA visitor that reports validation errors for this rule.

Example

import { buildSchema, parse, validate } from 'graphql';
import { NoDeprecatedCustomRule } from 'graphql/validation';
 
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
  name: String
  oldName: String @deprecated(reason: "Use name")
  dog: Dog
  cat: Cat
  human(id: ID!): Human
  pets: [Pet]
  search(filter: Filter): String
  count(limit: Int): Int
}`);
 
const invalidDocument = parse(`
  { oldName }
`);
const invalidErrors = validate(schema, invalidDocument, [NoDeprecatedCustomRule]);
 
// invalidErrors.length: 1
 
const validDocument = parse(`
  { name }
`);
const validErrors = validate(schema, validDocument, [NoDeprecatedCustomRule]);
 
// validErrors.length: 0

NoSchemaIntrospectionCustomRule()

Prohibit introspection queries

A GraphQL document is only valid if all fields selected are not fields that return an introspection type.

Note: This rule is optional and is not part of the Validation section of the GraphQL Specification. This rule effectively disables introspection, which does not reflect best practices and should only be done if absolutely necessary.

Signature:

NoSchemaIntrospectionCustomRule(context: ValidationContext): ASTVisitor

Arguments

NameTypeDescription
contextValidationContextThe validation context used while checking the document.

Returns

TypeDescription
ASTVisitorA visitor that reports validation errors for this rule.

Example

import { buildSchema, parse, validate } from 'graphql';
import { NoSchemaIntrospectionCustomRule } from 'graphql/validation';
 
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
  name: String
  oldName: String @deprecated(reason: "Use name")
  dog: Dog
  cat: Cat
  human(id: ID!): Human
  pets: [Pet]
  search(filter: Filter): String
  count(limit: Int): Int
}`);
 
const invalidDocument = parse(`
  { __schema { queryType { name } } }
`);
const invalidErrors = validate(schema, invalidDocument, [NoSchemaIntrospectionCustomRule]);
 
// invalidErrors.length: 1
 
const validDocument = parse(`
  { name }
`);
const validErrors = validate(schema, validDocument, [NoSchemaIntrospectionCustomRule]);
 
// validErrors.length: 0