Skip to content

Commit fd0a3aa

Browse files
buildSchema/extendSchema: improve test coverage (#2310)
1 parent 94b7e72 commit fd0a3aa

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ describe('Schema Builder', () => {
8888
});
8989
});
9090

91+
it('Ignores non-type system definitions', () => {
92+
const sdl = `
93+
type Query {
94+
str: String
95+
}
96+
97+
fragment SomeFragment on Query {
98+
str
99+
}
100+
`;
101+
expect(() => buildSchema(sdl)).to.not.throw();
102+
});
103+
91104
it('Empty type', () => {
92105
const sdl = dedent`
93106
type EmptyType
@@ -875,4 +888,27 @@ describe('Schema Builder', () => {
875888
buildSchema(sdl, { assumeValid: true });
876889
buildSchema(sdl, { assumeValidSDL: true });
877890
});
891+
892+
it('Throws on unknown types', () => {
893+
const sdl = `
894+
type Query {
895+
unknown: UnknownType
896+
}
897+
`;
898+
expect(() => buildSchema(sdl, { assumeValidSDL: true })).to.throw(
899+
'Type "UnknownType" not found in document.',
900+
);
901+
});
902+
903+
it('Rejects invalid AST', () => {
904+
// $DisableFlowOnNegativeTest
905+
expect(() => buildASTSchema(null)).to.throw(
906+
'Must provide valid Document AST',
907+
);
908+
909+
// $DisableFlowOnNegativeTest
910+
expect(() => buildASTSchema({})).to.throw(
911+
'Must provide valid Document AST',
912+
);
913+
});
878914
});

src/utilities/__tests__/extendSchema-test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,32 @@ describe('extendSchema', () => {
11581158
extendSchema(schema, extendAST, { assumeValidSDL: true });
11591159
});
11601160

1161+
it('Throws on unknown types', () => {
1162+
const schema = new GraphQLSchema({});
1163+
const ast = parse(`
1164+
type Query {
1165+
unknown: UnknownType
1166+
}
1167+
`);
1168+
expect(() => extendSchema(schema, ast, { assumeValidSDL: true })).to.throw(
1169+
'Unknown type: "UnknownType".',
1170+
);
1171+
});
1172+
1173+
it('Rejects invalid AST', () => {
1174+
const schema = new GraphQLSchema({});
1175+
1176+
// $DisableFlowOnNegativeTest
1177+
expect(() => extendSchema(schema, null)).to.throw(
1178+
'Must provide valid Document AST',
1179+
);
1180+
1181+
// $DisableFlowOnNegativeTest
1182+
expect(() => extendSchema(schema, {})).to.throw(
1183+
'Must provide valid Document AST',
1184+
);
1185+
});
1186+
11611187
it('does not allow replacing a default directive', () => {
11621188
const schema = new GraphQLSchema({});
11631189
const extendAST = parse(`
@@ -1231,6 +1257,20 @@ describe('extendSchema', () => {
12311257
expect(printExtensionNodes(extendedSchema)).to.equal(extensionSDL);
12321258
});
12331259

1260+
it('adds directive via schema extension', () => {
1261+
const schema = buildSchema(`
1262+
type Query
1263+
1264+
directive @foo on SCHEMA
1265+
`);
1266+
const extensionSDL = dedent`
1267+
extend schema @foo
1268+
`;
1269+
const extendedSchema = extendSchema(schema, parse(extensionSDL));
1270+
1271+
expect(printExtensionNodes(extendedSchema)).to.equal(extensionSDL);
1272+
});
1273+
12341274
it('adds multiple new root types via schema extension', () => {
12351275
const schema = buildSchema('type Query');
12361276
const extendAST = parse(`

src/utilities/extendSchema.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,8 @@ export function extendSchema(
169169
subscription:
170170
schemaConfig.subscription && replaceNamedType(schemaConfig.subscription),
171171
// Then, incorporate schema definition and all schema extensions.
172-
...astBuilder.getOperationTypes(
173-
concatMaybeArrays(schemaDef && [schemaDef], schemaExtensions) || [],
174-
),
172+
...(schemaDef && astBuilder.getOperationTypes([schemaDef])),
173+
...astBuilder.getOperationTypes(schemaExtensions),
175174
};
176175

177176
// Then produce and return a Schema with these types.

0 commit comments

Comments
 (0)