11package com.auritylab.graphql.kotlin.toolkit.codegen.generator.fieldResolver
22
3- import com.auritylab.graphql.kotlin.toolkit.codegen._test.AbstractMockCompilationTest
3+ import com.auritylab.graphql.kotlin.toolkit.codegen._test.AbstractCompilationTest
44import com.auritylab.graphql.kotlin.toolkit.codegen._test.TestObject
55import graphql.Scalars
66import graphql.schema.GraphQLArgument
77import graphql.schema.GraphQLFieldDefinition
8+ import graphql.schema.GraphQLFieldsContainer
89import graphql.schema.GraphQLNonNull
910import 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
1017import kotlin.reflect.KClass
1118import kotlin.reflect.KFunction
1219import kotlin.reflect.KProperty1
@@ -16,15 +23,9 @@ import kotlin.reflect.full.createType
1623import kotlin.reflect.full.memberFunctions
1724import kotlin.reflect.full.memberProperties
1825import 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
2526
2627@TestInstance(TestInstance .Lifecycle .PER_CLASS )
27- internal class FieldResolverGeneratorTest : AbstractMockCompilationTest ( ) {
28+ internal class FieldResolverGeneratorTest : AbstractCompilationTest ( true ) {
2829 @Nested
2930 @DisplayName(" Simple field resolver" )
3031 @TestInstance(TestInstance .Lifecycle .PER_CLASS )
@@ -213,8 +214,63 @@ internal class FieldResolverGeneratorTest : AbstractMockCompilationTest() {
213214 }
214215 }
215216
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+
216268 /* *
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.
218274 */
219275 private fun getEnvClass (generated : KClass <* >): KClass <* > {
220276 val first = generated.nestedClasses.firstOrNull { it.simpleName == " Env" }
@@ -223,7 +279,11 @@ internal class FieldResolverGeneratorTest : AbstractMockCompilationTest() {
223279 }
224280
225281 /* *
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.
227287 */
228288 private fun getResolveFunction (generated : KClass <* >): KFunction <* > {
229289 val first = generated.memberFunctions.firstOrNull { it.name == " resolve" }
@@ -232,20 +292,23 @@ internal class FieldResolverGeneratorTest : AbstractMockCompilationTest() {
232292 }
233293}
234294
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