Replies: 1 comment
-
This is not easily done out of the box. The In theory you achieve this by using a thread safe count down latch (or some sort of inter thread mechanism to coordinate on) This groovy code shows two DataFetchers coordinating with each other. You can decide if its maintainable code or not. It couples the two DF implementations closely together which some people would not like. def "dependant data fetchers"() {
def sdl = """
type Query {
channel : Channel
builds : [Build]
}
type Channel {
name : String
}
type Build {
status : String
}
"""
DataFetcher channelDF = new DataFetcher() {
@Override
Object get(DataFetchingEnvironment environment) throws Exception {
return CompletableFuture.supplyAsync {
GraphQLContext context = environment.getGraphQlContext()
CountDownLatch latch = context.get("channelDFLatch")
// just to demonstrate that we can share global context between sibling DFs
context.put("buildInfo", "passed")
def value = ["name": "channel1"]
latch.countDown()
return value
}
}
}
DataFetcher buildsDF = new DataFetcher() {
@Override
Object get(DataFetchingEnvironment environment) throws Exception {
return CompletableFuture.supplyAsync {
GraphQLContext context = environment.getGraphQlContext()
CountDownLatch latch = context.get("channelDFLatch")
// wait until latch is zero from other DF
latch.await()
String buildInfo = context.get("buildInfo")
def value = [["status": buildInfo]]
return value
}
}
}
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring().type(TypeRuntimeWiring.newTypeWiring("Query")
.dataFetcher("channel", channelDF)
.dataFetcher("builds", buildsDF))
.build()
GraphQL graphQL = TestUtil.graphQL(sdl, runtimeWiring).build()
String query = "query q { channel { name } builds { status }}"
Consumer<GraphQLContext.Builder> builderConsumer = { GraphQLContext.Builder builder ->
builder.put("channelDFLatch", new CountDownLatch(1))
}
ExecutionInput executionInput = newExecutionInput()
.query(query)
.graphQLContext(builderConsumer)
.build()
when:
def executionResult = graphQL.execute(executionInput)
then:
executionResult.errors.isEmpty()
executionResult.data == [channel: [name: "channel1"], builds: [[status: "passed"]]]
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
I have a use case with 2 sibling graphQL nodes like this.
{ channel: { ... } builds: { ... } <more nodes> }Both these nodes will have standalone data fetchers. Please note that I am using AsyncExecutionStrategy. For the builds node, I want the channel DataFetcher to resolve first and provide me with some data.
How can I achieve this functionality?
Beta Was this translation helpful? Give feedback.
All reactions