Skip to content

Commit 6652178

Browse files
schema: Always validate basic config structure (#2408)
It introduces small perfomance penalty but simplify writing code around GraphQLSchema a lot. Also good long term solution would be to have separate dev and production (without `devAssert` checks) builds.
1 parent 443b108 commit 6652178

File tree

2 files changed

+13
-36
lines changed

2 files changed

+13
-36
lines changed

src/type/__tests__/schema-test.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -333,24 +333,6 @@ describe('Type System: Schema', () => {
333333
}).__validationErrors,
334334
).to.deep.equal([]);
335335
});
336-
337-
it('does not check the configuration for mistakes', () => {
338-
const config = [];
339-
// $DisableFlowOnNegativeTest
340-
config.assumeValid = true;
341-
// $DisableFlowOnNegativeTest
342-
expect(() => new GraphQLSchema(config)).to.not.throw();
343-
344-
expect(
345-
() =>
346-
// $DisableFlowOnNegativeTest
347-
new GraphQLSchema({
348-
assumeValid: true,
349-
types: {},
350-
directives: { reduce: () => [] },
351-
}),
352-
).to.not.throw();
353-
});
354336
});
355337
});
356338
});

src/type/schema.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,19 @@ export class GraphQLSchema {
138138
constructor(config: $ReadOnly<GraphQLSchemaConfig>): void {
139139
// If this schema was built from a source known to be valid, then it may be
140140
// marked with assumeValid to avoid an additional type system validation.
141-
if (config.assumeValid === true) {
142-
this.__validationErrors = [];
143-
} else {
144-
this.__validationErrors = undefined;
145-
146-
// Otherwise check for common mistakes during construction to produce
147-
// clear and early error messages.
148-
devAssert(isObjectLike(config), 'Must provide configuration object.');
149-
devAssert(
150-
!config.types || Array.isArray(config.types),
151-
`"types" must be Array if provided but got: ${inspect(config.types)}.`,
152-
);
153-
devAssert(
154-
!config.directives || Array.isArray(config.directives),
155-
'"directives" must be Array if provided but got: ' +
156-
`${inspect(config.directives)}.`,
157-
);
158-
}
141+
this.__validationErrors = config.assumeValid === true ? [] : undefined;
142+
143+
// Check for common mistakes during construction to produce early errors.
144+
devAssert(isObjectLike(config), 'Must provide configuration object.');
145+
devAssert(
146+
!config.types || Array.isArray(config.types),
147+
`"types" must be Array if provided but got: ${inspect(config.types)}.`,
148+
);
149+
devAssert(
150+
!config.directives || Array.isArray(config.directives),
151+
'"directives" must be Array if provided but got: ' +
152+
`${inspect(config.directives)}.`,
153+
);
159154

160155
this.extensions = config.extensions && toObjMap(config.extensions);
161156
this.astNode = config.astNode;

0 commit comments

Comments
 (0)