1
1
package com.auritylab.graphql.kotlin.toolkit.codegen.generator.fieldResolver
2
2
3
- import com.auritylab.graphql.kotlin.toolkit.codegen._test.AbstractMockCompilationTest
3
+ import com.auritylab.graphql.kotlin.toolkit.codegen._test.AbstractCompilationTest
4
4
import com.auritylab.graphql.kotlin.toolkit.codegen._test.TestObject
5
5
import graphql.Scalars
6
6
import graphql.schema.GraphQLArgument
7
7
import graphql.schema.GraphQLFieldDefinition
8
+ import graphql.schema.GraphQLFieldsContainer
8
9
import graphql.schema.GraphQLNonNull
9
10
import graphql.schema.GraphQLObjectType
11
+ import org.junit.jupiter.api.Assertions
12
+ import org.junit.jupiter.api.BeforeAll
13
+ import org.junit.jupiter.api.DisplayName
14
+ import org.junit.jupiter.api.Nested
15
+ import org.junit.jupiter.api.Test
16
+ import org.junit.jupiter.api.TestInstance
10
17
import kotlin.reflect.KClass
11
18
import kotlin.reflect.KFunction
12
19
import kotlin.reflect.KProperty1
@@ -16,15 +23,9 @@ import kotlin.reflect.full.createType
16
23
import kotlin.reflect.full.memberFunctions
17
24
import kotlin.reflect.full.memberProperties
18
25
import kotlin.reflect.full.starProjectedType
19
- import org.junit.jupiter.api.Assertions
20
- import org.junit.jupiter.api.BeforeAll
21
- import org.junit.jupiter.api.DisplayName
22
- import org.junit.jupiter.api.Nested
23
- import org.junit.jupiter.api.Test
24
- import org.junit.jupiter.api.TestInstance
25
26
26
27
@TestInstance(TestInstance .Lifecycle .PER_CLASS )
27
- internal class FieldResolverGeneratorTest : AbstractMockCompilationTest ( ) {
28
+ internal class FieldResolverGeneratorTest : AbstractCompilationTest ( true ) {
28
29
@Nested
29
30
@DisplayName(" Simple field resolver" )
30
31
@TestInstance(TestInstance .Lifecycle .PER_CLASS )
@@ -213,8 +214,63 @@ internal class FieldResolverGeneratorTest : AbstractMockCompilationTest() {
213
214
}
214
215
}
215
216
217
+ @Nested
218
+ @DisplayName(" Resolver interface clashes" )
219
+ @TestInstance(TestInstance .Lifecycle .PER_CLASS )
220
+ inner class ResolverDuplicates {
221
+ private val variationOne: GraphQLObjectType =
222
+ GraphQLObjectType .newObject()
223
+ .name(" User" )
224
+ .field(
225
+ GraphQLFieldDefinition .newFieldDefinition()
226
+ .name(" emailAddress" )
227
+ .type(Scalars .GraphQLString )
228
+ )
229
+ .build()
230
+
231
+ private val variationTwo: GraphQLObjectType =
232
+ GraphQLObjectType .newObject()
233
+ .name(" UserEmail" )
234
+ .field(
235
+ GraphQLFieldDefinition .newFieldDefinition()
236
+ .name(" address" )
237
+ .type(Scalars .GraphQLString )
238
+ )
239
+ .build()
240
+
241
+ @Test
242
+ fun `similar generated resolvers should not clash because of the same package and class name` () {
243
+ val firstGenerator = buildGenerator(variationOne, variationOne.getFieldDefinition(" emailAddress" ))
244
+ val secondGenerator = buildGenerator(variationTwo, variationTwo.getFieldDefinition(" address" ))
245
+
246
+ // It's sufficient to just compile as the compilation would fail due to duplicate classes.
247
+ compile(firstGenerator, secondGenerator)
248
+ }
249
+
250
+ /* *
251
+ * Will build a [FieldResolverGenerator] with the given [container] and [field]. This will get all other
252
+ * dependencies from the [TestObject].
253
+ */
254
+ private fun buildGenerator (
255
+ container : GraphQLFieldsContainer ,
256
+ field : GraphQLFieldDefinition
257
+ ): FieldResolverGenerator =
258
+ FieldResolverGenerator (
259
+ container, field,
260
+ TestObject .implementerMapper,
261
+ TestObject .argumentCodeBlockGenerator,
262
+ TestObject .options.copy(globalContext = " kotlin.String" ),
263
+ TestObject .kotlinTypeMapper,
264
+ TestObject .generatedMapper
265
+ )
266
+ }
267
+
216
268
/* *
217
- * Will return the "Env" class from the current [generatedClass].
269
+ * Will return a reference to the "Env" class of the given [KClass] as [KClass]. The This will additionally
270
+ * assert against null on the search result for the class.
271
+ *
272
+ * @param generated The class of which the inner class shall be resolved.
273
+ * @return The [KClass] reference to the "Env" class.
218
274
*/
219
275
private fun getEnvClass (generated : KClass <* >): KClass <* > {
220
276
val first = generated.nestedClasses.firstOrNull { it.simpleName == " Env" }
@@ -223,7 +279,11 @@ internal class FieldResolverGeneratorTest : AbstractMockCompilationTest() {
223
279
}
224
280
225
281
/* *
226
- * Will return the "resolve" function from the current [generatedClass].
282
+ * Will return a reference to the "resolve" function for the given [KClass] as [KFunction]. This will additional
283
+ * assert against null on the search result for the function.
284
+ *
285
+ * @param generated The class of which the function shall be resolved.
286
+ * @return The [KFunction] reference to the "Env" class.
227
287
*/
228
288
private fun getResolveFunction (generated : KClass <* >): KFunction <* > {
229
289
val first = generated.memberFunctions.firstOrNull { it.name == " resolve" }
@@ -232,20 +292,23 @@ internal class FieldResolverGeneratorTest : AbstractMockCompilationTest() {
232
292
}
233
293
}
234
294
235
- val testSimpleFieldDefinition = GraphQLFieldDefinition .newFieldDefinition()
236
- .name(" simple" )
237
- .type(Scalars .GraphQLString )
238
- .build()
239
-
240
- val testArgumentFieldDefinition = GraphQLFieldDefinition .newFieldDefinition()
241
- .name(" arguments" )
242
- .argument(GraphQLArgument .newArgument().name(" bool" ).type(GraphQLNonNull (Scalars .GraphQLBoolean )).build())
243
- .argument(GraphQLArgument .newArgument().name(" int" ).type(GraphQLNonNull (Scalars .GraphQLInt )).build())
244
- .type(GraphQLNonNull (Scalars .GraphQLString ))
245
- .build()
246
-
247
- val testObjectType = GraphQLObjectType .newObject()
248
- .name(" TestObjectType" )
249
- .field(testSimpleFieldDefinition)
250
- .field(testArgumentFieldDefinition)
251
- .build()
295
+ val testSimpleFieldDefinition: GraphQLFieldDefinition =
296
+ GraphQLFieldDefinition .newFieldDefinition()
297
+ .name(" simple" )
298
+ .type(Scalars .GraphQLString )
299
+ .build()
300
+
301
+ val testArgumentFieldDefinition: GraphQLFieldDefinition =
302
+ GraphQLFieldDefinition .newFieldDefinition()
303
+ .name(" arguments" )
304
+ .argument(GraphQLArgument .newArgument().name(" bool" ).type(GraphQLNonNull (Scalars .GraphQLBoolean )).build())
305
+ .argument(GraphQLArgument .newArgument().name(" int" ).type(GraphQLNonNull (Scalars .GraphQLInt )).build())
306
+ .type(GraphQLNonNull (Scalars .GraphQLString ))
307
+ .build()
308
+
309
+ val testObjectType: GraphQLObjectType =
310
+ GraphQLObjectType .newObject()
311
+ .name(" TestObjectType" )
312
+ .field(testSimpleFieldDefinition)
313
+ .field(testArgumentFieldDefinition)
314
+ .build()
0 commit comments