Skip to content

Commit 958eb96

Browse files
Add missing assertDirective and assertSchema (#1580)
1 parent 8991208 commit 958eb96

File tree

8 files changed

+37
-16
lines changed

8 files changed

+37
-16
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ export {
104104
isIntrospectionType,
105105
isSpecifiedDirective,
106106
// Assertions
107+
assertSchema,
108+
assertDirective,
107109
assertType,
108110
assertScalarType,
109111
assertObjectType,

src/type/directives.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import defineToStringTag from '../jsutils/defineToStringTag';
1717
import defineToJSON from '../jsutils/defineToJSON';
1818
import instanceOf from '../jsutils/instanceOf';
1919
import invariant from '../jsutils/invariant';
20+
import inspect from '../jsutils/inspect';
2021
import type { DirectiveDefinitionNode } from '../language/ast';
2122
import {
2223
DirectiveLocation,
@@ -34,6 +35,14 @@ export function isDirective(directive) {
3435
return instanceOf(directive, GraphQLDirective);
3536
}
3637

38+
export function assertDirective(directive: mixed): GraphQLDirective {
39+
invariant(
40+
isDirective(directive),
41+
`Expected ${inspect(directive)} to be a GraphQL directive.`,
42+
);
43+
return directive;
44+
}
45+
3746
/**
3847
* Directives are used by the GraphQL runtime as a way of modifying execution
3948
* behavior. Type system creators will usually not create these directly.

src/type/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
export {
1111
// Predicate
1212
isSchema,
13+
// Assertion
14+
assertSchema,
1315
// GraphQL Schema definition
1416
GraphQLSchema,
1517
} from './schema';
@@ -73,6 +75,8 @@ export {
7375
export {
7476
// Predicate
7577
isDirective,
78+
// Assertion
79+
assertDirective,
7680
// Directives Definition
7781
GraphQLDirective,
7882
// Built-in Directives defined by the Spec

src/type/schema.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ export function isSchema(schema) {
5050
return instanceOf(schema, GraphQLSchema);
5151
}
5252

53+
export function assertSchema(schema: mixed): GraphQLSchema {
54+
invariant(
55+
isSchema(schema),
56+
`Expected ${inspect(schema)} to be a GraphQL schema.`,
57+
);
58+
return schema;
59+
}
60+
5361
/**
5462
* Schema Definition
5563
*

src/type/validate.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ import type {
2828
import { isDirective } from './directives';
2929
import type { GraphQLDirective } from './directives';
3030
import { isIntrospectionType } from './introspection';
31-
import { isSchema } from './schema';
31+
import { assertSchema } from './schema';
3232
import type { GraphQLSchema } from './schema';
3333
import inspect from '../jsutils/inspect';
3434
import find from '../jsutils/find';
35-
import invariant from '../jsutils/invariant';
3635
import objectValues from '../jsutils/objectValues';
3736
import { GraphQLError } from '../error/GraphQLError';
3837
import type {
@@ -57,10 +56,7 @@ export function validateSchema(
5756
schema: GraphQLSchema,
5857
): $ReadOnlyArray<GraphQLError> {
5958
// First check to ensure the provided value is in fact a GraphQLSchema.
60-
invariant(
61-
isSchema(schema),
62-
`Expected ${inspect(schema)} to be a GraphQL schema.`,
63-
);
59+
assertSchema(schema);
6460

6561
// If this Schema has already been validated, return the previous results.
6662
if (schema.__validationErrors) {

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { buildASTSchema, buildSchema } from '../buildASTSchema';
1616
import dedent from '../../jsutils/dedent';
1717
import { Kind } from '../../language/kinds';
1818
import {
19+
assertDirective,
1920
assertObjectType,
2021
assertInputObjectType,
2122
assertEnumType,
@@ -716,8 +717,7 @@ describe('Schema Builder', () => {
716717
const testInterface = assertInterfaceType(schema.getType('TestInterface'));
717718
const testType = assertObjectType(schema.getType('TestType'));
718719
const testScalar = assertScalarType(schema.getType('TestScalar'));
719-
const testDirective = schema.getDirective('test');
720-
invariant(testDirective);
720+
const testDirective = assertDirective(schema.getDirective('test'));
721721

722722
const restoredSchemaAST = {
723723
kind: Kind.DOCUMENT,

src/utilities/__tests__/extendSchema-test.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { printSchema } from '../schemaPrinter';
1717
import { Kind } from '../../language/kinds';
1818
import { graphqlSync } from '../../';
1919
import {
20+
assertDirective,
2021
assertObjectType,
2122
assertInputObjectType,
2223
assertEnumType,
@@ -325,8 +326,7 @@ describe('extendSchema', () => {
325326
const inputField = queryType.getFields().someInput;
326327
expect(inputField).to.have.nested.property('args[0].type', someInputType);
327328

328-
const fooDirective = extendedSchema.getDirective('foo');
329-
invariant(fooDirective);
329+
const fooDirective = assertDirective(extendedSchema.getDirective('foo'));
330330
expect(fooDirective.args[0].type).to.equal(someInputType);
331331
});
332332

@@ -429,8 +429,9 @@ describe('extendSchema', () => {
429429
const testInterface = assertInterfaceType(
430430
extendedTwiceSchema.getType('TestInterface'),
431431
);
432-
const testDirective = extendedTwiceSchema.getDirective('test');
433-
invariant(testDirective);
432+
const testDirective = assertDirective(
433+
extendedTwiceSchema.getDirective('test'),
434+
);
434435

435436
expect(testType).to.include({ extensionASTNodes: undefined });
436437
expect(testEnum).to.include({ extensionASTNodes: undefined });
@@ -1051,8 +1052,9 @@ describe('extendSchema', () => {
10511052
directive @profile(enable: Boolean! tag: String) on QUERY | FIELD
10521053
`);
10531054

1054-
const extendedDirective = extendedSchema.getDirective('profile');
1055-
invariant(extendedDirective);
1055+
const extendedDirective = assertDirective(
1056+
extendedSchema.getDirective('profile'),
1057+
);
10561058
expect(extendedDirective.locations).to.deep.equal(['QUERY', 'FIELD']);
10571059

10581060
expect(extendedDirective.args).to.have.lengthOf(2);

src/utilities/extendSchema.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import objectValues from '../jsutils/objectValues';
1414
import { ASTDefinitionBuilder } from './buildASTSchema';
1515
import { assertValidSDLExtension } from '../validation/validate';
1616
import { GraphQLError } from '../error/GraphQLError';
17-
import { isSchema, GraphQLSchema } from '../type/schema';
17+
import { assertSchema, GraphQLSchema } from '../type/schema';
1818
import { isIntrospectionType } from '../type/introspection';
1919
import { isSpecifiedScalarType } from '../type/scalars';
2020

@@ -105,7 +105,7 @@ export function extendSchema(
105105
documentAST: DocumentNode,
106106
options?: Options,
107107
): GraphQLSchema {
108-
invariant(isSchema(schema), 'Must provide valid GraphQLSchema');
108+
assertSchema(schema);
109109

110110
invariant(
111111
documentAST && documentAST.kind === Kind.DOCUMENT,

0 commit comments

Comments
 (0)