15
15
package graphql.servlet
16
16
17
17
import com.fasterxml.jackson.databind.ObjectMapper
18
- import graphql.GraphQLError
19
18
import graphql.Scalars
20
- import graphql.execution.SimpleExecutionStrategy
19
+ import graphql.execution.TypeInfo
21
20
import graphql.schema.DataFetcher
21
+ import graphql.schema.GraphQLFieldDefinition
22
+ import graphql.schema.GraphQLNonNull
22
23
import graphql.schema.GraphQLObjectType
23
24
import graphql.schema.GraphQLSchema
24
25
import org.springframework.mock.web.MockHttpServletRequest
25
26
import org.springframework.mock.web.MockHttpServletResponse
26
27
import spock.lang.Shared
27
28
import spock.lang.Specification
28
29
29
- import javax.servlet.http.HttpServletRequest
30
- import javax.servlet.http.HttpServletResponse
31
-
32
30
/**
33
31
* @author Andrew Potter
34
32
*/
@@ -55,29 +53,34 @@ class GraphQLServletSpec extends Specification {
55
53
def createServlet (DataFetcher queryDataFetcher = { env -> env. arguments. arg }, DataFetcher mutationDataFetcher = { env -> env. arguments. arg }) {
56
54
GraphQLObjectType query = GraphQLObjectType . newObject()
57
55
.name(" Query" )
58
- .field { field ->
59
- field. name(" echo" )
60
- field. type(Scalars.GraphQLString )
61
- field. argument { argument ->
62
- argument. name(" arg" )
63
- argument. type(Scalars.GraphQLString )
56
+ .field { GraphQLFieldDefinition.Builder field ->
57
+ field. name(" echo" )
58
+ field. type(Scalars.GraphQLString )
59
+ field. argument { argument ->
60
+ argument. name(" arg" )
61
+ argument. type(Scalars.GraphQLString )
62
+ }
63
+ field. dataFetcher(queryDataFetcher)
64
64
}
65
- field. dataFetcher(queryDataFetcher)
66
- }
67
- .build()
65
+ .field { GraphQLFieldDefinition.Builder field ->
66
+ field. name(" returnsNullIncorrectly" )
67
+ field. type(new GraphQLNonNull (Scalars.GraphQLString ))
68
+ field. dataFetcher({env -> null })
69
+ }
70
+ .build()
68
71
69
72
GraphQLObjectType mutation = GraphQLObjectType . newObject()
70
73
.name(" Mutation" )
71
74
.field { field ->
72
- field. name(" echo" )
73
- field. type(Scalars.GraphQLString )
74
- field. argument { argument ->
75
- argument. name(" arg" )
76
- argument. type(Scalars.GraphQLString )
75
+ field. name(" echo" )
76
+ field. type(Scalars.GraphQLString )
77
+ field. argument { argument ->
78
+ argument. name(" arg" )
79
+ argument. type(Scalars.GraphQLString )
80
+ }
81
+ field. dataFetcher(mutationDataFetcher)
77
82
}
78
- field. dataFetcher(mutationDataFetcher)
79
- }
80
- .build()
83
+ .build()
81
84
82
85
return new SimpleGraphQLServlet (new GraphQLSchema (query, mutation, [query, mutation]. toSet()))
83
86
}
@@ -416,7 +419,25 @@ class GraphQLServletSpec extends Specification {
416
419
resp. errors != null
417
420
}
418
421
419
- private byte [] createContent (String data ) {
420
- data. split(' \\ n' ). collect { it. replaceAll(' ^\\ s+' , ' ' ) }. join(' \n ' ). getBytes()
422
+ def " NonNullableFieldWasNullException is masked by default" () {
423
+ setup :
424
+ request. addParameter(' query' , ' query { returnsNullIncorrectly }' )
425
+
426
+ when :
427
+ servlet. doGet(request, response)
428
+
429
+ then :
430
+ response. getStatus() == STATUS_OK
431
+ response. getContentType() == CONTENT_TYPE_JSON_UTF8
432
+ def resp = getResponseContent()
433
+ resp. containsKey(" data" )
434
+ resp. data == null
435
+ resp. errors != null
436
+ resp. errors. first(). message. contains(' Internal Server Error' )
437
+ }
438
+
439
+ def " typeInfo is serialized correctly" () {
440
+ expect :
441
+ GraphQLServlet . mapper. writeValueAsString(TypeInfo . newTypeInfo(). type(new GraphQLNonNull (Scalars.GraphQLString )). build()) != " {}"
421
442
}
422
443
}
0 commit comments