Skip to content
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
36 changes: 36 additions & 0 deletions src/utilities/__tests__/buildASTSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ describe('Schema Builder', () => {
});
});

it('Ignores non-type system definitions', () => {
const sdl = `
type Query {
str: String
}

fragment SomeFragment on Query {
str
}
`;
expect(() => buildSchema(sdl)).to.not.throw();
});

it('Empty type', () => {
const sdl = dedent`
type EmptyType
Expand Down Expand Up @@ -875,4 +888,27 @@ describe('Schema Builder', () => {
buildSchema(sdl, { assumeValid: true });
buildSchema(sdl, { assumeValidSDL: true });
});

it('Throws on unknown types', () => {
const sdl = `
type Query {
unknown: UnknownType
}
`;
expect(() => buildSchema(sdl, { assumeValidSDL: true })).to.throw(
'Type "UnknownType" not found in document.',
);
});

it('Rejects invalid AST', () => {
// $DisableFlowOnNegativeTest
expect(() => buildASTSchema(null)).to.throw(
'Must provide valid Document AST',
);

// $DisableFlowOnNegativeTest
expect(() => buildASTSchema({})).to.throw(
'Must provide valid Document AST',
);
});
});
40 changes: 40 additions & 0 deletions src/utilities/__tests__/extendSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,32 @@ describe('extendSchema', () => {
extendSchema(schema, extendAST, { assumeValidSDL: true });
});

it('Throws on unknown types', () => {
const schema = new GraphQLSchema({});
const ast = parse(`
type Query {
unknown: UnknownType
}
`);
expect(() => extendSchema(schema, ast, { assumeValidSDL: true })).to.throw(
'Unknown type: "UnknownType".',
);
});

it('Rejects invalid AST', () => {
const schema = new GraphQLSchema({});

// $DisableFlowOnNegativeTest
expect(() => extendSchema(schema, null)).to.throw(
'Must provide valid Document AST',
);

// $DisableFlowOnNegativeTest
expect(() => extendSchema(schema, {})).to.throw(
'Must provide valid Document AST',
);
});

it('does not allow replacing a default directive', () => {
const schema = new GraphQLSchema({});
const extendAST = parse(`
Expand Down Expand Up @@ -1231,6 +1257,20 @@ describe('extendSchema', () => {
expect(printExtensionNodes(extendedSchema)).to.equal(extensionSDL);
});

it('adds directive via schema extension', () => {
const schema = buildSchema(`
type Query

directive @foo on SCHEMA
`);
const extensionSDL = dedent`
extend schema @foo
`;
const extendedSchema = extendSchema(schema, parse(extensionSDL));

expect(printExtensionNodes(extendedSchema)).to.equal(extensionSDL);
});

it('adds multiple new root types via schema extension', () => {
const schema = buildSchema('type Query');
const extendAST = parse(`
Expand Down
5 changes: 2 additions & 3 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ export function extendSchema(
subscription:
schemaConfig.subscription && replaceNamedType(schemaConfig.subscription),
// Then, incorporate schema definition and all schema extensions.
...astBuilder.getOperationTypes(
concatMaybeArrays(schemaDef && [schemaDef], schemaExtensions) || [],
),
...(schemaDef && astBuilder.getOperationTypes([schemaDef])),
...astBuilder.getOperationTypes(schemaExtensions),
};

// Then produce and return a Schema with these types.
Expand Down