/
DgsFederationResolverTest.kt
331 lines (282 loc) · 16.1 KB
/
DgsFederationResolverTest.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright 2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.graphql.dgs
import com.apollographql.federation.graphqljava._Entity
import com.netflix.graphql.dgs.exceptions.DefaultDataFetcherExceptionHandler
import com.netflix.graphql.dgs.exceptions.DgsInvalidInputArgumentException
import com.netflix.graphql.dgs.federation.DefaultDgsFederationResolver
import com.netflix.graphql.dgs.internal.DgsSchemaProvider
import graphql.TypeResolutionEnvironment
import graphql.execution.DataFetcherExceptionHandler
import graphql.execution.DataFetcherResult
import graphql.execution.ExecutionStepInfo
import graphql.execution.ResultPath
import graphql.schema.*
import graphql.schema.idl.RuntimeWiring
import graphql.schema.idl.SchemaGenerator
import graphql.schema.idl.SchemaParser
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.context.ApplicationContext
import java.util.*
import java.util.concurrent.CompletableFuture
@Suppress("UNCHECKED_CAST")
@ExtendWith(MockKExtension::class)
class DgsFederationResolverTest {
@MockK
lateinit var applicationContextMock: ApplicationContext
lateinit var dgsSchemaProvider: DgsSchemaProvider
lateinit var dgsExceptionHandler: DataFetcherExceptionHandler
@BeforeEach
fun setup() {
dgsExceptionHandler = DefaultDataFetcherExceptionHandler()
dgsSchemaProvider = DgsSchemaProvider(
applicationContextMock,
Optional.empty(),
Optional.empty(),
Optional.empty(),
dataFetcherExceptionHandler = Optional.of(dgsExceptionHandler)
)
}
@Test
fun defaultTypeResolver() {
val schema = """
type Query {
something: String #Empty queries are not allowed
}
type Movie {
movieID: ID
title: String
}
""".trimIndent()
val graphQLSchema: GraphQLSchema = buildGraphQLSchema(schema)
val type = DefaultDgsFederationResolver(dgsSchemaProvider, Optional.of(dgsExceptionHandler)).typeResolver().getType(TypeResolutionEnvironment(Movie("123", "Stranger Things"), emptyMap(), null, null, graphQLSchema, null))
assertThat(type.name).isEqualTo("Movie")
}
@Test
fun typeNotFound() {
val schema = """
type Query {
something: String
}
""".trimIndent()
val graphQLSchema: GraphQLSchema = buildGraphQLSchema(schema)
val type = DefaultDgsFederationResolver(dgsSchemaProvider, Optional.of(dgsExceptionHandler)).typeResolver().getType(TypeResolutionEnvironment(Movie("123", "Stranger Things"), emptyMap(), null, null, graphQLSchema, null))
assertThat(type).isNull()
}
@Test
fun mappedType() {
val schema = """
type Query {
something: String #Empty queries are not allowed
}
#Represented by Java type Movie, but with a different name
type DgsMovie {
movieID: ID
title: String
}
""".trimIndent()
val graphQLSchema: GraphQLSchema = buildGraphQLSchema(schema)
val customTypeResolver = object : DefaultDgsFederationResolver(dgsSchemaProvider, Optional.of(dgsExceptionHandler)) {
override fun typeMapping(): Map<Class<*>, String> {
return mapOf(Pair(Movie::class.java, "DgsMovie"))
}
}
val type = customTypeResolver.typeResolver().getType(TypeResolutionEnvironment(Movie("123", "Stranger Things"), emptyMap(), null, null, graphQLSchema, null))
assertThat(type.name).isEqualTo("DgsMovie")
}
@Test
fun missingDgsEntityFetcher() {
val arguments = mapOf<String, Any>(Pair(_Entity.argumentName, listOf(mapOf(Pair("__typename", "Movie")))))
val dataFetchingEnvironment = DataFetchingEnvironmentImpl.newDataFetchingEnvironment().arguments(arguments).build()
val result = (DefaultDgsFederationResolver(dgsSchemaProvider, Optional.of(dgsExceptionHandler)).entitiesFetcher().get(dataFetchingEnvironment) as CompletableFuture<DataFetcherResult<List<*>>>)
assertThat(result).isNotNull
assertThat(result.get().data.size).isEqualTo(1)
assertThat(result.get().errors.size).isEqualTo(1)
assertThat(result.get().errors[0].message).contains("MissingDgsEntityFetcherException")
}
@Test
fun missingTypeNameForEntitiesQuery() {
val arguments = mapOf<String, Any>(Pair(_Entity.argumentName, listOf(mapOf(Pair("something", "Else")))))
val dataFetchingEnvironment = DataFetchingEnvironmentImpl.newDataFetchingEnvironment().arguments(arguments).build()
val result = (DefaultDgsFederationResolver(dgsSchemaProvider, Optional.of(dgsExceptionHandler)).entitiesFetcher().get(dataFetchingEnvironment) as CompletableFuture<DataFetcherResult<List<*>>>)
assertThat(result).isNotNull
assertThat(result.get().data.size).isEqualTo(1)
assertThat(result.get().errors.size).isEqualTo(1)
assertThat(result.get().errors.first().message)
.endsWith("MissingFederatedQueryArgument: The federated query is missing field(s) __typename")
}
@Test
fun dgsEntityFetcher() {
val movieEntityFetcher = object {
@DgsEntityFetcher(name = "Movie")
fun movieEntityFetcher(values: Map<String, Any>): Movie {
return Movie(values["movieId"].toString())
}
}
testEntityFetcher(movieEntityFetcher)
}
@Test
fun dgsEntityFetcherReturningNull() {
val movieEntityFetcher = object {
@DgsEntityFetcher(name = "Movie")
fun movieEntityFetcher(values: Map<String, Any>): Movie? {
return null
}
}
every { applicationContextMock.getBeansWithAnnotation(DgsScalar::class.java) } returns emptyMap()
every { applicationContextMock.getBeansWithAnnotation(DgsDirective::class.java) } returns emptyMap()
every { applicationContextMock.getBeansWithAnnotation(DgsComponent::class.java) } returns mapOf(Pair("MovieEntityFetcher", movieEntityFetcher))
dgsSchemaProvider.schema("""type Query {}""")
val arguments = mapOf<String, Any>(Pair(_Entity.argumentName, listOf(mapOf(Pair("__typename", "Movie"), Pair("movieId", "1")))))
val dataFetchingEnvironment = DgsDataFetchingEnvironment(DataFetchingEnvironmentImpl.newDataFetchingEnvironment().arguments(arguments).build())
val result = (DefaultDgsFederationResolver(dgsSchemaProvider, Optional.of(dgsExceptionHandler)).entitiesFetcher().get(dataFetchingEnvironment) as CompletableFuture<DataFetcherResult<List<*>>>)
assertThat(result).isNotNull
assertThat(result.get().data.size).isEqualTo(1)
}
@Test
fun dgsEntityFetcherWithDataFetchingEnv() {
val movieEntityFetcher = object {
@DgsEntityFetcher(name = "Movie")
fun movieEntityFetcher(values: Map<String, Any>, dfe: DataFetchingEnvironment?): Movie {
if (dfe == null) {
throw RuntimeException()
}
return Movie(values["movieId"].toString())
}
}
testEntityFetcher(movieEntityFetcher)
}
@Test
fun dgsEntityFetcherWithDgsDataFetchingEnv() {
val movieEntityFetcher = object {
@DgsEntityFetcher(name = "Movie")
fun movieEntityFetcher(values: Map<String, Any>, dfe: DgsDataFetchingEnvironment?): Movie {
if (dfe == null) {
throw RuntimeException()
}
return Movie(values["movieId"].toString())
}
}
testEntityFetcher(movieEntityFetcher)
}
@Test
fun dgsEntityFetcherWithException() {
val movieEntityFetcher = object {
@DgsEntityFetcher(name = "Movie")
fun movieEntityFetcher(values: Map<String, Any>): Movie {
if (values["movieId"] == "invalid") {
throw DgsInvalidInputArgumentException("Invalid input argument exception")
}
return Movie(values["movieId"].toString())
}
}
every { applicationContextMock.getBeansWithAnnotation(DgsScalar::class.java) } returns emptyMap()
every { applicationContextMock.getBeansWithAnnotation(DgsDirective::class.java) } returns emptyMap()
every { applicationContextMock.getBeansWithAnnotation(DgsComponent::class.java) } returns mapOf(Pair("MovieEntityFetcher", movieEntityFetcher))
dgsSchemaProvider.schema("""type Query {}""")
val arguments = mapOf<String, Any>(Pair(_Entity.argumentName, listOf(mapOf(Pair("__typename", "Movie"), Pair("movieId", "invalid")))))
val executionStepInfo = ExecutionStepInfo.newExecutionStepInfo().path(ResultPath.parse("/_entities")).type(GraphQLList.list(GraphQLUnionType.newUnionType().name("Entity").possibleTypes(GraphQLObjectType.newObject().name("Movie").build()).build())).build()
val dataFetchingEnvironment = DgsDataFetchingEnvironment(DataFetchingEnvironmentImpl.newDataFetchingEnvironment().arguments(arguments).executionStepInfo(executionStepInfo).build())
val result = (DefaultDgsFederationResolver(dgsSchemaProvider, Optional.of(dgsExceptionHandler)).entitiesFetcher().get(dataFetchingEnvironment) as CompletableFuture<DataFetcherResult<List<*>>>)
assertThat(result).isNotNull
assertThat(result.get().data.size).isEqualTo(1)
assertThat(result.get().errors.size).isEqualTo(1)
assertThat(result.get().errors[0].message).contains("DgsInvalidInputArgumentException")
}
@Test
fun dgsEntityFetcherWithFailedCompletableFuture() {
val movieEntityFetcher = object {
@DgsEntityFetcher(name = "Movie")
fun movieEntityFetcher(values: Map<String, Any>): CompletableFuture<Movie> {
return CompletableFuture.supplyAsync {
if (values["movieId"] == "invalid") {
throw DgsInvalidInputArgumentException("Invalid input argument exception")
}
Movie(values["movieId"].toString())
}
}
}
every { applicationContextMock.getBeansWithAnnotation(DgsScalar::class.java) } returns emptyMap()
every { applicationContextMock.getBeansWithAnnotation(DgsDirective::class.java) } returns emptyMap()
every { applicationContextMock.getBeansWithAnnotation(DgsComponent::class.java) } returns mapOf(Pair("MovieEntityFetcher", movieEntityFetcher))
dgsSchemaProvider.schema("""type Query {}""")
val arguments = mapOf<String, Any>(Pair(_Entity.argumentName, listOf(mapOf(Pair("__typename", "Movie"), Pair("movieId", "invalid")))))
val executionStepInfo = ExecutionStepInfo.newExecutionStepInfo().path(ResultPath.parse("/_entities")).type(GraphQLList.list(GraphQLUnionType.newUnionType().name("Entity").possibleTypes(GraphQLObjectType.newObject().name("Movie").build()).build())).build()
val dataFetchingEnvironment = DgsDataFetchingEnvironment(DataFetchingEnvironmentImpl.newDataFetchingEnvironment().arguments(arguments).executionStepInfo(executionStepInfo).build())
val result = (DefaultDgsFederationResolver(dgsSchemaProvider, Optional.of(dgsExceptionHandler)).entitiesFetcher().get(dataFetchingEnvironment) as CompletableFuture<DataFetcherResult<List<*>>>)
assertThat(result).isNotNull
assertThat(result.get().data.size).isEqualTo(1)
assertThat(result.get().errors.size).isEqualTo(1)
assertThat(result.get().errors[0].message).contains("DgsInvalidInputArgumentException")
}
@Test
fun dgsEntityFetcherWithIllegalArgumentException() {
val movieEntityFetcher = object {
@DgsEntityFetcher(name = "Movie")
fun movieEntityFetcher(values: Map<String, Any>, illegalArgument: Int): Movie {
return Movie()
}
}
every { applicationContextMock.getBeansWithAnnotation(DgsScalar::class.java) } returns emptyMap()
every { applicationContextMock.getBeansWithAnnotation(DgsDirective::class.java) } returns emptyMap()
every { applicationContextMock.getBeansWithAnnotation(DgsComponent::class.java) } returns mapOf(Pair("MovieEntityFetcher", movieEntityFetcher))
dgsSchemaProvider.schema("""type Query {}""")
val arguments = mapOf<String, Any>(Pair(_Entity.argumentName, listOf(mapOf(Pair("__typename", "Movie"), Pair("movieId", "invalid"), Pair("illegalArgument", 0)))))
val dataFetchingEnvironment = DgsDataFetchingEnvironment(DataFetchingEnvironmentImpl.newDataFetchingEnvironment().arguments(arguments).build())
val result = (DefaultDgsFederationResolver(dgsSchemaProvider, Optional.of(dgsExceptionHandler)).entitiesFetcher().get(dataFetchingEnvironment) as CompletableFuture<DataFetcherResult<List<*>>>)
assertThat(result).isNotNull
assertThat(result.get().data.size).isEqualTo(1)
assertThat(result.get().errors.size).isEqualTo(1)
assertThat(result.get().errors[0].message).contains("IllegalArgumentException")
}
@Test
fun dgsEntityFetcherMissingArgument() {
val arguments = mapOf<String, Any>(Pair(_Entity.argumentName, listOf(mapOf(Pair("__typename", "Movie")))))
val dataFetchingEnvironment = DataFetchingEnvironmentImpl.newDataFetchingEnvironment().arguments(arguments).build()
val result = (DefaultDgsFederationResolver(dgsSchemaProvider, Optional.of(dgsExceptionHandler)).entitiesFetcher().get(dataFetchingEnvironment) as CompletableFuture<DataFetcherResult<List<*>>>)
assertThat(result).isNotNull
assertThat(result.get().data.size).isEqualTo(1)
assertThat(result.get().errors.size).isEqualTo(1)
assertThat(result.get().errors[0].message).contains("MissingDgsEntityFetcherException")
}
private fun testEntityFetcher(movieEntityFetcher: Any) {
every { applicationContextMock.getBeansWithAnnotation(DgsScalar::class.java) } returns emptyMap()
every { applicationContextMock.getBeansWithAnnotation(DgsDirective::class.java) } returns emptyMap()
every { applicationContextMock.getBeansWithAnnotation(DgsComponent::class.java) } returns mapOf(Pair("MovieEntityFetcher", movieEntityFetcher))
dgsSchemaProvider.schema("""type Query {}""")
val arguments = mapOf<String, Any>(Pair(_Entity.argumentName, listOf(mapOf(Pair("__typename", "Movie"), Pair("movieId", "1")))))
val dataFetchingEnvironment =
DgsDataFetchingEnvironment(DataFetchingEnvironmentImpl.newDataFetchingEnvironment().arguments(arguments).build())
val result = (DefaultDgsFederationResolver(dgsSchemaProvider, Optional.of(dgsExceptionHandler)).entitiesFetcher().get(dataFetchingEnvironment) as CompletableFuture<DataFetcherResult<List<*>>>)
assertThat(result).isNotNull
assertThat(result.get().data.size).isEqualTo(1)
assertThat(result.get().data.first() is Movie).isTrue
assertThat((result.get().data.first() as Movie).movieId).isEqualTo("1")
}
private fun buildGraphQLSchema(schema: String): GraphQLSchema {
val schemaParser = SchemaParser()
val registry = schemaParser.parse(schema)
val schemaGenerator = SchemaGenerator()
return schemaGenerator.makeExecutableSchema(registry, RuntimeWiring.newRuntimeWiring().build())
}
}
data class Movie(val movieId: String = "", val title: String = "")