@@ -6,6 +6,7 @@ import { describe, it } from 'mocha';
6
6
import dedent from '../../jsutils/dedent' ;
7
7
import invariant from '../../jsutils/invariant' ;
8
8
9
+ import { Kind } from '../../language/kinds' ;
9
10
import { parse } from '../../language/parser' ;
10
11
import { print } from '../../language/printer' ;
11
12
@@ -34,7 +35,7 @@ import {
34
35
35
36
import { graphqlSync } from '../../graphql' ;
36
37
37
- import { printSchema } from '../schemaPrinter' ;
38
+ import { printType , printSchema } from '../schemaPrinter' ;
38
39
import { buildASTSchema , buildSchema } from '../buildASTSchema' ;
39
40
40
41
/**
@@ -54,6 +55,14 @@ function printASTNode(obj) {
54
55
return print ( obj . astNode ) ;
55
56
}
56
57
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
+
57
66
describe ( 'Schema Builder' , ( ) => {
58
67
it ( 'can use built schema for limited execution' , ( ) => {
59
68
const schema = buildASTSchema (
@@ -754,6 +763,168 @@ describe('Schema Builder', () => {
754
763
} ) ;
755
764
} ) ;
756
765
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
+
757
928
it ( 'Correctly assign AST nodes' , ( ) => {
758
929
const sdl = dedent `
759
930
schema {
0 commit comments