From 677bd95022cdab19c24f72309c26425e29f1dec0 Mon Sep 17 00:00:00 2001 From: vojtapol Date: Mon, 17 Feb 2020 14:27:34 -0500 Subject: [PATCH] Move DirectiveBehavior out of graphql.schema.idl package to allow fix Java 11 module error --- .../kickstart/tools/DirectiveBehavior.kt | 55 +++++++++++++++++++ .../graphql/kickstart/tools/SchemaParser.kt | 1 - .../graphql/schema/idl/DirectiveBehavior.kt | 32 ----------- 3 files changed, 55 insertions(+), 33 deletions(-) create mode 100644 src/main/kotlin/graphql/kickstart/tools/DirectiveBehavior.kt delete mode 100644 src/main/kotlin/graphql/schema/idl/DirectiveBehavior.kt diff --git a/src/main/kotlin/graphql/kickstart/tools/DirectiveBehavior.kt b/src/main/kotlin/graphql/kickstart/tools/DirectiveBehavior.kt new file mode 100644 index 00000000..5d31db25 --- /dev/null +++ b/src/main/kotlin/graphql/kickstart/tools/DirectiveBehavior.kt @@ -0,0 +1,55 @@ +package graphql.kickstart.tools + +import graphql.schema.* +import graphql.schema.idl.RuntimeWiring +import graphql.schema.idl.SchemaGeneratorDirectiveHelper +import graphql.schema.idl.TypeDefinitionRegistry +import kotlin.reflect.full.createInstance + +private val PARAMETERS = Class.forName("graphql.schema.idl.SchemaGeneratorDirectiveHelper\$Parameters") +private val DIRECTIVE_HELPER = SchemaGeneratorDirectiveHelper::class.java + +private val ON_OBJECT_METHOD = DIRECTIVE_HELPER.getMethod("onObject", GraphQLObjectType::class.java, PARAMETERS) +private val ON_INTERFACE_METHOD = DIRECTIVE_HELPER.getMethod("onInterface", GraphQLInterfaceType::class.java, PARAMETERS) +private val ON_UNION_METHOD = DIRECTIVE_HELPER.getMethod("onUnion", GraphQLUnionType::class.java, PARAMETERS) +private val ON_SCALAR_METHOD = DIRECTIVE_HELPER.getMethod("onScalar", GraphQLScalarType::class.java, PARAMETERS) +private val ON_ENUM_METHOD = DIRECTIVE_HELPER.getMethod("onEnum", GraphQLEnumType::class.java, PARAMETERS) +private val ON_INPUT_OBJECT_TYPE = DIRECTIVE_HELPER.getMethod("onInputObjectType", GraphQLInputObjectType::class.java, PARAMETERS) + +/** + * Directive behavior is used to wire up directives during schema parsing. Unfortunately, SchemaGeneratorDirectiveHelper + * which contains the logic has package-private access to some members and must be therefore accessed via reflection. + */ +class DirectiveBehavior { + + private val directiveHelper = SchemaGeneratorDirectiveHelper::class.createInstance() + + fun onObject(element: GraphQLObjectType, params: Params): GraphQLObjectType = + ON_OBJECT_METHOD.invoke(directiveHelper, element, params.toParameters()) as GraphQLObjectType + + fun onInterface(element: GraphQLInterfaceType, params: Params): GraphQLInterfaceType = + ON_INTERFACE_METHOD.invoke(directiveHelper, element, params.toParameters()) as GraphQLInterfaceType + + fun onUnion(element: GraphQLUnionType, params: Params): GraphQLUnionType = + ON_UNION_METHOD.invoke(directiveHelper, element, params.toParameters()) as GraphQLUnionType + + fun onScalar(element: GraphQLScalarType, params: Params): GraphQLScalarType = + ON_SCALAR_METHOD.invoke(directiveHelper, element, params.toParameters()) as GraphQLScalarType + + fun onEnum(element: GraphQLEnumType, params: Params): GraphQLEnumType = + ON_ENUM_METHOD.invoke(directiveHelper, element, params.toParameters()) as GraphQLEnumType + + fun onInputObject(element: GraphQLInputObjectType, params: Params): GraphQLInputObjectType = + ON_INPUT_OBJECT_TYPE.invoke(directiveHelper, element, params.toParameters()) as GraphQLInputObjectType + + data class Params(val runtimeWiring: RuntimeWiring, val codeRegistryBuilder: GraphQLCodeRegistry.Builder) { + internal fun toParameters() = PARAMETERS + .getDeclaredConstructor( + TypeDefinitionRegistry::class.java, + RuntimeWiring::class.java, + Map::class.java, + GraphQLCodeRegistry.Builder::class.java + ).apply { isAccessible = true } + .newInstance(null, runtimeWiring, null, codeRegistryBuilder) + } +} diff --git a/src/main/kotlin/graphql/kickstart/tools/SchemaParser.kt b/src/main/kotlin/graphql/kickstart/tools/SchemaParser.kt index 83d1d4e2..42bde120 100644 --- a/src/main/kotlin/graphql/kickstart/tools/SchemaParser.kt +++ b/src/main/kotlin/graphql/kickstart/tools/SchemaParser.kt @@ -3,7 +3,6 @@ package graphql.kickstart.tools import graphql.introspection.Introspection import graphql.language.* import graphql.schema.* -import graphql.schema.idl.DirectiveBehavior import graphql.schema.idl.RuntimeWiring import graphql.schema.idl.ScalarInfo import graphql.schema.idl.SchemaGeneratorHelper diff --git a/src/main/kotlin/graphql/schema/idl/DirectiveBehavior.kt b/src/main/kotlin/graphql/schema/idl/DirectiveBehavior.kt deleted file mode 100644 index 7e2aa62d..00000000 --- a/src/main/kotlin/graphql/schema/idl/DirectiveBehavior.kt +++ /dev/null @@ -1,32 +0,0 @@ -package graphql.schema.idl - -import graphql.schema.* - -class DirectiveBehavior { - - private val directiveHelper = SchemaGeneratorDirectiveHelper() - - fun onObject(element: GraphQLObjectType, params: Params): GraphQLObjectType { - return directiveHelper.onObject(element, params.toParameters()) - } - - fun onInterface(element: GraphQLInterfaceType, params: Params): GraphQLInterfaceType = - directiveHelper.onInterface(element, params.toParameters()) - - fun onUnion(element: GraphQLUnionType, params: Params): GraphQLUnionType = - directiveHelper.onUnion(element, params.toParameters()) - - fun onScalar(element: GraphQLScalarType, params: Params): GraphQLScalarType = - directiveHelper.onScalar(element, params.toParameters()) - - fun onEnum(element: GraphQLEnumType, params: Params): GraphQLEnumType = - directiveHelper.onEnum(element, params.toParameters()) - - fun onInputObject(element: GraphQLInputObjectType, params: Params): GraphQLInputObjectType = - directiveHelper.onInputObjectType(element, params.toParameters()) - - data class Params(val runtimeWiring: RuntimeWiring, val codeRegistryBuilder: GraphQLCodeRegistry.Builder) { - internal fun toParameters() = SchemaGeneratorDirectiveHelper.Parameters(null, runtimeWiring, null, codeRegistryBuilder) - } - -}