Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add permanent flag for schema configuration property assumeValid #4244

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading