Skip to content

Commit dc95d2e

Browse files
committed
Fixed some deprecation warnings
1 parent 5993a02 commit dc95d2e

File tree

6 files changed

+121
-83
lines changed

6 files changed

+121
-83
lines changed

src/main/kotlin/com/coxautodev/graphql/tools/SchemaClassScanner.kt

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import graphql.language.FieldDefinition
88
import graphql.language.InputObjectTypeDefinition
99
import graphql.language.InputValueDefinition
1010
import graphql.language.InterfaceTypeDefinition
11-
import graphql.language.ListType
12-
import graphql.language.NonNullType
1311
import graphql.language.ObjectTypeDefinition
1412
import graphql.language.ObjectTypeExtensionDefinition
1513
import graphql.language.ScalarTypeDefinition
@@ -154,17 +152,22 @@ internal class SchemaClassScanner(initialDictionary: BiMap<String, Class<*>>, al
154152
}.map { definition ->
155153
val provided = scalars[definition.name]
156154
?: throw SchemaClassScannerError("Expected a user-defined GraphQL scalar type with name '${definition.name}' but found none!")
157-
GraphQLScalarType(provided.name,
158-
if (definition.description != null) definition.description.content else SchemaParser.getDocumentation(definition)
159-
?: provided.description, provided.coercing, listOf(), definition)
155+
GraphQLScalarType.newScalar()
156+
.name(provided.name)
157+
.description(
158+
if (definition.description != null) definition.description.content
159+
else SchemaParser.getDocumentation(definition) ?: provided.description)
160+
.coercing(provided.coercing)
161+
.definition(definition)
162+
.build()
160163
}.associateBy { it.name!! }
161164

162165
val unusedDefinitions = (definitionsByName.values - observedDefinitions).toSet()
163166
unusedDefinitions
164167
.filter { definition -> definition.name != "PageInfo" }
165168
.forEach { definition ->
166-
log.warn("Schema type was defined but can never be accessed, and can be safely deleted: ${definition.name}")
167-
}
169+
log.warn("Schema type was defined but can never be accessed, and can be safely deleted: ${definition.name}")
170+
}
168171

169172
val fieldResolvers = fieldResolversByType.flatMap { it.value.map { it.value } }
170173
val observedNormalResolverInfos = fieldResolvers.map { it.resolverInfo }.distinct().filterIsInstance<NormalResolverInfo>()
@@ -242,26 +245,27 @@ internal class SchemaClassScanner(initialDictionary: BiMap<String, Class<*>>, al
242245
} else {
243246
resolverInfosByDataClass[item.clazz] ?: DataClassResolverInfo(item.clazz)
244247
}
245-
}) ?: throw throw SchemaClassScannerError("The GraphQL schema type '${item.type.name}' maps to a field of type java.lang.Object however there is no matching entry for this type in the type dictionary. You may need to add this type to the dictionary before building the schema.")
248+
})
249+
?: throw throw SchemaClassScannerError("The GraphQL schema type '${item.type.name}' maps to a field of type java.lang.Object however there is no matching entry for this type in the type dictionary. You may need to add this type to the dictionary before building the schema.")
246250

247251
scanResolverInfoForPotentialMatches(item.type, resolverInfo)
248252
}
249253

250254
private fun scanResolverInfoForPotentialMatches(type: ObjectTypeDefinition, resolverInfo: ResolverInfo) {
251255
type.getExtendedFieldDefinitions(extensionDefinitions).forEach { field ->
252-
// val searchField = applyDirective(field)
256+
// val searchField = applyDirective(field)
253257
val fieldResolver = fieldResolverScanner.findFieldResolver(field, resolverInfo)
254258

255259
fieldResolversByType.getOrPut(type) { mutableMapOf() }[fieldResolver.field] = fieldResolver
256260

257261
fieldResolver.scanForMatches().forEach { potentialMatch ->
258-
// if (potentialMatch.graphQLType is TypeName && !definitionsByName.containsKey((potentialMatch.graphQLType.name))) {
262+
// if (potentialMatch.graphQLType is TypeName && !definitionsByName.containsKey((potentialMatch.graphQLType.name))) {
259263
// val typeDefinition = ObjectTypeDefinition.newObjectTypeDefinition()
260264
// .name(potentialMatch.graphQLType.name)
261265
// .build()
262266
// handleFoundType(TypeClassMatcher.ValidMatch(typeDefinition, typeClassMatcher.toRealType(potentialMatch), potentialMatch.reference))
263267
// } else {
264-
handleFoundType(typeClassMatcher.match(potentialMatch))
268+
handleFoundType(typeClassMatcher.match(potentialMatch))
265269
// }
266270
}
267271
}

src/main/kotlin/com/coxautodev/graphql/tools/SchemaObjects.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.coxautodev.graphql.tools
22

3+
import graphql.schema.GraphQLCodeRegistry
34
import graphql.schema.GraphQLObjectType
45
import graphql.schema.GraphQLSchema
56
import graphql.schema.GraphQLType
@@ -21,7 +22,11 @@ data class SchemaObjects(val query: GraphQLObjectType, val mutation: GraphQLObje
2122
.additionalTypes(dictionary)
2223

2324
if (!introspectionEnabled) {
24-
builder.fieldVisibility(NoIntrospectionGraphqlFieldVisibility.NO_INTROSPECTION_FIELD_VISIBILITY)
25+
builder.codeRegistry(
26+
GraphQLCodeRegistry.newCodeRegistry()
27+
.fieldVisibility(NoIntrospectionGraphqlFieldVisibility.NO_INTROSPECTION_FIELD_VISIBILITY)
28+
.build()
29+
)
2530
}
2631

2732
return builder.build()

src/main/kotlin/com/coxautodev/graphql/tools/SchemaParserBuilder.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ data class SchemaParserOptions internal constructor(
290290
fun newOptions() = Builder()
291291

292292
@JvmStatic
293+
@ExperimentalCoroutinesApi
293294
fun defaultOptions() = Builder().build()
294295
}
295296

src/main/kotlin/graphql/schema/idl/DirectiveBehavior.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class DirectiveBehavior {
1919
return directiveHelper.onObject(element, params.toParameters())
2020
}
2121

22+
@Suppress("UNUSED_PARAMETER")
2223
fun onField(element: GraphQLFieldDefinition, params: Params): GraphQLFieldDefinition {
2324
// noop, since the actual behaviour has moved to onObject/onInterface since graphql-java 12.0
2425
return element
@@ -36,11 +37,13 @@ class DirectiveBehavior {
3637
fun onEnum(element: GraphQLEnumType, params: Params): GraphQLEnumType =
3738
directiveHelper.onEnum(element, params.toParameters())
3839

40+
@Suppress("UNUSED_PARAMETER")
3941
fun onEnumValue(element: GraphQLEnumValueDefinition, params: Params): GraphQLEnumValueDefinition {
4042
// noop, since the actual behaviour has moved to onEnum since graphql-java 12.0
4143
return element
4244
}
4345

46+
@Suppress("UNUSED_PARAMETER")
4447
fun onArgument(element: GraphQLArgument, params: Params): GraphQLArgument {
4548
// noop, since the actual behaviour has moved to onObject/onInterface since graphql-java 12.0
4649
return element
@@ -49,6 +52,7 @@ class DirectiveBehavior {
4952
fun onInputObject(element: GraphQLInputObjectType, params: Params): GraphQLInputObjectType =
5053
directiveHelper.onInputObjectType(element, params.toParameters())
5154

55+
@Suppress("UNUSED_PARAMETER")
5256
fun onInputObjectField(element: GraphQLInputObjectField, params: Params): GraphQLInputObjectField {
5357
// noop, since the actual behaviour has moved to onInputObjectType since graphql-java 12.0
5458
return element

src/test/kotlin/com/coxautodev/graphql/tools/EndToEndSpecHelper.kt

Lines changed: 90 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ import graphql.language.StringValue
77
import graphql.schema.Coercing
88
import graphql.schema.DataFetchingEnvironment
99
import graphql.schema.GraphQLScalarType
10-
import kotlinx.coroutines.*
10+
import kotlinx.coroutines.CompletableDeferred
1111
import kotlinx.coroutines.channels.Channel
1212
import kotlinx.coroutines.channels.ReceiveChannel
13+
import kotlinx.coroutines.coroutineScope
1314
import org.reactivestreams.Publisher
14-
import java.util.Optional
15-
import java.util.UUID
15+
import java.util.*
1616
import java.util.concurrent.CompletableFuture
1717

1818
fun createSchema() = SchemaParser.newParser()
19-
.schemaString(schemaDefinition)
20-
.resolvers(Query(), Mutation(), Subscription(), ItemResolver(), UnusedRootResolver(), UnusedResolver())
21-
.scalars(customScalarUUID, customScalarMap, customScalarId)
22-
.dictionary("OtherItem", OtherItemWithWrongName::class)
23-
.dictionary("ThirdItem", ThirdItem::class)
24-
.dictionary("ComplexMapItem", ComplexMapItem::class)
25-
.dictionary("NestedComplexMapItem", NestedComplexMapItem::class)
26-
.build()
27-
.makeExecutableSchema()
19+
.schemaString(schemaDefinition)
20+
.resolvers(Query(), Mutation(), Subscription(), ItemResolver(), UnusedRootResolver(), UnusedResolver())
21+
.scalars(customScalarUUID, customScalarMap, customScalarId)
22+
.dictionary("OtherItem", OtherItemWithWrongName::class)
23+
.dictionary("ThirdItem", ThirdItem::class)
24+
.dictionary("ComplexMapItem", ComplexMapItem::class)
25+
.dictionary("NestedComplexMapItem", NestedComplexMapItem::class)
26+
.build()
27+
.makeExecutableSchema()
2828

2929
val schemaDefinition = """
3030
@@ -210,13 +210,13 @@ type ItemWithGenericProperties {
210210

211211

212212
val items = mutableListOf(
213-
Item(0, "item1", Type.TYPE_1, UUID.fromString("38f685f1-b460-4a54-a17f-7fd69e8cf3f8"), listOf(Tag(1, "item1-tag1"), Tag(2, "item1-tag2"))),
214-
Item(1, "item2", Type.TYPE_2, UUID.fromString("38f685f1-b460-4a54-b17f-7fd69e8cf3f8"), listOf(Tag(3, "item2-tag1"), Tag(4, "item2-tag2")))
213+
Item(0, "item1", Type.TYPE_1, UUID.fromString("38f685f1-b460-4a54-a17f-7fd69e8cf3f8"), listOf(Tag(1, "item1-tag1"), Tag(2, "item1-tag2"))),
214+
Item(1, "item2", Type.TYPE_2, UUID.fromString("38f685f1-b460-4a54-b17f-7fd69e8cf3f8"), listOf(Tag(3, "item2-tag1"), Tag(4, "item2-tag2")))
215215
)
216216

217217
val otherItems = mutableListOf(
218-
OtherItemWithWrongName(0, "otherItem1", Type.TYPE_1, UUID.fromString("38f685f1-b460-4a54-c17f-7fd69e8cf3f8")),
219-
OtherItemWithWrongName(1, "otherItem2", Type.TYPE_2, UUID.fromString("38f685f1-b460-4a54-d17f-7fd69e8cf3f8"))
218+
OtherItemWithWrongName(0, "otherItem1", Type.TYPE_1, UUID.fromString("38f685f1-b460-4a54-c17f-7fd69e8cf3f8")),
219+
OtherItemWithWrongName(1, "otherItem2", Type.TYPE_2, UUID.fromString("38f685f1-b460-4a54-d17f-7fd69e8cf3f8"))
220220
)
221221

222222
val thirdItems = mutableListOf(
@@ -244,7 +244,7 @@ val propertyMapWithNestedComplexItems = mutableListOf(
244244
hashMapOf("nested" to NestedComplexMapItem(UndiscoveredItem(63)), "age" to 72)
245245
)
246246

247-
class Query: GraphQLQueryResolver, ListListResolver<String>() {
247+
class Query : GraphQLQueryResolver, ListListResolver<String>() {
248248
fun isEmpty() = items.isEmpty()
249249
fun allBaseItems() = items
250250
fun items(input: ItemSearchInput): List<Item> = items.filter { it.name == input.name }
@@ -257,8 +257,9 @@ class Query: GraphQLQueryResolver, ListListResolver<String>() {
257257
fun itemByBuiltInId(id: UUID): Item? {
258258
return items.find { it.uuid == id }
259259
}
260-
fun itemsWithOptionalInput(input: ItemSearchInput?) = if(input == null) items else items(input)
261-
fun itemsWithOptionalInputExplicit(input: Optional<ItemSearchInput>) = if(input.isPresent) items(input.get()) else items
260+
261+
fun itemsWithOptionalInput(input: ItemSearchInput?) = if (input == null) items else items(input)
262+
fun itemsWithOptionalInputExplicit(input: Optional<ItemSearchInput>) = if (input.isPresent) items(input.get()) else items
262263
fun enumInputType(type: Type) = type
263264
fun customScalarMapInputType(customScalarMap: Map<String, Any>) = customScalarMap
264265
fun itemWithGenericProperties() = ItemWithGenericProperties(listOf("A", "B"))
@@ -269,7 +270,9 @@ class Query: GraphQLQueryResolver, ListListResolver<String>() {
269270
fun futureItems() = CompletableFuture.completedFuture(items)
270271
fun complexNullableType(): ComplexNullable? = null
271272

272-
fun complexInputType(input: List<List<ComplexInputType>?>?) = input?.firstOrNull()?.firstOrNull()?.let { it.first == "foo" && it.second?.firstOrNull()?.firstOrNull()?.first == "bar" } ?: false
273+
fun complexInputType(input: List<List<ComplexInputType>?>?) = input?.firstOrNull()?.firstOrNull()?.let { it.first == "foo" && it.second?.firstOrNull()?.firstOrNull()?.first == "bar" }
274+
?: false
275+
273276
fun extendedType() = ExtendedType()
274277

275278
fun getItemsWithGetResolver() = items
@@ -286,16 +289,16 @@ class Query: GraphQLQueryResolver, ListListResolver<String>() {
286289
private val propertyField = "test"
287290

288291
fun dataFetcherResult(): DataFetcherResult<Item> {
289-
return DataFetcherResult(items.first(), listOf())
292+
return DataFetcherResult.newResult<Item>().data(items.first()).build()
290293
}
291294

292295
suspend fun coroutineItems(): List<Item> = CompletableDeferred(items).await()
293296

294297
fun arrayItems() = items.toTypedArray()
295298
}
296299

297-
class UnusedRootResolver: GraphQLQueryResolver
298-
class UnusedResolver: GraphQLResolver<String>
300+
class UnusedRootResolver : GraphQLQueryResolver
301+
class UnusedResolver : GraphQLResolver<String>
299302

300303
class ExtendedType {
301304
fun first() = "test"
@@ -306,7 +309,7 @@ abstract class ListListResolver<out E> {
306309
fun listList(): List<List<E>> = listOf(listOf())
307310
}
308311

309-
class Mutation: GraphQLMutationResolver {
312+
class Mutation : GraphQLMutationResolver {
310313
fun addItem(input: NewItemInput): Item {
311314
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
312315
}
@@ -316,10 +319,10 @@ class OnItemCreatedContext(val newItem: Item)
316319

317320
class Subscription : GraphQLSubscriptionResolver {
318321
fun onItemCreated(env: DataFetchingEnvironment) =
319-
Publisher<Item> { subscriber ->
320-
subscriber.onNext(env.getContext<OnItemCreatedContext>().newItem)
322+
Publisher<Item> { subscriber ->
323+
subscriber.onNext(env.getContext<OnItemCreatedContext>().newItem)
321324
// subscriber.onComplete()
322-
}
325+
}
323326

324327
fun onItemCreatedCoroutineChannel(env: DataFetchingEnvironment): ReceiveChannel<Item> {
325328
val channel = Channel<Item>(1)
@@ -343,7 +346,11 @@ class ItemResolver : GraphQLResolver<Item> {
343346
fun batchedName(items: List<Item>) = items.map { it.name }
344347

345348
@Batched
346-
fun batchedWithParamsTags(items: List<Item>, names: List<String>?): List<List<Tag>> = items.map{ it.tags.filter { names?.contains(it.name) ?: true } }
349+
fun batchedWithParamsTags(items: List<Item>, names: List<String>?): List<List<Tag>> = items.map {
350+
it.tags.filter {
351+
names?.contains(it.name) ?: true
352+
}
353+
}
347354
}
348355

349356
interface ItemInterface {
@@ -353,8 +360,8 @@ interface ItemInterface {
353360
}
354361

355362
enum class Type { TYPE_1, TYPE_2 }
356-
data class Item(val id: Int, override val name: String, override val type: Type, override val uuid:UUID, val tags: List<Tag>) : ItemInterface
357-
data class OtherItemWithWrongName(val id: Int, override val name: String, override val type: Type, override val uuid:UUID) : ItemInterface
363+
data class Item(val id: Int, override val name: String, override val type: Type, override val uuid: UUID, val tags: List<Tag>) : ItemInterface
364+
data class OtherItemWithWrongName(val id: Int, override val name: String, override val type: Type, override val uuid: UUID) : ItemInterface
358365
data class ThirdItem(val id: Int)
359366
data class ComplexMapItem(val id: Int)
360367
data class UndiscoveredItem(val id: Int)
@@ -367,44 +374,56 @@ data class ComplexInputType(val first: String, val second: List<List<ComplexInpu
367374
data class ComplexInputTypeTwo(val first: String)
368375
data class ItemWithGenericProperties(val keys: List<String>)
369376

370-
val customScalarId = GraphQLScalarType("ID", "Overrides built-in ID", object : Coercing<UUID, String> {
371-
override fun serialize(input: Any): String? = when (input) {
372-
is String -> input
373-
is UUID -> input.toString()
374-
else -> null
375-
}
376-
377-
override fun parseValue(input: Any): UUID? = parseLiteral(input)
378-
379-
override fun parseLiteral(input: Any): UUID? = when (input) {
380-
is StringValue -> UUID.fromString(input.value)
381-
else -> null
382-
}
383-
})
384-
385-
val customScalarUUID = GraphQLScalarType("UUID", "UUID", object : Coercing<UUID, String> {
386-
387-
override fun serialize(input: Any): String? = when (input) {
388-
is String -> input
389-
is UUID -> input.toString()
390-
else -> null
391-
}
392-
393-
override fun parseValue(input: Any): UUID? = parseLiteral(input)
394-
395-
override fun parseLiteral(input: Any): UUID? = when (input) {
396-
is StringValue -> UUID.fromString(input.value)
397-
else -> null
398-
}
399-
})
400-
401-
val customScalarMap = GraphQLScalarType("customScalarMap", "customScalarMap", object: Coercing<Map<String, Any>, Map<String, Any>> {
402-
403-
@Suppress("UNCHECKED_CAST")
404-
override fun parseValue(input: Any?): Map<String, Any> = input as Map<String, Any>
405-
406-
@Suppress("UNCHECKED_CAST")
407-
override fun serialize(dataFetcherResult: Any?): Map<String, Any> = dataFetcherResult as Map<String, Any>
408-
409-
override fun parseLiteral(input: Any?): Map<String, Any> = (input as ObjectValue).objectFields.associateBy { it.name }.mapValues { (it.value.value as StringValue).value }
410-
})
377+
val customScalarId = GraphQLScalarType.newScalar()
378+
.name("ID")
379+
.description("Overrides built-in ID")
380+
.coercing(object : Coercing<UUID, String> {
381+
override fun serialize(input: Any): String? = when (input) {
382+
is String -> input
383+
is UUID -> input.toString()
384+
else -> null
385+
}
386+
387+
override fun parseValue(input: Any): UUID? = parseLiteral(input)
388+
389+
override fun parseLiteral(input: Any): UUID? = when (input) {
390+
is StringValue -> UUID.fromString(input.value)
391+
else -> null
392+
}
393+
})
394+
.build()
395+
396+
val customScalarUUID = GraphQLScalarType.newScalar()
397+
.name("UUID")
398+
.description("UUID")
399+
.coercing(object : Coercing<UUID, String> {
400+
401+
override fun serialize(input: Any): String? = when (input) {
402+
is String -> input
403+
is UUID -> input.toString()
404+
else -> null
405+
}
406+
407+
override fun parseValue(input: Any): UUID? = parseLiteral(input)
408+
409+
override fun parseLiteral(input: Any): UUID? = when (input) {
410+
is StringValue -> UUID.fromString(input.value)
411+
else -> null
412+
}
413+
})
414+
.build()
415+
416+
val customScalarMap = GraphQLScalarType.newScalar()
417+
.name("customScalarMap")
418+
.description("customScalarMap")
419+
.coercing(object : Coercing<Map<String, Any>, Map<String, Any>> {
420+
421+
@Suppress("UNCHECKED_CAST")
422+
override fun parseValue(input: Any?): Map<String, Any> = input as Map<String, Any>
423+
424+
@Suppress("UNCHECKED_CAST")
425+
override fun serialize(dataFetcherResult: Any?): Map<String, Any> = dataFetcherResult as Map<String, Any>
426+
427+
override fun parseLiteral(input: Any?): Map<String, Any> = (input as ObjectValue).objectFields.associateBy { it.name }.mapValues { (it.value.value as StringValue).value }
428+
})
429+
.build()

0 commit comments

Comments
 (0)