From 1d74208b021140d1bc44627c3a29a58f9ad552b1 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Mon, 16 May 2022 23:15:22 +0300 Subject: [PATCH] Revert "Remove deprecated `TypeInfo` argument of `validate` function" This reverts commit b4ad1282ce38449a953d9375307c9e45f8151a70 from #3574 For the context see #3519 --- src/validation/__tests__/validation-test.ts | 30 +++++++++++++++++++++ src/validation/validate.ts | 4 ++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/validation/__tests__/validation-test.ts b/src/validation/__tests__/validation-test.ts index 1ef00192f9..2d49f9335b 100644 --- a/src/validation/__tests__/validation-test.ts +++ b/src/validation/__tests__/validation-test.ts @@ -9,6 +9,7 @@ import type { DirectiveNode } from '../../language/ast'; import { parse } from '../../language/parser'; import { buildSchema } from '../../utilities/buildASTSchema'; +import { TypeInfo } from '../../utilities/TypeInfo'; import { validate } from '../validate'; import type { ValidationContext } from '../ValidationContext'; @@ -57,6 +58,35 @@ describe('Validate: Supports full validation', () => { ]); }); + it('Deprecated: validates using a custom TypeInfo', () => { + // This TypeInfo will never return a valid field. + const typeInfo = new TypeInfo(testSchema, null, () => null); + + const doc = parse(` + query { + human { + pets { + ... on Cat { + meowsVolume + } + ... on Dog { + barkVolume + } + } + } + } + `); + + const errors = validate(testSchema, doc, undefined, undefined, typeInfo); + const errorMessages = errors.map((error) => error.message); + + expect(errorMessages).to.deep.equal([ + 'Cannot query field "human" on type "QueryRoot". Did you mean "human"?', + 'Cannot query field "meowsVolume" on type "Cat". Did you mean "meowsVolume"?', + 'Cannot query field "barkVolume" on type "Dog". Did you mean "barkVolume"?', + ]); + }); + it('validates using a custom rule', () => { const schema = buildSchema(` directive @custom(arg: String) on FIELD diff --git a/src/validation/validate.ts b/src/validation/validate.ts index 7fb62736d4..61ec1412a2 100644 --- a/src/validation/validate.ts +++ b/src/validation/validate.ts @@ -40,6 +40,9 @@ export function validate( documentAST: DocumentNode, rules: ReadonlyArray = specifiedRules, options?: { maxErrors?: number }, + + /** @deprecated will be removed in 17.0.0 */ + typeInfo: TypeInfo = new TypeInfo(schema), ): ReadonlyArray { const maxErrors = options?.maxErrors ?? 100; @@ -49,7 +52,6 @@ export function validate( const abortObj = Object.freeze({}); const errors: Array = []; - const typeInfo = new TypeInfo(schema); const context = new ValidationContext( schema, documentAST,