Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/main/kotlin/com/coxautodev/graphql/tools/TypeClassMatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ internal class TypeClassMatcher(private val definitionsByName: Map<String, TypeD
}

private fun stripBatchedType(potentialMatch: PotentialMatch): PotentialMatch {
val realType = potentialMatch.generic.unwrapGenericType(potentialMatch.javaType)

if(realType is ParameterizedType && isListType(realType, potentialMatch)) {
return potentialMatch.copy(javaType = realType.actualTypeArguments.first(), batched = false)
if (potentialMatch.location != Location.RETURN_TYPE) {
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!")
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/test/groovy/com/coxautodev/graphql/tools/EndToEndSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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']]
}
}
4 changes: 4 additions & 0 deletions src/test/kotlin/com/coxautodev/graphql/tools/EndToEndSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type Item implements ItemInterface {
uuid: UUID!
tags(names: [String!]): [Tag!]
batchedName: String!
batchedWithParamsTags(names: [String!]): [Tag!]
}

type OtherItem implements ItemInterface {
Expand Down Expand Up @@ -214,6 +215,9 @@ class ItemResolver : GraphQLResolver<Item> {

@Batched
fun batchedName(items: List<Item>) = items.map { it.name }

@Batched
fun batchedWithParamsTags(items: List<Item>, names: List<String>?): List<List<Tag>> = items.map{ it.tags.filter { names?.contains(it.name) ?: true } }
}

interface ItemInterface {
Expand Down