1717package com .introproventures .graphql .jpa .query .schema ;
1818
1919import 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 ;
2022
21- import java .util .stream . Collectors ;
23+ import java .util .Optional ;
2224
2325import javax .persistence .EntityManager ;
2426
2527import com .introproventures .graphql .jpa .query .schema .impl .GraphQLJpaSchemaBuilder ;
2628import graphql .schema .GraphQLFieldDefinition ;
29+ import graphql .schema .GraphQLList ;
2730import graphql .schema .GraphQLSchema ;
2831import org .junit .Before ;
2932import org .junit .Test ;
@@ -99,18 +102,13 @@ public void correctlyDerivesToManyOptionalFromGivenEntities() {
99102 .isNotNull ();
100103
101104 //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 );
114112 }
115113
116114 @ Test
@@ -124,20 +122,45 @@ public void correctlyDerivesToOneOptionalFromGivenEntities() {
124122 .isNotNull ();
125123
126124 //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 );
139132 }
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+
141164 @ Test
142165 public void testBuildSchema (){
143166 //given
@@ -146,5 +169,18 @@ public void testBuildSchema(){
146169 //then
147170 assertThat (schema ).isNotNull ();
148171 }
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+
150186}
0 commit comments