-
Notifications
You must be signed in to change notification settings - Fork 0
/
DepartmentDataLoader.java
44 lines (37 loc) · 1.75 KB
/
DepartmentDataLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.sample.dgs_otel.fetchers;
import com.netflix.graphql.dgs.DgsDataLoader;
import com.sample.dgs_otel.generated.types.Department;
import io.opentelemetry.context.Context;
import org.dataloader.BatchLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
@DgsDataLoader(name = "departmentDataLoader")
public class DepartmentDataLoader implements BatchLoader<Parameter, Department> {
private final ExecutorService dgsAsyncTaskExecutor;
@Autowired
public DepartmentDataLoader(@Qualifier("dgsAsyncTaskExecutor") ExecutorService dgsAsyncTaskExecutor) {
this.dgsAsyncTaskExecutor = dgsAsyncTaskExecutor;
}
@Override
public CompletionStage<List<Department>> load(List<Parameter> parameters) {
//TODO: If I call parameters.get(0).getContext().makeCurrent() then context gets propagated correctly.
System.out.println("### context lost here, even before offloading a task on executor: " + Context.current());
return CompletableFuture.supplyAsync(() -> fetchDepartments(parameters), dgsAsyncTaskExecutor);
}
private List<Department> fetchDepartments(List<Parameter> parameters) {
try {
TimeUnit.SECONDS.sleep(3); // simulate IO call
} catch (Exception e) {
throw new RuntimeException(e);
}
return parameters.stream()
.map(Parameter::getDepartmentId)
.map(id -> Department.newBuilder().id(id).name("Name:" + id).build())
.toList();
}
}