Skip to content

Commit

Permalink
add permanent flag for schema configuration property assumeValid
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Oct 17, 2024
1 parent 858aa32 commit 3626cc2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
31 changes: 21 additions & 10 deletions src/type/__tests__/schema-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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([]);
});
});
});
Expand Down
6 changes: 4 additions & 2 deletions src/type/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class GraphQLSchema {
astNode: Maybe<SchemaDefinitionNode>;
extensionASTNodes: ReadonlyArray<SchemaExtensionNode>;

// Used as a cache for validateSchema().
assumeValid: boolean;
__validationErrors: Maybe<ReadonlyArray<GraphQLError>>;

private _queryType: Maybe<GraphQLObjectType>;
Expand All @@ -159,6 +159,8 @@ export class GraphQLSchema {
constructor(config: Readonly<GraphQLSchemaConfig>) {
// 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;
Expand Down Expand Up @@ -384,7 +386,7 @@ export class GraphQLSchema {
extensions: this.extensions,
astNode: this.astNode,
extensionASTNodes: this.extensionASTNodes,
assumeValid: this.__validationErrors !== undefined,
assumeValid: this.assumeValid,
};
}
}
Expand Down

0 comments on commit 3626cc2

Please sign in to comment.