From c4e028158650ebd03ff6cc07e1bec5f9487e5f16 Mon Sep 17 00:00:00 2001 From: Pavel Pavlov Date: Sun, 3 Dec 2017 13:03:33 +0300 Subject: [PATCH 1/3] Expect only first parameter as List (fixes #93) --- .../coxautodev/graphql/tools/TypeClassMatcher.kt | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/coxautodev/graphql/tools/TypeClassMatcher.kt b/src/main/kotlin/com/coxautodev/graphql/tools/TypeClassMatcher.kt index 7acdfc94..7387cbfd 100644 --- a/src/main/kotlin/com/coxautodev/graphql/tools/TypeClassMatcher.kt +++ b/src/main/kotlin/com/coxautodev/graphql/tools/TypeClassMatcher.kt @@ -91,12 +91,15 @@ internal class TypeClassMatcher(private val definitionsByName: Map 0) { + return potentialMatch.copy(javaType = potentialMatch.javaType, batched = false) } else { - throw error(potentialMatch, "Method was marked as @Batched but ${potentialMatch.location.prettyName} was not a list!") + val realType = potentialMatch.generic.unwrapGenericType(potentialMatch.javaType) + if (realType is ParameterizedType && isListType(realType, potentialMatch)) { + return potentialMatch.copy(javaType = realType.actualTypeArguments.first(), batched = false) + } else { + throw error(potentialMatch, "Method was marked as @Batched but ${potentialMatch.location.prettyName} was not a list!") + } } } From 18e136de86fba8a91443ef67b06bd9eaadfb847d Mon Sep 17 00:00:00 2001 From: Pavel Pavlov Date: Thu, 25 Jan 2018 23:28:16 +0300 Subject: [PATCH 2/3] Expect only RETURN_TYPE to be List (fixes #93) --- .../kotlin/com/coxautodev/graphql/tools/TypeClassMatcher.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/coxautodev/graphql/tools/TypeClassMatcher.kt b/src/main/kotlin/com/coxautodev/graphql/tools/TypeClassMatcher.kt index 7387cbfd..e2069792 100644 --- a/src/main/kotlin/com/coxautodev/graphql/tools/TypeClassMatcher.kt +++ b/src/main/kotlin/com/coxautodev/graphql/tools/TypeClassMatcher.kt @@ -91,7 +91,7 @@ internal class TypeClassMatcher(private val definitionsByName: Map 0) { + if (potentialMatch.location != Location.RETURN_TYPE) { return potentialMatch.copy(javaType = potentialMatch.javaType, batched = false) } else { val realType = potentialMatch.generic.unwrapGenericType(potentialMatch.javaType) From 6d4990585c8a3ecf49e8c4e1ca9586eb368e7482 Mon Sep 17 00:00:00 2001 From: Pavel Pavlov Date: Thu, 25 Jan 2018 23:29:38 +0300 Subject: [PATCH 3/3] Added tests to check batched methods with parameters --- .../graphql/tools/EndToEndSpec.groovy | 18 ++++++++++++++++++ .../coxautodev/graphql/tools/EndToEndSpec.kt | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/src/test/groovy/com/coxautodev/graphql/tools/EndToEndSpec.groovy b/src/test/groovy/com/coxautodev/graphql/tools/EndToEndSpec.groovy index 4b357456..f55bfe1d 100644 --- a/src/test/groovy/com/coxautodev/graphql/tools/EndToEndSpec.groovy +++ b/src/test/groovy/com/coxautodev/graphql/tools/EndToEndSpec.groovy @@ -375,4 +375,22 @@ class EndToEndSpec extends Specification { then: data.allBaseItems.collect { it.name } == ['item1', 'item2'] } + + def "generated schema supports batched datafetchers with params"() { + when: + def data = Utils.assertNoGraphQlErrors(gql) { + ''' + { + allBaseItems { + tags: batchedWithParamsTags(names: ["item2-tag1"]) { + name + } + } + } + ''' + } + + then: + data.allBaseItems.collect { it.tags.collect { it.name } } == [[], ['item2-tag1']] + } } diff --git a/src/test/kotlin/com/coxautodev/graphql/tools/EndToEndSpec.kt b/src/test/kotlin/com/coxautodev/graphql/tools/EndToEndSpec.kt index 815da309..d2755f82 100644 --- a/src/test/kotlin/com/coxautodev/graphql/tools/EndToEndSpec.kt +++ b/src/test/kotlin/com/coxautodev/graphql/tools/EndToEndSpec.kt @@ -109,6 +109,7 @@ type Item implements ItemInterface { uuid: UUID! tags(names: [String!]): [Tag!] batchedName: String! + batchedWithParamsTags(names: [String!]): [Tag!] } type OtherItem implements ItemInterface { @@ -214,6 +215,9 @@ class ItemResolver : GraphQLResolver { @Batched fun batchedName(items: List) = items.map { it.name } + + @Batched + fun batchedWithParamsTags(items: List, names: List?): List> = items.map{ it.tags.filter { names?.contains(it.name) ?: true } } } interface ItemInterface {