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

Update directive wiring environment #684

Merged
merged 1 commit into from
Aug 12, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,21 @@ class DirectiveWiringHelper(
private fun <T : GraphQLDirectiveContainer> wireDirectives(wrapper: WiringWrapper<T>): T {
val directivesContainer = wrapper.graphQlType.definition as DirectivesContainer<*>
val directives = buildDirectives(directivesContainer.directives, wrapper.directiveLocation)
val directivesByName = directives.associateBy { it.name }
var output = wrapper.graphQlType
// first the specific named directives
directives.forEach { directive ->
val env = buildEnvironment(wrapper, directives, directive)
val wiring = runtimeWiring.registeredDirectiveWiring[directive.name]
wrapper.graphQlType.appliedDirectives.forEach { appliedDirective ->
val env = buildEnvironment(wrapper, directives, directivesByName[appliedDirective.name], appliedDirective)
val wiring = runtimeWiring.registeredDirectiveWiring[appliedDirective.name]
wiring?.let { output = wrapper.invoker(it, env) }
}
// now call any statically added to the runtime
runtimeWiring.directiveWiring.forEach { staticWiring ->
val env = buildEnvironment(wrapper, directives, null)
val env = buildEnvironment(wrapper, directives, null, null)
output = wrapper.invoker(staticWiring, env)
}
// wiring factory is last (if present)
val env = buildEnvironment(wrapper, directives, null)
val env = buildEnvironment(wrapper, directives, null, null)
if (runtimeWiring.wiringFactory.providesSchemaDirectiveWiring(env)) {
val factoryWiring = runtimeWiring.wiringFactory.getSchemaDirectiveWiring(env)
output = wrapper.invoker(factoryWiring, env)
Expand Down Expand Up @@ -131,7 +132,7 @@ class DirectiveWiringHelper(
return output
}

private fun <T : GraphQLDirectiveContainer> buildEnvironment(wrapper: WiringWrapper<T>, directives: List<GraphQLDirective>, directive: GraphQLDirective?): SchemaDirectiveWiringEnvironmentImpl<T> {
private fun <T : GraphQLDirectiveContainer> buildEnvironment(wrapper: WiringWrapper<T>, directives: List<GraphQLDirective>, directive: GraphQLDirective?, appliedDirective: GraphQLAppliedDirective?): SchemaDirectiveWiringEnvironmentImpl<T> {
val nodeParentTree = buildAstTree(*listOfNotNull(
wrapper.fieldsContainer?.definition,
wrapper.inputFieldsContainer?.definition,
Expand All @@ -154,7 +155,7 @@ class DirectiveWiringHelper(
is GraphQLFieldsContainer -> schemaDirectiveParameters.newParams(wrapper.graphQlType, nodeParentTree, elementParentTree)
else -> schemaDirectiveParameters.newParams(nodeParentTree, elementParentTree)
}
return SchemaDirectiveWiringEnvironmentImpl(wrapper.graphQlType, directives, wrapper.graphQlType.appliedDirectives, directive, params)
return SchemaDirectiveWiringEnvironmentImpl(wrapper.graphQlType, directives, wrapper.graphQlType.appliedDirectives, directive, appliedDirective, params)
}

fun buildDirectiveInputType(value: Value<*>): GraphQLInputType? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import graphql.schema.*
import graphql.schema.idl.RuntimeWiring
import graphql.schema.idl.SchemaDirectiveWiringEnvironment
import graphql.schema.idl.TypeDefinitionRegistry
import graphql.util.FpKit

class SchemaDirectiveWiringEnvironmentImpl<T : GraphQLDirectiveContainer?>(
private val element: T,
directives: List<GraphQLDirective>,
appliedDirectives: List<GraphQLAppliedDirective>,
private val registeredDirective: GraphQLDirective?,
private val registeredAppliedDirective: GraphQLAppliedDirective?,
parameters: Parameters
) : SchemaDirectiveWiringEnvironment<T> {
private val directives: Map<String, GraphQLDirective>
Expand All @@ -27,8 +27,8 @@ class SchemaDirectiveWiringEnvironmentImpl<T : GraphQLDirectiveContainer?>(

init {
typeDefinitionRegistry = parameters.typeRegistry
this.directives = FpKit.getByName(directives) { obj: GraphQLDirective -> obj.name }
this.appliedDirectives = FpKit.getByName(appliedDirectives) { obj: GraphQLAppliedDirective -> obj.name }
this.directives = directives.associateBy { it.name }
this.appliedDirectives = appliedDirectives.associateBy { it.name }
context = parameters.context
codeRegistry = parameters.codeRegistry
nodeParentTree = parameters.nodeParentTree
Expand All @@ -39,7 +39,7 @@ class SchemaDirectiveWiringEnvironmentImpl<T : GraphQLDirectiveContainer?>(

override fun getElement(): T = element
override fun getDirective(): GraphQLDirective? = registeredDirective
override fun getAppliedDirective(): GraphQLAppliedDirective? = appliedDirectives[registeredDirective?.name]
override fun getAppliedDirective(): GraphQLAppliedDirective? = registeredAppliedDirective
override fun getDirectives(): Map<String, GraphQLDirective> = LinkedHashMap(directives)
override fun getDirective(directiveName: String): GraphQLDirective = directives[directiveName]!!
override fun getAppliedDirectives(): Map<String, GraphQLAppliedDirective> = appliedDirectives
Expand Down