Skip to content

Commit d28248f

Browse files
smyrickamandaducrou
authored andcommitted
refactor: more clean up (#66)
1 parent 46c00cf commit d28248f

File tree

13 files changed

+83
-53
lines changed

13 files changed

+83
-53
lines changed

detekt_baseline.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
<ID>LargeClass:SchemaGenerator.kt$SchemaGenerator</ID>
66
<ID>MethodOverloading:SchemaGeneratorTest.kt$SchemaGeneratorTest</ID>
77
<ID>TooManyFunctions:SchemaGenerator.kt$SchemaGenerator</ID>
8-
<ID>UnsafeCast:SchemaGeneratorAsyncTests.kt$SchemaGeneratorAsyncTests$schema.getObjectType("TopLevelQuery").getFieldDefinition("asynchronouslyDo").type as GraphQLNonNull</ID>
9-
<ID>UnsafeCast:SchemaGeneratorAsyncTests.kt$SchemaGeneratorAsyncTests$schema.getObjectType("TopLevelQuery").getFieldDefinition("asynchronouslyDoSingle").type as GraphQLNonNull</ID>
10-
<ID>UnsafeCast:SchemaGeneratorAsyncTests.kt$SchemaGeneratorAsyncTests$schema.getObjectType("TopLevelQuery").getFieldDefinition("maybe").type as GraphQLNonNull</ID>
118
<ID>ComplexInterface:SchemaGeneratorHooks.kt$SchemaGeneratorHooks</ID>
129
</Whitelist>
1310
</SmellBaseline>

src/main/kotlin/com/expedia/graphql/schema/extensions/annotationExtensions.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import kotlin.reflect.full.findAnnotation
1515
import com.expedia.graphql.annotations.GraphQLDirective as DirectiveAnnotation
1616

1717
internal fun KAnnotatedElement.graphQLDescription(): String? {
18-
val directiveNames = listOfDirectives().map { it.normalizeDirectiveName() }
18+
val directiveNames = listOfDirectives().map { normalizeDirectiveName(it) }
1919

2020
val description = this.findAnnotation<GraphQLDescription>()?.value
2121

@@ -86,7 +86,7 @@ private fun DirectiveAnnotation.getGraphQLDirective(): GraphQLDirective {
8686
}
8787

8888
@Suppress("Detekt.SpreadOperator")
89-
builder.name(name.normalizeDirectiveName())
89+
builder.name(normalizeDirectiveName(name))
9090
.validLocations(*this.locations)
9191
.description(this.description)
9292

@@ -95,10 +95,15 @@ private fun DirectiveAnnotation.getGraphQLDirective(): GraphQLDirective {
9595
val value = kFunction.call(kClass)
9696
@Suppress("Detekt.UnsafeCast")
9797
val type = defaultGraphQLScalars(kFunction.returnType) as GraphQLInputType
98-
builder.argument(GraphQLArgument.newArgument().name(propertyName).value(value).type(type).build())
98+
val argument = GraphQLArgument.newArgument()
99+
.name(propertyName)
100+
.value(value)
101+
.type(type)
102+
.build()
103+
builder.argument(argument)
99104
}
100105

101106
return builder.build()
102107
}
103108

104-
private fun String.normalizeDirectiveName() = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, this)
109+
private fun normalizeDirectiveName(string: String) = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, string)

src/main/kotlin/com/expedia/graphql/schema/extensions/kClassExtensions.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ package com.expedia.graphql.schema.extensions
22

33
import com.expedia.graphql.schema.generator.functionFilters
44
import com.expedia.graphql.schema.generator.propertyFilters
5+
import com.expedia.graphql.schema.hooks.NoopSchemaGeneratorHooks
56
import com.expedia.graphql.schema.hooks.SchemaGeneratorHooks
67
import kotlin.reflect.KClass
78
import kotlin.reflect.full.declaredMemberFunctions
89
import kotlin.reflect.full.declaredMemberProperties
910

10-
internal fun KClass<*>.getValidProperties(hooks: SchemaGeneratorHooks? = null) = this.declaredMemberProperties
11-
.filter { hooks?.isValidProperty(it) ?: true }
11+
private val noopHooks = NoopSchemaGeneratorHooks()
12+
13+
internal fun KClass<*>.getValidProperties(hooks: SchemaGeneratorHooks = noopHooks) = this.declaredMemberProperties
14+
.filter { hooks.isValidProperty(it) }
1215
.filter { prop -> propertyFilters.all { it.invoke(prop) } }
1316

14-
internal fun KClass<*>.getValidFunctions(hooks: SchemaGeneratorHooks? = null) = this.declaredMemberFunctions
15-
.filter { hooks?.isValidFunction(it) ?: true }
17+
internal fun KClass<*>.getValidFunctions(hooks: SchemaGeneratorHooks = noopHooks) = this.declaredMemberFunctions
18+
.filter { hooks.isValidFunction(it) }
1619
.filter { func -> functionFilters.all { it.invoke(func) } }
1720

1821
internal fun KClass<*>.canBeGraphQLInterface(): Boolean = this.java.isInterface

src/main/kotlin/com/expedia/graphql/schema/generator/SchemaGenerator.kt

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import com.expedia.graphql.schema.extensions.isGraphQLContext
1616
import com.expedia.graphql.schema.extensions.isGraphQLID
1717
import com.expedia.graphql.schema.extensions.throwIfUnathorizedInterface
1818
import com.expedia.graphql.schema.extensions.wrapInNonNull
19+
import com.expedia.graphql.schema.generator.models.KGraphQLType
1920
import com.expedia.graphql.schema.generator.state.SchemaGeneratorState
2021
import com.expedia.graphql.schema.generator.types.defaultGraphQLScalars
2122
import com.expedia.graphql.schema.generator.types.enumType
2223
import com.expedia.graphql.schema.generator.types.getInputClassName
23-
import com.expedia.graphql.schema.models.KGraphQLType
2424
import graphql.TypeResolutionEnvironment
2525
import graphql.schema.DataFetcher
2626
import graphql.schema.GraphQLArgument
@@ -271,7 +271,7 @@ internal class SchemaGenerator(
271271
}
272272

273273
private fun interfaceType(kClass: KClass<*>): GraphQLType {
274-
return state.cache.buildIfNotUnderConstruction(kClass) {
274+
return state.cache.buildIfNotUnderConstruction(kClass) { _ ->
275275
val builder = GraphQLInterfaceType.newInterface()
276276

277277
builder.name(kClass.simpleName)
@@ -287,47 +287,43 @@ internal class SchemaGenerator(
287287
val interfaceType = builder.build()
288288

289289
val implementations = subTypeMapper.getSubTypesOf(kClass)
290-
implementations
291-
.filterNot { it.kotlin.isAbstract }
292-
.forEach {
293-
val objectType = objectType(it.kotlin, interfaceType)
290+
implementations.forEach {
291+
val objectType = objectType(it.kotlin, interfaceType)
294292

295-
if (objectType !is GraphQLTypeReference) {
296-
state.additionalTypes.add(objectType)
297-
}
298-
state.cache.removeTypeUnderConstruction(it.kotlin)
299-
}
293+
if (objectType !is GraphQLTypeReference) {
294+
state.additionalTypes.add(objectType)
295+
}
296+
state.cache.removeTypeUnderConstruction(it.kotlin)
297+
}
300298

301299
interfaceType
302300
}
303301
}
304302

305303
private fun unionType(kClass: KClass<*>): GraphQLType {
306-
return state.cache.buildIfNotUnderConstruction(kClass) {
304+
return state.cache.buildIfNotUnderConstruction(kClass) { _ ->
307305
val builder = GraphQLUnionType.newUnionType()
308306

309307
builder.name(kClass.simpleName)
310308
builder.description(kClass.graphQLDescription())
311309
builder.typeResolver { env: TypeResolutionEnvironment -> env.schema.getObjectType(env.getObject<Any>().javaClass.simpleName) }
312310

313311
val implementations = subTypeMapper.getSubTypesOf(kClass)
314-
implementations
315-
.filterNot { it.kotlin.isAbstract }
316-
.forEach {
317-
val objectType = state.cache.get(TypesCacheKey(it.kotlin.createType(), false)) ?: objectType(it.kotlin)
312+
implementations.forEach {
313+
val objectType = state.cache.get(TypesCacheKey(it.kotlin.createType(), false)) ?: objectType(it.kotlin)
318314

319-
val key = TypesCacheKey(it.kotlin.createType(), false)
315+
val key = TypesCacheKey(it.kotlin.createType(), false)
320316

321-
if (objectType is GraphQLTypeReference) {
322-
builder.possibleType(objectType)
323-
} else {
324-
builder.possibleType(objectType as GraphQLObjectType)
325-
}
317+
if (objectType is GraphQLTypeReference) {
318+
builder.possibleType(objectType)
319+
} else {
320+
builder.possibleType(objectType as GraphQLObjectType)
321+
}
326322

327-
if (state.cache.doesNotContain(it.kotlin)) {
328-
state.cache.put(key, KGraphQLType(it.kotlin, objectType))
329-
}
330-
}
323+
if (state.cache.doesNotContain(it.kotlin)) {
324+
state.cache.put(key, KGraphQLType(it.kotlin, objectType))
325+
}
326+
}
331327

332328
builder.build()
333329
}

src/main/kotlin/com/expedia/graphql/schema/generator/SubTypeMapper.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ internal class SubTypeMapper(supportedPackages: List<String>) {
77

88
private val reflections = Reflections(supportedPackages)
99

10-
fun getSubTypesOf(kclass: KClass<*>): MutableSet<out Class<out Any>> =
10+
fun getSubTypesOf(kclass: KClass<*>): List<Class<*>> =
1111
reflections.getSubTypesOf(Class.forName(kclass.javaObjectType.name))
12+
.filterNot { it.kotlin.isAbstract }
1213
}

src/main/kotlin/com/expedia/graphql/schema/generator/TypesCache.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.expedia.graphql.schema.exceptions.ConflictingTypesException
55
import com.expedia.graphql.schema.exceptions.CouldNotGetJvmNameOfKTypeException
66
import com.expedia.graphql.schema.exceptions.CouldNotGetNameOfEnumException
77
import com.expedia.graphql.schema.exceptions.TypeNotSupportedException
8-
import com.expedia.graphql.schema.models.KGraphQLType
8+
import com.expedia.graphql.schema.generator.models.KGraphQLType
99
import graphql.schema.GraphQLType
1010
import graphql.schema.GraphQLTypeReference
1111
import kotlin.reflect.KClass

src/main/kotlin/com/expedia/graphql/schema/models/KGraphQLType.kt renamed to src/main/kotlin/com/expedia/graphql/schema/generator/models/KGraphQLType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.expedia.graphql.schema.models
1+
package com.expedia.graphql.schema.generator.models
22

33
import graphql.schema.GraphQLType
44
import kotlin.reflect.KClass

src/main/kotlin/com/expedia/graphql/toSchema.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import graphql.schema.GraphQLSchema
1313
* @param config Schema generation configuration
1414
*/
1515
fun toSchema(
16-
queries: List<TopLevelObjectDef> = emptyList(),
16+
queries: List<TopLevelObjectDef>,
1717
mutations: List<TopLevelObjectDef> = emptyList(),
1818
config: SchemaGeneratorConfig
1919
): GraphQLSchema {

src/test/kotlin/com/expedia/graphql/schema/generator/SchemaGeneratorAsyncTests.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,31 @@ class SchemaGeneratorAsyncTests {
2929
fun `SchemaGenerator strips type argument from CompletableFuture to support async servlet`() {
3030
val schema = toSchema(listOf(TopLevelObjectDef(AsyncQuery())), config = testSchemaConfig)
3131
val returnTypeName =
32-
(schema.getObjectType("TopLevelQuery").getFieldDefinition("asynchronouslyDo").type as GraphQLNonNull).wrappedType.name
32+
(schema.getObjectType("TopLevelQuery").getFieldDefinition("asynchronouslyDo").type as? GraphQLNonNull)?.wrappedType?.name
3333
assertEquals("Int", returnTypeName)
3434
}
3535

3636
@Test
3737
fun `SchemaGenerator strips type argument from RxJava2 Observable`() {
3838
val schema = toSchema(listOf(TopLevelObjectDef(RxJava2Query())), config = configWithRxJavaMonads)
3939
val returnTypeName =
40-
(schema.getObjectType("TopLevelQuery").getFieldDefinition("asynchronouslyDo").type as GraphQLNonNull).wrappedType.name
40+
(schema.getObjectType("TopLevelQuery").getFieldDefinition("asynchronouslyDo").type as? GraphQLNonNull)?.wrappedType?.name
4141
assertEquals("Int", returnTypeName)
4242
}
4343

4444
@Test
4545
fun `SchemaGenerator strips type argument from RxJava2 Single`() {
4646
val schema = toSchema(listOf(TopLevelObjectDef(RxJava2Query())), config = configWithRxJavaMonads)
4747
val returnTypeName =
48-
(schema.getObjectType("TopLevelQuery").getFieldDefinition("asynchronouslyDoSingle").type as GraphQLNonNull).wrappedType.name
48+
(schema.getObjectType("TopLevelQuery").getFieldDefinition("asynchronouslyDoSingle").type as? GraphQLNonNull)?.wrappedType?.name
4949
assertEquals("Int", returnTypeName)
5050
}
5151

5252
@Test
5353
fun `SchemaGenerator strips type argument from RxJava2 Maybe`() {
5454
val schema = toSchema(listOf(TopLevelObjectDef(RxJava2Query())), config = configWithRxJavaMonads)
5555
val returnTypeName =
56-
(schema.getObjectType("TopLevelQuery").getFieldDefinition("maybe").type as GraphQLNonNull).wrappedType.name
56+
(schema.getObjectType("TopLevelQuery").getFieldDefinition("maybe").type as? GraphQLNonNull)?.wrappedType?.name
5757
assertEquals("Int", returnTypeName)
5858
}
5959

src/test/kotlin/com/expedia/graphql/schema/generator/SchemaGeneratorTest.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import kotlin.test.assertFailsWith
2323
import kotlin.test.assertNotNull
2424
import kotlin.test.assertTrue
2525

26-
@Suppress("Detekt.UnusedPrivateMember", "Detekt.FunctionOnlyReturningConstant", "Detekt.LargeClass")
26+
@Suppress("Detekt.UnusedPrivateMember",
27+
"Detekt.FunctionOnlyReturningConstant",
28+
"Detekt.LargeClass",
29+
"Detekt.MethodOverloading")
2730
class SchemaGeneratorTest {
2831
@Test
2932
fun `SchemaGenerator generates a simple GraphQL schema`() {
@@ -266,7 +269,7 @@ class SchemaGeneratorTest {
266269

267270
@Test
268271
fun `SchemaGenerator supports Scalar GraphQLID for input types`() {
269-
val schema = toSchema(mutations = listOf(TopLevelObjectDef(MutationWithId())), config = testSchemaConfig)
272+
val schema = toSchema(queries = emptyList(), mutations = listOf(TopLevelObjectDef(MutationWithId())), config = testSchemaConfig)
270273

271274
val furnitureType = schema.getObjectType("Furniture")
272275
val serialField = furnitureType.getFieldDefinition("serial").type as? GraphQLNonNull

0 commit comments

Comments
 (0)