Skip to content

Commit

Permalink
Merge pull request #1202 from Netflix/feature/duplicate-fetcher-valid…
Browse files Browse the repository at this point in the history
…ation

Add duplicate data fetcher validation
  • Loading branch information
srinivasankavitha committed Aug 26, 2022
2 parents b7ca0d3 + 8ef30b0 commit eb6493e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Expand Up @@ -289,6 +289,11 @@ class DgsSchemaProvider(
val field = dgsDataAnnotation.getString("field").ifEmpty { method.name }
val parentType = dgsDataAnnotation.getString("parentType")

if (dataFetchers.any { it.parentType == parentType && it.field == field }) {
logger.error("Duplicate data fetchers registered for $parentType.$field")
throw InvalidDgsConfigurationException("Duplicate data fetchers registered for $parentType.$field")
}

dataFetchers.add(DataFetcherReference(dgsComponent, method, mergedAnnotations, parentType, field))

val enableInstrumentation =
Expand Down
Expand Up @@ -16,6 +16,7 @@

package com.netflix.graphql.dgs

import com.netflix.graphql.dgs.exceptions.InvalidDgsConfigurationException
import com.netflix.graphql.dgs.exceptions.InvalidTypeResolverException
import com.netflix.graphql.dgs.exceptions.NoSchemaFoundException
import com.netflix.graphql.dgs.internal.DefaultInputObjectMapper
Expand Down Expand Up @@ -229,6 +230,27 @@ internal class DgsSchemaProviderTest {
verifyComponents()
}

@Test
fun withDuplicateFetchers() {
val fetcher = object : Any() {
@DgsData(parentType = "Query", field = "hello")
fun fetcher1(): String {
return "fetcher1"
}
@DgsData(parentType = "Query", field = "hello")
fun fetcher2(): String {
return "fetcher2"
}
}

withComponents("helloFetcher" to fetcher)

val error: InvalidDgsConfigurationException = assertThrows {
GraphQL.newGraphQL(schemaProvider().schema()).build()
}
assertThat(error.message).isEqualTo("Duplicate data fetchers registered for Query.hello")
}

open class BaseClassFetcher {
@DgsData(parentType = "Query", field = "hello")
private fun someFetcher(): String {
Expand Down

0 comments on commit eb6493e

Please sign in to comment.