Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#656 Support non-public datafetcher methods #808

Merged
merged 1 commit into from
Dec 30, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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