Skip to content

Commit

Permalink
Merge pull request #808 from Netflix/non-public-datafetcher
Browse files Browse the repository at this point in the history
#656 Support non-public datafetcher methods
  • Loading branch information
berngp committed Dec 30, 2021
2 parents 3ca9d8d + a9043c9 commit d9898fb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class DataFetcherInvoker(

launch.asCompletableFuture()
} else {
ReflectionUtils.makeAccessible(method)
ReflectionUtils.invokeMethod(method, dgsComponent, *args.toTypedArray())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,17 @@ import graphql.execution.DataFetcherExceptionHandler
import graphql.language.InterfaceTypeDefinition
import graphql.language.TypeName
import graphql.language.UnionTypeDefinition
import graphql.schema.*
import graphql.schema.idl.*
import graphql.schema.Coercing
import graphql.schema.DataFetcher
import graphql.schema.FieldCoordinates
import graphql.schema.GraphQLCodeRegistry
import graphql.schema.GraphQLScalarType
import graphql.schema.GraphQLSchema
import graphql.schema.idl.RuntimeWiring
import graphql.schema.idl.SchemaDirectiveWiring
import graphql.schema.idl.SchemaParser
import graphql.schema.idl.TypeDefinitionRegistry
import graphql.schema.idl.TypeRuntimeWiring
import graphql.schema.visibility.DefaultGraphqlFieldVisibility
import graphql.schema.visibility.GraphqlFieldVisibility
import org.slf4j.Logger
Expand Down Expand Up @@ -188,8 +197,7 @@ class DgsSchemaProvider(
) {
dgsComponents.forEach { dgsComponent ->
val javaClass = AopUtils.getTargetClass(dgsComponent)

javaClass.methods.asSequence()
ReflectionUtils.getUniqueDeclaredMethods(javaClass).asSequence()
.filter { method ->
MergedAnnotations.from(method, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.isPresent(DgsData::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,62 @@ internal class DgsSchemaProviderTest {
verify { applicationContextMock.getBeansWithAnnotation(DgsComponent::class.java) }
}

@Test
fun addPrivateFetchers() {
val fetcher = object : Any() {
@DgsData(parentType = "Query", field = "hello")
private fun someFetcher(dfe: DataFetchingEnvironment): String {
return "Hello"
}
}

every { applicationContextMock.getBeansWithAnnotation(DgsComponent::class.java) } returns mapOf(
Pair(
"helloFetcher",
fetcher
)
)
every { applicationContextMock.getBeansWithAnnotation(DgsScalar::class.java) } returns emptyMap()
every { applicationContextMock.getBeansWithAnnotation(DgsDirective::class.java) } returns emptyMap()

val provider = DgsSchemaProvider(applicationContextMock, Optional.empty(), Optional.empty(), Optional.empty())
val schema = provider.schema()
val build = GraphQL.newGraphQL(schema).build()
assertHello(build)

verify { applicationContextMock.getBeansWithAnnotation(DgsComponent::class.java) }
}

open class BaseClassFetcher {
@DgsData(parentType = "Query", field = "hello")
private fun someFetcher(dfe: DataFetchingEnvironment): String {
return "Hello"
}
}

@Test
fun addSubClassFetchers() {
val fetcher = object : BaseClassFetcher() {
// We're only interested in the base class for this test
}

every { applicationContextMock.getBeansWithAnnotation(DgsComponent::class.java) } returns mapOf(
Pair(
"helloFetcher",
fetcher
)
)
every { applicationContextMock.getBeansWithAnnotation(DgsScalar::class.java) } returns emptyMap()
every { applicationContextMock.getBeansWithAnnotation(DgsDirective::class.java) } returns emptyMap()

val provider = DgsSchemaProvider(applicationContextMock, Optional.empty(), Optional.empty(), Optional.empty())
val schema = provider.schema()
val build = GraphQL.newGraphQL(schema).build()
assertHello(build)

verify { applicationContextMock.getBeansWithAnnotation(DgsComponent::class.java) }
}

@Test
fun addDefaultTypeResolvers() {
val schema = """
Expand Down

0 comments on commit d9898fb

Please sign in to comment.