Skip to content

Commit 722bb45

Browse files
authored
Merge pull request #684 from graphql-java-kickstart/directive-wiring-env
Update directive wiring environment
2 parents 8307b63 + c2ca16b commit 722bb45

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/main/kotlin/graphql/kickstart/tools/directive/DirectiveWiringHelper.kt

+8-7
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,21 @@ class DirectiveWiringHelper(
7777
private fun <T : GraphQLDirectiveContainer> wireDirectives(wrapper: WiringWrapper<T>): T {
7878
val directivesContainer = wrapper.graphQlType.definition as DirectivesContainer<*>
7979
val directives = buildDirectives(directivesContainer.directives, wrapper.directiveLocation)
80+
val directivesByName = directives.associateBy { it.name }
8081
var output = wrapper.graphQlType
8182
// first the specific named directives
82-
directives.forEach { directive ->
83-
val env = buildEnvironment(wrapper, directives, directive)
84-
val wiring = runtimeWiring.registeredDirectiveWiring[directive.name]
83+
wrapper.graphQlType.appliedDirectives.forEach { appliedDirective ->
84+
val env = buildEnvironment(wrapper, directives, directivesByName[appliedDirective.name], appliedDirective)
85+
val wiring = runtimeWiring.registeredDirectiveWiring[appliedDirective.name]
8586
wiring?.let { output = wrapper.invoker(it, env) }
8687
}
8788
// now call any statically added to the runtime
8889
runtimeWiring.directiveWiring.forEach { staticWiring ->
89-
val env = buildEnvironment(wrapper, directives, null)
90+
val env = buildEnvironment(wrapper, directives, null, null)
9091
output = wrapper.invoker(staticWiring, env)
9192
}
9293
// wiring factory is last (if present)
93-
val env = buildEnvironment(wrapper, directives, null)
94+
val env = buildEnvironment(wrapper, directives, null, null)
9495
if (runtimeWiring.wiringFactory.providesSchemaDirectiveWiring(env)) {
9596
val factoryWiring = runtimeWiring.wiringFactory.getSchemaDirectiveWiring(env)
9697
output = wrapper.invoker(factoryWiring, env)
@@ -131,7 +132,7 @@ class DirectiveWiringHelper(
131132
return output
132133
}
133134

134-
private fun <T : GraphQLDirectiveContainer> buildEnvironment(wrapper: WiringWrapper<T>, directives: List<GraphQLDirective>, directive: GraphQLDirective?): SchemaDirectiveWiringEnvironmentImpl<T> {
135+
private fun <T : GraphQLDirectiveContainer> buildEnvironment(wrapper: WiringWrapper<T>, directives: List<GraphQLDirective>, directive: GraphQLDirective?, appliedDirective: GraphQLAppliedDirective?): SchemaDirectiveWiringEnvironmentImpl<T> {
135136
val nodeParentTree = buildAstTree(*listOfNotNull(
136137
wrapper.fieldsContainer?.definition,
137138
wrapper.inputFieldsContainer?.definition,
@@ -154,7 +155,7 @@ class DirectiveWiringHelper(
154155
is GraphQLFieldsContainer -> schemaDirectiveParameters.newParams(wrapper.graphQlType, nodeParentTree, elementParentTree)
155156
else -> schemaDirectiveParameters.newParams(nodeParentTree, elementParentTree)
156157
}
157-
return SchemaDirectiveWiringEnvironmentImpl(wrapper.graphQlType, directives, wrapper.graphQlType.appliedDirectives, directive, params)
158+
return SchemaDirectiveWiringEnvironmentImpl(wrapper.graphQlType, directives, wrapper.graphQlType.appliedDirectives, directive, appliedDirective, params)
158159
}
159160

160161
fun buildDirectiveInputType(value: Value<*>): GraphQLInputType? {

src/main/kotlin/graphql/kickstart/tools/directive/SchemaDirectiveWiringEnvironmentImpl.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import graphql.schema.*
66
import graphql.schema.idl.RuntimeWiring
77
import graphql.schema.idl.SchemaDirectiveWiringEnvironment
88
import graphql.schema.idl.TypeDefinitionRegistry
9-
import graphql.util.FpKit
109

1110
class SchemaDirectiveWiringEnvironmentImpl<T : GraphQLDirectiveContainer?>(
1211
private val element: T,
1312
directives: List<GraphQLDirective>,
1413
appliedDirectives: List<GraphQLAppliedDirective>,
1514
private val registeredDirective: GraphQLDirective?,
15+
private val registeredAppliedDirective: GraphQLAppliedDirective?,
1616
parameters: Parameters
1717
) : SchemaDirectiveWiringEnvironment<T> {
1818
private val directives: Map<String, GraphQLDirective>
@@ -27,8 +27,8 @@ class SchemaDirectiveWiringEnvironmentImpl<T : GraphQLDirectiveContainer?>(
2727

2828
init {
2929
typeDefinitionRegistry = parameters.typeRegistry
30-
this.directives = FpKit.getByName(directives) { obj: GraphQLDirective -> obj.name }
31-
this.appliedDirectives = FpKit.getByName(appliedDirectives) { obj: GraphQLAppliedDirective -> obj.name }
30+
this.directives = directives.associateBy { it.name }
31+
this.appliedDirectives = appliedDirectives.associateBy { it.name }
3232
context = parameters.context
3333
codeRegistry = parameters.codeRegistry
3434
nodeParentTree = parameters.nodeParentTree
@@ -39,7 +39,7 @@ class SchemaDirectiveWiringEnvironmentImpl<T : GraphQLDirectiveContainer?>(
3939

4040
override fun getElement(): T = element
4141
override fun getDirective(): GraphQLDirective? = registeredDirective
42-
override fun getAppliedDirective(): GraphQLAppliedDirective? = appliedDirectives[registeredDirective?.name]
42+
override fun getAppliedDirective(): GraphQLAppliedDirective? = registeredAppliedDirective
4343
override fun getDirectives(): Map<String, GraphQLDirective> = LinkedHashMap(directives)
4444
override fun getDirective(directiveName: String): GraphQLDirective = directives[directiveName]!!
4545
override fun getAppliedDirectives(): Map<String, GraphQLAppliedDirective> = appliedDirectives

0 commit comments

Comments
 (0)