From d77d661e7369f1627d0729cbc87e3c98518e208f Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Fri, 18 Oct 2024 00:12:53 +0300 Subject: [PATCH] add permanent flag for schema configuration property assumeValid (#4244) closes https://github.com/graphql/graphql-js/issues/3448 --- src/type/__tests__/schema-test.ts | 31 +++++++++++++++++++++---------- src/type/schema.ts | 6 ++++-- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/type/__tests__/schema-test.ts b/src/type/__tests__/schema-test.ts index 5bd86e030a..0dedb01cc9 100644 --- a/src/type/__tests__/schema-test.ts +++ b/src/type/__tests__/schema-test.ts @@ -24,6 +24,7 @@ import { } from '../introspection.js'; import { GraphQLBoolean, GraphQLInt, GraphQLString } from '../scalars.js'; import { GraphQLSchema } from '../schema.js'; +import { validateSchema } from '../validate.js'; describe('Type System: Schema', () => { it('Define sample schema', () => { @@ -432,11 +433,21 @@ describe('Type System: Schema', () => { describe('Validity', () => { describe('when not assumed valid', () => { it('configures the schema to still needing validation', () => { - expect( - new GraphQLSchema({ - assumeValid: false, - }).__validationErrors, - ).to.equal(undefined); + const schema = new GraphQLSchema({ + assumeValid: false, + }); + expect(schema.assumeValid).to.equal(false); + expect(schema.__validationErrors).to.equal(undefined); + }); + + it('configures the schema to have required validation even once validated', () => { + const schema = new GraphQLSchema({ + assumeValid: false, + }); + const validationErrors = validateSchema(schema); + expect(validationErrors.length).to.be.greaterThan(0); + expect(validationErrors).to.equal(schema.__validationErrors); + expect(schema.assumeValid).to.equal(false); }); }); @@ -486,11 +497,11 @@ describe('Type System: Schema', () => { describe('when assumed valid', () => { it('configures the schema to have no errors', () => { - expect( - new GraphQLSchema({ - assumeValid: true, - }).__validationErrors, - ).to.deep.equal([]); + const schema = new GraphQLSchema({ + assumeValid: true, + }); + expect(schema.assumeValid).to.equal(true); + expect(schema.__validationErrors).to.deep.equal([]); }); }); }); diff --git a/src/type/schema.ts b/src/type/schema.ts index 2dd324b5fb..03d7422085 100644 --- a/src/type/schema.ts +++ b/src/type/schema.ts @@ -138,7 +138,7 @@ export class GraphQLSchema { astNode: Maybe; extensionASTNodes: ReadonlyArray; - // Used as a cache for validateSchema(). + assumeValid: boolean; __validationErrors: Maybe>; private _queryType: Maybe; @@ -159,6 +159,8 @@ export class GraphQLSchema { constructor(config: Readonly) { // If this schema was built from a source known to be valid, then it may be // marked with assumeValid to avoid an additional type system validation. + this.assumeValid = config.assumeValid ?? false; + // Used as a cache for validateSchema(). this.__validationErrors = config.assumeValid === true ? [] : undefined; this.description = config.description; @@ -384,7 +386,7 @@ export class GraphQLSchema { extensions: this.extensions, astNode: this.astNode, extensionASTNodes: this.extensionASTNodes, - assumeValid: this.__validationErrors !== undefined, + assumeValid: this.assumeValid, }; } }