Skip to content

Commit 1283c84

Browse files
buildSchema/extendSchema: add support for extensions (#2248)
Fixes #922
1 parent fd0a3aa commit 1283c84

File tree

4 files changed

+253
-21
lines changed

4 files changed

+253
-21
lines changed

src/utilities/__tests__/buildASTSchema-test.js

+172-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { describe, it } from 'mocha';
66
import dedent from '../../jsutils/dedent';
77
import invariant from '../../jsutils/invariant';
88

9+
import { Kind } from '../../language/kinds';
910
import { parse } from '../../language/parser';
1011
import { print } from '../../language/printer';
1112

@@ -34,7 +35,7 @@ import {
3435

3536
import { graphqlSync } from '../../graphql';
3637

37-
import { printSchema } from '../schemaPrinter';
38+
import { printType, printSchema } from '../schemaPrinter';
3839
import { buildASTSchema, buildSchema } from '../buildASTSchema';
3940

4041
/**
@@ -54,6 +55,14 @@ function printASTNode(obj) {
5455
return print(obj.astNode);
5556
}
5657

58+
function printAllASTNodes(obj) {
59+
invariant(obj.astNode != null && obj.extensionASTNodes != null);
60+
return print({
61+
kind: Kind.DOCUMENT,
62+
definitions: [obj.astNode, ...obj.extensionASTNodes],
63+
});
64+
}
65+
5766
describe('Schema Builder', () => {
5867
it('can use built schema for limited execution', () => {
5968
const schema = buildASTSchema(
@@ -754,6 +763,168 @@ describe('Schema Builder', () => {
754763
});
755764
});
756765

766+
it('Correctly extend scalar type', () => {
767+
const scalarSDL = dedent`
768+
scalar SomeScalar
769+
770+
extend scalar SomeScalar @foo
771+
772+
extend scalar SomeScalar @bar
773+
`;
774+
const schema = buildSchema(`
775+
${scalarSDL}
776+
directive @foo on SCALAR
777+
directive @bar on SCALAR
778+
`);
779+
780+
const someScalar = assertScalarType(schema.getType('SomeScalar'));
781+
expect(printType(someScalar) + '\n').to.equal(dedent`
782+
scalar SomeScalar
783+
`);
784+
785+
expect(printAllASTNodes(someScalar)).to.equal(scalarSDL);
786+
});
787+
788+
it('Correctly extend object type', () => {
789+
const objectSDL = dedent`
790+
type SomeObject implements Foo {
791+
first: String
792+
}
793+
794+
extend type SomeObject implements Bar {
795+
second: Int
796+
}
797+
798+
extend type SomeObject implements Baz {
799+
third: Float
800+
}
801+
`;
802+
const schema = buildSchema(`
803+
${objectSDL}
804+
interface Foo
805+
interface Bar
806+
interface Baz
807+
`);
808+
809+
const someObject = assertObjectType(schema.getType('SomeObject'));
810+
expect(printType(someObject) + '\n').to.equal(dedent`
811+
type SomeObject implements Foo & Bar & Baz {
812+
first: String
813+
second: Int
814+
third: Float
815+
}
816+
`);
817+
818+
expect(printAllASTNodes(someObject)).to.equal(objectSDL);
819+
});
820+
821+
it('Correctly extend interface type', () => {
822+
const interfaceSDL = dedent`
823+
interface SomeInterface {
824+
first: String
825+
}
826+
827+
extend interface SomeInterface {
828+
second: Int
829+
}
830+
831+
extend interface SomeInterface {
832+
third: Float
833+
}
834+
`;
835+
const schema = buildSchema(interfaceSDL);
836+
837+
const someInterface = assertInterfaceType(schema.getType('SomeInterface'));
838+
expect(printType(someInterface) + '\n').to.equal(dedent`
839+
interface SomeInterface {
840+
first: String
841+
second: Int
842+
third: Float
843+
}
844+
`);
845+
846+
expect(printAllASTNodes(someInterface)).to.equal(interfaceSDL);
847+
});
848+
849+
it('Correctly extend union type', () => {
850+
const unionSDL = dedent`
851+
union SomeUnion = FirstType
852+
853+
extend union SomeUnion = SecondType
854+
855+
extend union SomeUnion = ThirdType
856+
`;
857+
const schema = buildSchema(`
858+
${unionSDL}
859+
type FirstType
860+
type SecondType
861+
type ThirdType
862+
`);
863+
864+
const someUnion = assertUnionType(schema.getType('SomeUnion'));
865+
expect(printType(someUnion) + '\n').to.equal(dedent`
866+
union SomeUnion = FirstType | SecondType | ThirdType
867+
`);
868+
869+
expect(printAllASTNodes(someUnion)).to.equal(unionSDL);
870+
});
871+
872+
it('Correctly extend enum type', () => {
873+
const enumSDL = dedent`
874+
enum SomeEnum {
875+
FIRST
876+
}
877+
878+
extend enum SomeEnum {
879+
SECOND
880+
}
881+
882+
extend enum SomeEnum {
883+
THIRD
884+
}
885+
`;
886+
const schema = buildSchema(enumSDL);
887+
888+
const someEnum = assertEnumType(schema.getType('SomeEnum'));
889+
expect(printType(someEnum) + '\n').to.equal(dedent`
890+
enum SomeEnum {
891+
FIRST
892+
SECOND
893+
THIRD
894+
}
895+
`);
896+
897+
expect(printAllASTNodes(someEnum)).to.equal(enumSDL);
898+
});
899+
900+
it('Correctly extend input object type', () => {
901+
const inputSDL = dedent`
902+
input SomeInput {
903+
first: String
904+
}
905+
906+
extend input SomeInput {
907+
second: Int
908+
}
909+
910+
extend input SomeInput {
911+
third: Float
912+
}
913+
`;
914+
const schema = buildSchema(inputSDL);
915+
916+
const someInput = assertInputObjectType(schema.getType('SomeInput'));
917+
expect(printType(someInput) + '\n').to.equal(dedent`
918+
input SomeInput {
919+
first: String
920+
second: Int
921+
third: Float
922+
}
923+
`);
924+
925+
expect(printAllASTNodes(someInput)).to.equal(inputSDL);
926+
});
927+
757928
it('Correctly assign AST nodes', () => {
758929
const sdl = dedent`
759930
schema {

src/utilities/__tests__/extendSchema-test.js

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
assertScalarType,
3232
} from '../../type/definition';
3333

34+
import { concatAST } from '../concatAST';
3435
import { printSchema } from '../schemaPrinter';
3536
import { extendSchema } from '../extendSchema';
3637
import { buildSchema } from '../buildASTSchema';
@@ -419,6 +420,14 @@ describe('extendSchema', () => {
419420
secondExtensionAST,
420421
);
421422

423+
const extendedInOneGoSchema = extendSchema(
424+
schema,
425+
concatAST([firstExtensionAST, secondExtensionAST]),
426+
);
427+
expect(printSchema(extendedInOneGoSchema)).to.equal(
428+
printSchema(extendedTwiceSchema),
429+
);
430+
422431
const query = assertObjectType(extendedTwiceSchema.getType('Query'));
423432
const someEnum = assertEnumType(extendedTwiceSchema.getType('SomeEnum'));
424433
const someUnion = assertUnionType(extendedTwiceSchema.getType('SomeUnion'));

0 commit comments

Comments
 (0)