Repository navigation
Expand file tree
/
Copy pathSentryBatchLoaderRegistry.java
More file actions
119 lines (99 loc) · 4.02 KB
/
Copy pathSentryBatchLoaderRegistry.java
File metadata and controls
119 lines (99 loc) · 4.02 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package io.sentry.spring7.graphql;
import static io.sentry.graphql.SentryGraphqlInstrumentation.SENTRY_SCOPES_CONTEXT_KEY;
import graphql.GraphQLContext;
import io.sentry.Breadcrumb;
import io.sentry.IScopes;
import io.sentry.NoOpScopes;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import org.dataloader.BatchLoaderEnvironment;
import org.dataloader.DataLoaderOptions;
import org.dataloader.DataLoaderRegistry;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.graphql.execution.BatchLoaderRegistry;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@ApiStatus.Internal
public final class SentryBatchLoaderRegistry implements BatchLoaderRegistry {
private final @NotNull BatchLoaderRegistry delegate;
SentryBatchLoaderRegistry(final @NotNull BatchLoaderRegistry delegate) {
this.delegate = delegate;
}
@Override
public <K, V> RegistrationSpec<K, V> forTypePair(Class<K> keyType, Class<V> valueType) {
return new SentryRegistrationSpec<K, V>(
delegate.forTypePair(keyType, valueType), keyType, valueType);
}
@Override
public <K, V> RegistrationSpec<K, V> forName(String name) {
return new SentryRegistrationSpec<K, V>(delegate.forName(name), name);
}
@Override
public void registerDataLoaders(DataLoaderRegistry registry, GraphQLContext context) {
delegate.registerDataLoaders(registry, context);
}
public static final class SentryRegistrationSpec<K, V>
implements BatchLoaderRegistry.RegistrationSpec<K, V> {
private final @NotNull RegistrationSpec<K, V> delegate;
private final @Nullable String name;
private final @Nullable Class<K> keyType;
private final @Nullable Class<V> valueType;
public SentryRegistrationSpec(
final @NotNull RegistrationSpec<K, V> delegate, Class<K> keyType, Class<V> valueType) {
this.delegate = delegate;
this.keyType = keyType;
this.valueType = valueType;
this.name = null;
}
public SentryRegistrationSpec(final @NotNull RegistrationSpec<K, V> delegate, String name) {
this.delegate = delegate;
this.name = name;
this.keyType = null;
this.valueType = null;
}
@Override
public BatchLoaderRegistry.RegistrationSpec<K, V> withName(String name) {
return delegate.withName(name);
}
@Override
public BatchLoaderRegistry.RegistrationSpec<K, V> withOptions(
Consumer<DataLoaderOptions.Builder> optionsConsumer) {
return delegate.withOptions(optionsConsumer);
}
@Override
public BatchLoaderRegistry.RegistrationSpec<K, V> withOptions(DataLoaderOptions options) {
return delegate.withOptions(options);
}
@Override
public void registerBatchLoader(BiFunction<List<K>, BatchLoaderEnvironment, Flux<V>> loader) {
delegate.registerBatchLoader(
(keys, batchLoaderEnvironment) -> {
scopesFromContext(batchLoaderEnvironment)
.addBreadcrumb(Breadcrumb.graphqlDataLoader(keys, keyType, valueType, name));
return loader.apply(keys, batchLoaderEnvironment);
});
}
@Override
public void registerMappedBatchLoader(
BiFunction<Set<K>, BatchLoaderEnvironment, Mono<Map<K, V>>> loader) {
delegate.registerMappedBatchLoader(
(keys, batchLoaderEnvironment) -> {
scopesFromContext(batchLoaderEnvironment)
.addBreadcrumb(Breadcrumb.graphqlDataLoader(keys, keyType, valueType, name));
return loader.apply(keys, batchLoaderEnvironment);
});
}
private @NotNull IScopes scopesFromContext(final @NotNull BatchLoaderEnvironment environment) {
Object context = environment.getContext();
if (context instanceof GraphQLContext graphqlContext) {
return graphqlContext.getOrDefault(SENTRY_SCOPES_CONTEXT_KEY, NoOpScopes.getInstance());
}
return NoOpScopes.getInstance();
}
}
}