1
+ import '@aws-cdk/assert/jest' ;
2
+ import * as cdk from '@aws-cdk/core' ;
3
+ import * as appsync from '../lib' ;
4
+ import * as t from './scalar-type-defintions' ;
5
+
6
+ const out = 'type Test1 {\n test1: String\n}\ntype Test2 {\n test2: String\n}\nunion UnionTest = Test1 | Test2\n' ;
7
+ const test1 = new appsync . ObjectType ( 'Test1' , {
8
+ definition : { test1 : t . string } ,
9
+ } ) ;
10
+ const test2 = new appsync . ObjectType ( 'Test2' , {
11
+ definition : { test2 : t . string } ,
12
+ } ) ;
13
+ let stack : cdk . Stack ;
14
+ let api : appsync . GraphqlApi ;
15
+ beforeEach ( ( ) => {
16
+ // GIVEN
17
+ stack = new cdk . Stack ( ) ;
18
+ api = new appsync . GraphqlApi ( stack , 'api' , {
19
+ name : 'api' ,
20
+ } ) ;
21
+ api . addType ( test1 ) ;
22
+ api . addType ( test2 ) ;
23
+ } ) ;
24
+
25
+ describe ( 'testing Union Type properties' , ( ) => {
26
+ test ( 'UnionType configures properly' , ( ) => {
27
+ // WHEN
28
+ const union = new appsync . UnionType ( 'UnionTest' , {
29
+ definition : [ test1 , test2 ] ,
30
+ } ) ;
31
+ api . addType ( union ) ;
32
+ // THEN
33
+ expect ( stack ) . toHaveResourceLike ( 'AWS::AppSync::GraphQLSchema' , {
34
+ Definition : `${ out } ` ,
35
+ } ) ;
36
+ expect ( stack ) . not . toHaveResource ( 'AWS::AppSync::Resolver' ) ;
37
+ } ) ;
38
+
39
+ test ( 'UnionType can addField' , ( ) => {
40
+ // WHEN
41
+ const union = new appsync . UnionType ( 'UnionTest' , {
42
+ definition : [ test1 ] ,
43
+ } ) ;
44
+ api . addType ( union ) ;
45
+ union . addField ( { field : test2 . attribute ( ) } ) ;
46
+
47
+ // THEN
48
+ expect ( stack ) . toHaveResourceLike ( 'AWS::AppSync::GraphQLSchema' , {
49
+ Definition : `${ out } ` ,
50
+ } ) ;
51
+ } ) ;
52
+
53
+ test ( 'UnionType errors when addField is configured with fieldName option' , ( ) => {
54
+ // WHEN
55
+ const union = new appsync . UnionType ( 'UnionTest' , {
56
+ definition : [ test1 ] ,
57
+ } ) ;
58
+ api . addType ( union ) ;
59
+
60
+ // THEN
61
+ expect ( ( ) => {
62
+ union . addField ( { fieldName : 'fail' , field : test2 . attribute ( ) } ) ;
63
+ } ) . toThrowError ( 'Union Types cannot be configured with the fieldName option. Use the field option instead.' ) ;
64
+ } ) ;
65
+
66
+ test ( 'UnionType errors when addField is not configured with field option' , ( ) => {
67
+ // WHEN
68
+ const union = new appsync . UnionType ( 'UnionTest' , {
69
+ definition : [ test1 ] ,
70
+ } ) ;
71
+ api . addType ( union ) ;
72
+
73
+ // THEN
74
+ expect ( ( ) => {
75
+ union . addField ( { } ) ;
76
+ } ) . toThrowError ( 'Union Types must be configured with the field option.' ) ;
77
+ } ) ;
78
+
79
+ test ( 'UnionType can be a GraphqlType' , ( ) => {
80
+ // WHEN
81
+ const union = new appsync . UnionType ( 'UnionTest' , {
82
+ definition : [ test1 , test2 ] ,
83
+ } ) ;
84
+ api . addType ( union ) ;
85
+
86
+ api . addType ( new appsync . ObjectType ( 'Test2' , {
87
+ definition : { union : union . attribute ( ) } ,
88
+ } ) ) ;
89
+
90
+ const obj = 'type Test2 {\n union: UnionTest\n}\n' ;
91
+
92
+ // THEN
93
+ expect ( stack ) . toHaveResourceLike ( 'AWS::AppSync::GraphQLSchema' , {
94
+ Definition : `${ out } ${ obj } ` ,
95
+ } ) ;
96
+ } ) ;
97
+
98
+ test ( 'appsync errors when addField with Graphql Types' , ( ) => {
99
+ // WHEN
100
+ const test = new appsync . UnionType ( 'Test' , {
101
+ definition : [ ] ,
102
+ } ) ;
103
+ // THEN
104
+ expect ( ( ) => {
105
+ test . addField ( { field : t . string } ) ;
106
+ } ) . toThrowError ( 'Fields for Union Types must be Object Types.' ) ;
107
+ } ) ;
108
+
109
+ test ( 'appsync errors when addField with Field' , ( ) => {
110
+ // WHEN
111
+ const test = new appsync . UnionType ( 'Test' , {
112
+ definition : [ ] ,
113
+ } ) ;
114
+ // THEN
115
+ expect ( ( ) => {
116
+ test . addField ( { field : new appsync . Field ( { returnType : t . string } ) } ) ;
117
+ } ) . toThrowError ( 'Fields for Union Types must be Object Types.' ) ;
118
+ } ) ;
119
+
120
+ test ( 'appsync errors when addField with ResolvableField' , ( ) => {
121
+ // WHEN
122
+ const test = new appsync . UnionType ( 'Test' , {
123
+ definition : [ ] ,
124
+ } ) ;
125
+ // THEN
126
+ expect ( ( ) => {
127
+ test . addField ( { field : new appsync . ResolvableField ( { returnType : t . string } ) } ) ;
128
+ } ) . toThrowError ( 'Fields for Union Types must be Object Types.' ) ;
129
+ } ) ;
130
+
131
+ test ( 'appsync errors when addField with Interface Types' , ( ) => {
132
+ // WHEN
133
+ const test = new appsync . UnionType ( 'Test' , {
134
+ definition : [ ] ,
135
+ } ) ;
136
+ // THEN
137
+ expect ( ( ) => {
138
+ test . addField ( { field : new appsync . InterfaceType ( 'break' , { definition : { } } ) . attribute ( ) } ) ;
139
+ } ) . toThrowError ( 'Fields for Union Types must be Object Types.' ) ;
140
+ } ) ;
141
+
142
+ test ( 'appsync errors when addField with Union Types' , ( ) => {
143
+ // WHEN
144
+ const test = new appsync . UnionType ( 'Test' , {
145
+ definition : [ ] ,
146
+ } ) ;
147
+ // THEN
148
+ expect ( ( ) => {
149
+ test . addField ( { field : test . attribute ( ) } ) ;
150
+ } ) . toThrowError ( 'Fields for Union Types must be Object Types.' ) ;
151
+ } ) ;
152
+ } ) ;
0 commit comments