forked from graphql-java-kickstart/graphql-java-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EndToEndSpec.kt
261 lines (208 loc) · 7.89 KB
/
EndToEndSpec.kt
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.coxautodev.graphql.tools
import graphql.execution.batched.Batched
import graphql.language.ObjectValue
import graphql.language.StringValue
import graphql.schema.Coercing
import graphql.schema.DataFetchingEnvironment
import graphql.schema.GraphQLScalarType
import org.reactivestreams.Publisher
import java.util.Optional
import java.util.UUID
import java.util.concurrent.CompletableFuture
fun createSchema() = SchemaParser.newParser()
.schemaString(schemaDefinition)
.resolvers(Query(), Mutation(), Subscription(), ItemResolver(), UnusedRootResolver(), UnusedResolver())
.scalars(customScalarUUID, customScalarMap)
.dictionary("OtherItem", OtherItemWithWrongName::class)
.dictionary("ThirdItem", ThirdItem::class)
.build()
.makeExecutableSchema()
val schemaDefinition = """
scalar UUID
scalar customScalarMap
type Query {
# Check if items list is empty
empty: Boolean!
# Get all items
allBaseItems: [Item!]
# Get items by name
items(itemsInput: ItemSearchInput!): [Item!]
optionalItem(itemsInput: ItemSearchInput!): Item
allItems: [AllItems!]
otherUnionItems: [OtherUnion!]
nestedUnionItems: [NestedUnion!]
itemsByInterface: [ItemInterface!]
itemByUUID(uuid: UUID!): Item
itemsWithOptionalInput(itemsInput: ItemSearchInput): [Item!]
itemsWithOptionalInputExplicit(itemsInput: ItemSearchInput): [Item!]
enumInputType(type: Type!): Type!
customScalarMapInputType(customScalarMap: customScalarMap): customScalarMap
defaultArgument(arg: Boolean = true): Boolean!
listList: [[String!]!]!
futureItems: [Item!]!
complexNullableType: ComplexNullable
complexInputType(complexInput: [[ComplexInputType!]]): String!
extendedType: ExtendedType!
propertyField: String!
}
type ExtendedType {
first: String!
}
extend type ExtendedType {
second: String!
}
type ComplexNullable {
first: String!
second: String!
third: String!
}
input ComplexInputType {
first: String!
second: [[ComplexInputTypeTwo!]]
}
input ComplexInputTypeTwo {
first: String!
}
type Mutation {
addItem(newItem: NewItemInput!): Item!
}
type Subscription {
onItemCreated: Item!
}
input ItemSearchInput {
# The item name to look for
name: String!
}
input NewItemInput {
name: String! @deprecated
type: Type! @deprecated(reason: "This is a reason")
}
enum Type {
# Item type 1
TYPE_1
# Item type 2
TYPE_2
}
type Item implements ItemInterface {
id: Int!
name: String!
type: Type!
uuid: UUID!
tags(names: [String!]): [Tag!]
batchedName: String!
}
type OtherItem implements ItemInterface {
id: Int!
name: String!
type: Type!
uuid: UUID!
}
interface ItemInterface {
name: String!
type: Type!
uuid: UUID!
}
union AllItems = Item | OtherItem
type ThirdItem {
id: Int!
}
union OtherUnion = Item | ThirdItem
union NestedUnion = OtherUnion | OtherItem
type Tag {
id: Int!
name: String!
}
"""
val items = mutableListOf(
Item(0, "item1", Type.TYPE_1, UUID.fromString("38f685f1-b460-4a54-a17f-7fd69e8cf3f8"), listOf(Tag(1, "item1-tag1"), Tag(2, "item1-tag2"))),
Item(1, "item2", Type.TYPE_2, UUID.fromString("38f685f1-b460-4a54-b17f-7fd69e8cf3f8"), listOf(Tag(3, "item2-tag1"), Tag(4, "item2-tag2")))
)
val otherItems = mutableListOf(
OtherItemWithWrongName(0, "otherItem1", Type.TYPE_1, UUID.fromString("38f685f1-b460-4a54-c17f-7fd69e8cf3f8")),
OtherItemWithWrongName(1, "otherItem2", Type.TYPE_2, UUID.fromString("38f685f1-b460-4a54-d17f-7fd69e8cf3f8"))
)
val thirdItems = mutableListOf(
ThirdItem(100)
)
class Query: GraphQLQueryResolver, ListListResolver<String>() {
fun isEmpty() = items.isEmpty()
fun allBaseItems() = items
fun items(input: ItemSearchInput): List<Item> = items.filter { it.name == input.name }
fun optionalItem(input: ItemSearchInput) = items(input).firstOrNull()?.let { Optional.of(it) } ?: Optional.empty()
fun allItems(): List<Any> = items + otherItems
fun otherUnionItems(): List<Any> = items + thirdItems
fun nestedUnionItems(): List<Any> = items + otherItems + thirdItems
fun itemsByInterface(): List<ItemInterface> = items + otherItems
fun itemByUUID(uuid: UUID): Item? = items.find { it.uuid == uuid }
fun itemsWithOptionalInput(input: ItemSearchInput?) = if(input == null) items else items(input)
fun itemsWithOptionalInputExplicit(input: Optional<ItemSearchInput>) = if(input.isPresent) items(input.get()) else items
fun enumInputType(type: Type) = type
fun customScalarMapInputType(customScalarMap: Map<String, Any>) = customScalarMap
fun defaultArgument(arg: Boolean) = arg
fun futureItems() = CompletableFuture.completedFuture(items)
fun complexNullableType(): ComplexNullable? = null
fun complexInputType(input: List<List<ComplexInputType>?>?) = input?.firstOrNull()?.firstOrNull()?.let { it.first == "foo" && it.second?.firstOrNull()?.firstOrNull()?.first == "bar" } ?: false
fun extendedType() = ExtendedType()
private val propertyField = "test"
}
class UnusedRootResolver: GraphQLQueryResolver
class UnusedResolver: GraphQLResolver<String>
class ExtendedType {
fun first() = "test"
fun second() = "test"
}
abstract class ListListResolver<out E> {
fun listList(): List<List<E>> = listOf(listOf())
}
class Mutation: GraphQLMutationResolver {
fun addItem(input: NewItemInput): Item {
return Item(items.size, input.name, input.type, UUID.randomUUID(), listOf()) // Don't actually add the item to the list, since we want the test to be deterministic
}
}
class OnItemCreatedContext(val newItem: Item)
class Subscription : GraphQLSubscriptionResolver {
fun onItemCreated(env: DataFetchingEnvironment) =
Publisher<Item> { subscriber ->
subscriber.onNext(env.getContext<OnItemCreatedContext>().newItem)
// subscriber.onComplete()
}
}
class ItemResolver : GraphQLResolver<Item> {
fun tags(item: Item, names: List<String>?): List<Tag> = item.tags.filter { names?.contains(it.name) ?: true }
@Batched
fun batchedName(items: List<Item>) = items.map { it.name }
}
interface ItemInterface {
val name: String
val type: Type
val uuid: UUID
}
enum class Type { TYPE_1, TYPE_2 }
data class Item(val id: Int, override val name: String, override val type: Type, override val uuid:UUID, val tags: List<Tag>) : ItemInterface
data class OtherItemWithWrongName(val id: Int, override val name: String, override val type: Type, override val uuid:UUID) : ItemInterface
data class ThirdItem(val id: Int)
data class Tag(val id: Int, val name: String)
data class ItemSearchInput(val name: String)
data class NewItemInput(val name: String, val type: Type)
data class ComplexNullable(val first: String, val second: String, val third: String)
data class ComplexInputType(val first: String, val second: List<List<ComplexInputTypeTwo>?>?)
data class ComplexInputTypeTwo(val first: String)
val customScalarUUID = GraphQLScalarType("UUID", "UUID", object : Coercing<UUID, String> {
override fun serialize(input: Any): String? = when (input) {
is String -> input
is UUID -> input.toString()
else -> null
}
override fun parseValue(input: Any): UUID? = parseLiteral(input)
override fun parseLiteral(input: Any): UUID? = when (input) {
is StringValue -> UUID.fromString(input.value)
else -> null
}
})
val customScalarMap = GraphQLScalarType("customScalarMap", "customScalarMap", object: Coercing<Map<String, Any>, Map<String, Any>> {
@Suppress("UNCHECKED_CAST")
override fun parseValue(input: Any?): Map<String, Any> = input as Map<String, Any>
@Suppress("UNCHECKED_CAST")
override fun serialize(dataFetcherResult: Any?): Map<String, Any> = dataFetcherResult as Map<String, Any>
override fun parseLiteral(input: Any?): Map<String, Any> = (input as ObjectValue).objectFields.associateBy { it.name }.mapValues { (it.value.value as StringValue).value }
})