Repository navigation
Expand file tree
/
Copy pathDataLoaderTypeMismatchTest.groovy
More file actions
75 lines (60 loc) · 2.5 KB
/
Copy pathDataLoaderTypeMismatchTest.groovy
File metadata and controls
75 lines (60 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package graphql.execution.instrumentation.dataloader
import graphql.ExecutionInput
import graphql.GraphQL
import graphql.schema.DataFetcher
import graphql.schema.DataFetchingEnvironment
import graphql.schema.idl.RuntimeWiring
import graphql.schema.idl.SchemaGenerator
import graphql.schema.idl.SchemaParser
import org.dataloader.BatchLoader
import org.dataloader.DataLoader
import org.dataloader.DataLoaderFactory
import org.dataloader.DataLoaderRegistry
import spock.lang.Specification
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage
import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring
class DataLoaderTypeMismatchTest extends Specification {
def "when actual field value return type is different from expected return type, then it should not hang execution"() {
setup:
def sdl = """
type Todo {
id: ID!
}
type Query {
getTodos: [Todo]
}
schema {
query: Query
}"""
def typeDefinitionRegistry = new SchemaParser().parse(sdl)
def dataLoader = DataLoaderFactory.newDataLoader(new BatchLoader<Object, Object>() {
@Override
CompletionStage<List<Object>> load(List<Object> keys) {
return CompletableFuture.completedFuture([
[a: "map instead of a list of todos"]
])
}
})
def dataLoaderRegistry = new DataLoaderRegistry()
dataLoaderRegistry.register("getTodos", dataLoader)
def todosDef = new DataFetcher<CompletableFuture<Object>>() {
@Override
CompletableFuture<Object> get(DataFetchingEnvironment environment) {
return environment.getDataLoader("getTodos").load(environment)
}
}
def wiring = RuntimeWiring.newRuntimeWiring()
.type(newTypeWiring("Query")
.dataFetcher("getTodos", todosDef))
.build()
def schema = new SchemaGenerator().makeExecutableSchema(typeDefinitionRegistry, wiring)
def graphql = GraphQL.newGraphQL(schema)
.build()
when:
def result = graphql.execute(ExecutionInput.newExecutionInput().dataLoaderRegistry(dataLoaderRegistry).query("query { getTodos { id } }").build())
then: "execution shouldn't hang"
!result.errors.empty
result.errors[0].message == "Can't resolve value (/getTodos) : type mismatch error, expected type LIST"
}
}