1+ package graphql.kickstart.tools
2+
3+ import graphql.ExecutionInput
4+ import graphql.GraphQL
5+ import graphql.kickstart.tools.resolver.FieldResolverError
6+ import graphql.schema.DataFetchingEnvironment
7+ import org.junit.Assert
8+ import org.junit.Test
9+ import java.util.*
10+
11+ class MissingFieldResolverTest {
12+
13+ @Test(expected = FieldResolverError ::class )
14+ fun `should throw error` () {
15+ SchemaParser .newParser()
16+ .schemaString("""
17+ type Query {
18+ implementedField(input: String): String
19+ missingField(input: Int): Int
20+ }
21+ """
22+ )
23+ .resolvers(object : GraphQLQueryResolver {
24+ fun implementedField (input : Optional <String >) = input.toString()
25+ })
26+ .build()
27+ .makeExecutableSchema()
28+ }
29+
30+ @Test
31+ fun `should call missing field resolver handler if provided` () {
32+ val schema = SchemaParser .newParser()
33+ .schemaString("""
34+ type Query {
35+ implementedField(input: String): String
36+ missingField(input: Int): Int
37+ }
38+ """
39+ )
40+ .resolvers(object : GraphQLQueryResolver {
41+ fun implementedField (input : Optional <String >) = input.toString()
42+ })
43+ .options(SchemaParserOptions .newOptions()
44+ .missingFieldHandler(TestMissingFieldHandler ())
45+ .build())
46+ .build()
47+ .makeExecutableSchema()
48+
49+ val gql = GraphQL .newGraphQL(schema).build()
50+
51+ val result = gql
52+ .execute(ExecutionInput .newExecutionInput()
53+ .query("""
54+ query {
55+ implementedField(input: "test-value")
56+ missingField(input: 1)
57+ }
58+ """ )
59+ .context(Object ())
60+ .root(Object ()))
61+
62+ val expected = mapOf (
63+ " implementedField" to " Optional[test-value]" ,
64+ " missingField" to 1
65+ )
66+
67+ Assert .assertEquals(expected, result.getData())
68+ }
69+
70+ class TestMissingFieldHandler : MissingFieldHandler {
71+ override fun resolve (env : DataFetchingEnvironment ? ): Any? {
72+ return env?.getArgument(" input" );
73+ }
74+ }
75+ }
0 commit comments