Skip to content

Commit 5747def

Browse files
committed
Upgrade to GraphQL Java 24.0
This commit sets the new GraphQL Java baseline to 24.0 for this Spring for GraphQL generation. This also uses the new `DataLoader#getName` property in the batch loader registry and the observability instrumentation. Closes gh-1210 Closes gh-1211
1 parent 998ba45 commit 5747def

File tree

9 files changed

+53
-40
lines changed

9 files changed

+53
-40
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description = "Spring for GraphQL"
33
ext {
44
moduleProjects = [project(":spring-graphql"), project(":spring-graphql-test")]
55
springFrameworkVersion = "6.2.6"
6-
graphQlJavaVersion = "23.1"
6+
graphQlJavaVersion = "24.0"
77
springBootVersion = "3.4.3"
88
}
99

spring-graphql-docs/modules/ROOT/pages/observability.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ By default, the following KeyValues are created:
9696
|===
9797
|Name | Description
9898
|`graphql.error.type` _(required)_|Class name of the data fetching error
99-
|`graphql.loader.type` _(required)_|Class name of the elements being fetched.
99+
|`graphql.loader.name` _(required)_|Name of the DataLoader being used.
100100
|`graphql.outcome` _(required)_|Outcome of the GraphQL data fetching operation, "SUCCESS" or "ERROR".
101101
|===
102102

spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultBatchLoaderRegistry.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,12 +60,11 @@ public class DefaultBatchLoaderRegistry implements BatchLoaderRegistry {
6060
private final Supplier<DataLoaderOptions> defaultOptionsSupplier;
6161

6262

63-
6463
/**
6564
* Default constructor.
6665
*/
6766
public DefaultBatchLoaderRegistry() {
68-
this(DataLoaderOptions::newOptions);
67+
this(DataLoaderOptions::newDefaultOptions);
6968
}
7069

7170
/**
@@ -99,24 +98,24 @@ public boolean hasRegistrations() {
9998
public void registerDataLoaders(DataLoaderRegistry registry, GraphQLContext context) {
10099
BatchLoaderContextProvider contextProvider = () -> context;
101100
for (ReactorBatchLoader<?, ?> loader : this.loaders) {
102-
DataLoaderOptions options = loader.getOptions();
103-
options = options.setBatchLoaderContextProvider(contextProvider);
104-
DataLoader<?, ?> dataLoader = DataLoaderFactory.newDataLoader(loader, options);
105-
registerDataLoader(loader.getName(), dataLoader, registry);
101+
DataLoaderOptions options = loader.getOptions()
102+
.transform((opt) -> opt.setBatchLoaderContextProvider(contextProvider));
103+
DataLoader<?, ?> dataLoader = DataLoaderFactory.newDataLoader(loader.getName(), loader, options);
104+
registerDataLoader(dataLoader, registry);
106105
}
107106
for (ReactorMappedBatchLoader<?, ?> loader : this.mappedLoaders) {
108-
DataLoaderOptions options = loader.getOptions();
109-
options = options.setBatchLoaderContextProvider(contextProvider);
110-
DataLoader<?, ?> dataLoader = DataLoaderFactory.newMappedDataLoader(loader, options);
111-
registerDataLoader(loader.getName(), dataLoader, registry);
107+
DataLoaderOptions options = loader.getOptions()
108+
.transform((opt) -> opt.setBatchLoaderContextProvider(contextProvider));
109+
DataLoader<?, ?> dataLoader = DataLoaderFactory.newMappedDataLoader(loader.getName(), loader, options);
110+
registerDataLoader(dataLoader, registry);
112111
}
113112
}
114113

115-
private void registerDataLoader(String name, DataLoader<?, ?> dataLoader, DataLoaderRegistry registry) {
116-
if (registry.getDataLoader(name) != null) {
117-
throw new IllegalStateException("More than one DataLoader named '" + name + "'");
114+
private void registerDataLoader(DataLoader<?, ?> dataLoader, DataLoaderRegistry registry) {
115+
if (registry.getDataLoader(dataLoader.getName()) != null) {
116+
throw new IllegalStateException("More than one DataLoader named '" + dataLoader.getName() + "'");
118117
}
119-
registry.register(name, dataLoader);
118+
registry.register(dataLoader.getName(), dataLoader);
120119
}
121120

122121

@@ -183,20 +182,21 @@ private String initName() {
183182
}
184183

185184
private Supplier<DataLoaderOptions> initOptionsSupplier() {
186-
187-
Supplier<DataLoaderOptions> optionsSupplier = () ->
188-
new DataLoaderOptions((this.options != null) ?
189-
this.options : DefaultBatchLoaderRegistry.this.defaultOptionsSupplier.get());
190-
191-
if (this.optionsBuilderConsumer == null) {
192-
return optionsSupplier;
193-
}
194-
195185
return () -> {
196-
DataLoaderOptions options = optionsSupplier.get();
197-
return options.transform(this.optionsBuilderConsumer);
186+
DataLoaderOptions.Builder builder;
187+
if (this.options != null) {
188+
builder = DataLoaderOptions.newOptions(this.options);
189+
}
190+
else {
191+
builder = DataLoaderOptions.newOptions(DefaultBatchLoaderRegistry.this.defaultOptionsSupplier.get());
192+
}
193+
if (this.optionsBuilderConsumer != null) {
194+
this.optionsBuilderConsumer.accept(builder);
195+
}
196+
return builder.build();
198197
};
199198
}
199+
200200
}
201201

202202

spring-graphql/src/main/java/org/springframework/graphql/observation/DataLoaderObservationContext.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import io.micrometer.observation.Observation;
2222
import org.dataloader.BatchLoaderEnvironment;
23+
import org.dataloader.DataLoader;
2324

2425
/**
2526
* Context that holds information for metadata collection during observations
@@ -30,17 +31,27 @@
3031
*/
3132
public class DataLoaderObservationContext extends Observation.Context {
3233

34+
private final DataLoader<?, ?> dataLoader;
35+
3336
private final List<?> keys;
3437

3538
private final BatchLoaderEnvironment environment;
3639

3740
private List<?> result = List.of();
3841

39-
DataLoaderObservationContext(List<?> keys, BatchLoaderEnvironment environment) {
42+
DataLoaderObservationContext(DataLoader<?, ?> dataLoader, List<?> keys, BatchLoaderEnvironment environment) {
43+
this.dataLoader = dataLoader;
4044
this.keys = keys;
4145
this.environment = environment;
4246
}
4347

48+
/**
49+
* Return the {@link DataLoader} being used for the operation.
50+
*/
51+
public DataLoader<?, ?> getDataLoader() {
52+
return this.dataLoader;
53+
}
54+
4455
/**
4556
* Return the keys for loading by the {@link org.dataloader.DataLoader}.
4657
*/

spring-graphql/src/main/java/org/springframework/graphql/observation/DefaultDataLoaderObservationConvention.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.graphql.observation.GraphQlObservationDocumentation.DataLoaderHighCardinalityKeyNames;
2626
import org.springframework.graphql.observation.GraphQlObservationDocumentation.DataLoaderLowCardinalityKeyNames;
27+
import org.springframework.util.StringUtils;
2728

2829
/**
2930
* Default implementation for a {@link DataLoaderObservationConvention}
@@ -38,7 +39,7 @@ public class DefaultDataLoaderObservationConvention implements DataLoaderObserva
3839

3940
private static final KeyValue ERROR_TYPE_NONE = KeyValue.of(DataLoaderLowCardinalityKeyNames.ERROR_TYPE, "NONE");
4041

41-
private static final KeyValue LOADER_TYPE_UNKNOWN = KeyValue.of(DataLoaderLowCardinalityKeyNames.LOADER_TYPE, "unknown");
42+
private static final KeyValue LOADER_TYPE_UNKNOWN = KeyValue.of(DataLoaderLowCardinalityKeyNames.LOADER_NAME, "unknown");
4243

4344
private static final KeyValue OUTCOME_SUCCESS = KeyValue.of(DataLoaderLowCardinalityKeyNames.OUTCOME, "SUCCESS");
4445

@@ -78,10 +79,10 @@ protected KeyValue errorType(DataLoaderObservationContext context) {
7879
}
7980

8081
protected KeyValue loaderType(DataLoaderObservationContext context) {
81-
if (context.getResult().isEmpty()) {
82-
return LOADER_TYPE_UNKNOWN;
82+
if (StringUtils.hasText(context.getDataLoader().getName())) {
83+
return KeyValue.of(DataLoaderLowCardinalityKeyNames.LOADER_NAME, context.getDataLoader().getName());
8384
}
84-
return KeyValue.of(DataLoaderLowCardinalityKeyNames.LOADER_TYPE, context.getResult().get(0).getClass().getSimpleName());
85+
return LOADER_TYPE_UNKNOWN;
8586
}
8687

8788
protected KeyValue outcome(DataLoaderObservationContext context) {

spring-graphql/src/main/java/org/springframework/graphql/observation/GraphQlObservationDocumentation.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.micrometer.observation.Observation;
2222
import io.micrometer.observation.ObservationConvention;
2323
import io.micrometer.observation.docs.ObservationDocumentation;
24+
import org.dataloader.DataLoader;
2425

2526
/**
2627
* Documented {@link io.micrometer.common.KeyValue KeyValues} for {@link graphql.GraphQL GraphQL server observations}.
@@ -205,12 +206,12 @@ public String asString() {
205206
},
206207

207208
/**
208-
* {@link Class#getSimpleName()} of the returned elements.
209+
* {@link DataLoader#getName()} of the data loader.
209210
*/
210-
LOADER_TYPE {
211+
LOADER_NAME {
211212
@Override
212213
public String asString() {
213-
return "graphql.loader.type";
214+
return "graphql.loader.name";
214215
}
215216
},
216217

spring-graphql/src/main/java/org/springframework/graphql/observation/GraphQlObservationInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public DataLoaderInstrumentationContext<List<?>> beginBatchLoader(DataLoader<?,
278278
Observation observation = GraphQlObservationDocumentation.DATA_LOADER
279279
.observation(GraphQlObservationInstrumentation.this.dataLoaderObservationConvention,
280280
DEFAULT_DATA_LOADER_CONVENTION,
281-
() -> new DataLoaderObservationContext(keys, environment),
281+
() -> new DataLoaderObservationContext(dataLoader, keys, environment),
282282
GraphQlObservationInstrumentation.this.observationRegistry);
283283
if (environment.getContext() instanceof GraphQLContext graphQLContext) {
284284
Observation parentObservation = graphQLContext.get(ObservationThreadLocalAccessor.KEY);

spring-graphql/src/test/java/org/springframework/graphql/execution/DefaultBatchLoaderRegistryTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DefaultBatchLoaderRegistryTests {
4646
private final BatchLoaderRegistry batchLoaderRegistry =
4747
new DefaultBatchLoaderRegistry(() -> {
4848
// Disable batching, so we can test loading immediately
49-
return DataLoaderOptions.newOptions().setBatchingEnabled(false);
49+
return DataLoaderOptions.newOptions().setBatchingEnabled(false).build();
5050
});
5151

5252
private final DataLoaderRegistry dataLoaderRegistry = DataLoaderRegistry.newRegistry().build();
@@ -105,7 +105,7 @@ void mappedBatchLoader() throws Exception {
105105
@Test
106106
void dataLoaderOptions() throws Exception {
107107

108-
DataLoaderOptions defaultOptions = DataLoaderOptions.newOptions().setBatchingEnabled(false);
108+
DataLoaderOptions defaultOptions = DataLoaderOptions.newOptions().setBatchingEnabled(false).build();
109109
DefaultBatchLoaderRegistry batchLoaderRegistry = new DefaultBatchLoaderRegistry(() -> defaultOptions);
110110

111111
AtomicInteger counter = new AtomicInteger(1);

spring-graphql/src/test/java/org/springframework/graphql/observation/GraphQlObservationInstrumentationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ void shouldRecordBatchLoadingAsSingleObservation() {
411411
.hasObservationWithNameEqualTo("graphql.dataloader")
412412
.that()
413413
.hasLowCardinalityKeyValue("graphql.outcome", "SUCCESS")
414-
.hasLowCardinalityKeyValue("graphql.loader.type", "Author")
414+
.hasLowCardinalityKeyValue("graphql.loader.name", "org.springframework.graphql.Author")
415415
.hasHighCardinalityKeyValue("graphql.loader.size", "4")
416416
.hasContextualNameEqualTo("graphql dataloader author");
417417
}

0 commit comments

Comments
 (0)