17
17
package com .introproventures .graphql .jpa .query .schema ;
18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .assertj .core .api .BDDAssertions .then ;
21
+ import static org .assertj .core .api .BDDAssertions .thenCode ;
20
22
21
- import java .util .stream . Collectors ;
23
+ import java .util .Optional ;
22
24
23
25
import javax .persistence .EntityManager ;
24
26
25
27
import com .introproventures .graphql .jpa .query .schema .impl .GraphQLJpaSchemaBuilder ;
26
28
import graphql .schema .GraphQLFieldDefinition ;
29
+ import graphql .schema .GraphQLList ;
27
30
import graphql .schema .GraphQLSchema ;
28
31
import org .junit .Before ;
29
32
import org .junit .Test ;
@@ -99,18 +102,13 @@ public void correctlyDerivesToManyOptionalFromGivenEntities() {
99
102
.isNotNull ();
100
103
101
104
//then
102
- assertThat (schema .getQueryType ()
103
- .getFieldDefinition ("Book" )
104
- .getType ()
105
- .getChildren ()
106
- .stream ()
107
- .map (GraphQLFieldDefinition .class ::cast )
108
- .collect (Collectors .toList ())
109
- )
110
- .filteredOn ("name" , "author" )
111
- .extracting (it -> it .getArgument ("optional" ))
112
- .extractingResultOf ("getDefaultValue" , Object .class )
113
- .containsExactly (new Boolean (false ));
105
+ assertThat (getFieldForType ("author" ,
106
+ "Book" ,
107
+ schema ))
108
+ .isPresent ().get ()
109
+ .extracting (it -> it .getArgument ("optional" ))
110
+ .extracting ("defaultValue" )
111
+ .containsExactly (Boolean .FALSE );
114
112
}
115
113
116
114
@ Test
@@ -124,20 +122,45 @@ public void correctlyDerivesToOneOptionalFromGivenEntities() {
124
122
.isNotNull ();
125
123
126
124
//then
127
- assertThat (schema .getQueryType ()
128
- .getFieldDefinition ("Author" )
129
- .getType ()
130
- .getChildren ()
131
- .stream ()
132
- .map (GraphQLFieldDefinition .class ::cast )
133
- .collect (Collectors .toList ())
134
- )
135
- .filteredOn ("name" , "books" )
136
- .extracting (it -> it .getArgument ("optional" ))
137
- .extractingResultOf ("getDefaultValue" , Object .class )
138
- .containsExactly (new Boolean (true ));
125
+ assertThat (getFieldForType ("books" ,
126
+ "Author" ,
127
+ schema ))
128
+ .isPresent ().get ()
129
+ .extracting (it -> it .getArgument ("optional" ))
130
+ .extracting ("defaultValue" )
131
+ .containsExactly (Boolean .TRUE );
139
132
}
140
-
133
+
134
+ @ Test
135
+ public void shouldBuildSchemaWithStringArrayAsStringListType () {
136
+ //given
137
+ //there is a property in the model that is of array type
138
+
139
+ //when
140
+ GraphQLSchema schema = builder .build ();
141
+
142
+ //then
143
+ Optional <GraphQLFieldDefinition > tags = getFieldForType ("tags" ,
144
+ "SuperBook" ,
145
+ schema );
146
+ then (tags )
147
+ .isPresent ().get ()
148
+ .extracting (GraphQLFieldDefinition ::getType )
149
+ .isInstanceOf (GraphQLList .class )
150
+ .extracting ("wrappedType" )
151
+ .extracting ("name" )
152
+ .containsOnly ("String" );
153
+ }
154
+
155
+ @ Test
156
+ public void shouldBuildSchemaWithStringArrayAsStringListTypeWithoutAnyError () {
157
+ //given
158
+ //there is a property in the model that is of array type
159
+
160
+ //then
161
+ thenCode (() -> builder .build ()).doesNotThrowAnyException ();
162
+ }
163
+
141
164
@ Test
142
165
public void testBuildSchema (){
143
166
//given
@@ -146,5 +169,18 @@ public void testBuildSchema(){
146
169
//then
147
170
assertThat (schema ).isNotNull ();
148
171
}
149
-
172
+
173
+ private Optional <GraphQLFieldDefinition > getFieldForType (String fieldName ,
174
+ String type ,
175
+ GraphQLSchema schema ) {
176
+ return schema .getQueryType ()
177
+ .getFieldDefinition (type )
178
+ .getType ()
179
+ .getChildren ()
180
+ .stream ()
181
+ .map (GraphQLFieldDefinition .class ::cast )
182
+ .filter (graphQLFieldDefinition -> graphQLFieldDefinition .getName ().equals (fieldName ))
183
+ .findFirst ();
184
+ }
185
+
150
186
}
0 commit comments