Skip to content

Commit af4ca9e

Browse files
authored
breaking: update to graphql-java 22 (#395)
The only breaking change is to the caching instrumentation that relied on deprecated methods. Disabling Spring subscription-callback lib until we get compatible Spring Boot release.
1 parent 8c6cf6a commit af4ca9e

File tree

78 files changed

+1593
-176
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1593
-176
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version = 3.0-SNAPSHOT
33

44
# dependencies
55
annotationsVersion = 24.1.0
6-
graphQLJavaVersion = 21.3
6+
graphQLJavaVersion = 22.0
77
mockWebServerVersion = 4.12.0
88
protobufVersion = 4.26.1
99
slf4jVersion = 2.0.13

graphql-java-support/src/main/java/com/apollographql/federation/graphqljava/caching/CacheControlInstrumentation.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111
import graphql.execution.instrumentation.parameters.InstrumentationFieldParameters;
1212
import graphql.schema.*;
1313
import java.util.*;
14-
import java.util.concurrent.CompletableFuture;
1514
import java.util.stream.Collectors;
1615
import org.jetbrains.annotations.Nullable;
1716

1817
/**
1918
* A GraphQL Java Instrumentation that computes a max age for an operation based on @cacheControl
2019
* directives.
2120
*
22-
* <p>You can retrieve the "max-age=..." header value with a {@link graphql.GraphQLContext}: <code>
21+
* <p>You can retrieve the "max-age=..." header value with a {@link GraphQLContext}: <code>
2322
* String cacheControlHeader = CacheControlInstrumentation.cacheControlContext(context);
2423
* </code>
2524
*
26-
* <p>See https://www.apollographql.com/docs/apollo-server/performance/caching/ and the original
27-
* implementation at
28-
* https://github.com/apollographql/apollo-server/blob/main/packages/apollo-server-core/src/plugin/cacheControl/index.ts
25+
* <p>See <a
26+
* href="https://www.apollographql.com/docs/apollo-server/performance/caching/">@cacheControl
27+
* documentation</a> and the original Apollo Server <a
28+
* href="https://github.com/apollographql/apollo-server/tree/main/packages/plugin-response-cache">plugin-response-cache</a>
29+
* implementation.
2930
*/
3031
public class CacheControlInstrumentation extends SimplePerformantInstrumentation {
3132
private final int defaultMaxAge;
@@ -63,9 +64,12 @@ public InstrumentationState createState(InstrumentationCreateStateParameters par
6364
@Override
6465
public InstrumentationContext<ExecutionResult> beginExecution(
6566
InstrumentationExecutionParameters parameters, InstrumentationState state) {
66-
return new InstrumentationContext<ExecutionResult>() {
67+
return new InstrumentationContext<>() {
68+
6769
@Override
68-
public void onDispatched(CompletableFuture<ExecutionResult> completableFuture) {}
70+
public void onDispatched() {
71+
// do nothing
72+
}
6973

7074
@Override
7175
public void onCompleted(ExecutionResult executionResult, Throwable throwable) {
@@ -81,7 +85,7 @@ public void onCompleted(ExecutionResult executionResult, Throwable throwable) {
8185
}
8286

8387
@Override
84-
public InstrumentationContext<ExecutionResult> beginField(
88+
public @Nullable InstrumentationContext<Object> beginFieldExecution(
8589
InstrumentationFieldParameters parameters, InstrumentationState state) {
8690
CacheControlState cacheControlState = (CacheControlState) state;
8791
CacheControlPolicy fieldPolicy = new CacheControlPolicy(allowZeroMaxAge);
@@ -168,8 +172,7 @@ public InstrumentationContext<ExecutionResult> beginField(
168172
}
169173

170174
cacheControlState.overallPolicy.restrict(fieldPolicy);
171-
172-
return super.beginField(parameters, state);
175+
return super.beginFieldExecution(parameters, state);
173176
}
174177

175178
enum CacheControlScope {

graphql-java-support/src/main/java/com/apollographql/federation/graphqljava/directives/LinkDirectiveProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ private static Map<String, String> parseLinkImports(Directive linkDirective) {
144144
.filter(field -> field.getName().equals("as"))
145145
.findFirst();
146146

147-
if (!nameField.isPresent() || !(nameField.get().getValue() instanceof StringValue)) {
147+
if (nameField.isEmpty() || !(nameField.get().getValue() instanceof StringValue)) {
148148
throw new UnsupportedLinkImportException(importedObjectValue);
149149
}
150150
final String name = ((StringValue) nameField.get().getValue()).getValue();
151151

152-
if (!renameAsField.isPresent()) {
152+
if (renameAsField.isEmpty()) {
153153
imports.put(name, name);
154154
} else {
155155
final Value renamedAsValue = renameAsField.get().getValue();

graphql-java-support/src/main/java/com/apollographql/federation/graphqljava/printer/ServiceSDLPrinter.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import graphql.schema.GraphQLNamedSchemaElement;
1313
import graphql.schema.GraphQLSchema;
1414
import graphql.schema.GraphQLSchemaElement;
15+
import graphql.schema.idl.DirectiveInfo;
1516
import graphql.schema.idl.SchemaPrinter;
1617
import graphql.schema.visibility.GraphqlFieldVisibility;
17-
import java.util.Arrays;
1818
import java.util.Collections;
1919
import java.util.HashSet;
2020
import java.util.List;
@@ -28,10 +28,6 @@
2828
*/
2929
public final class ServiceSDLPrinter {
3030

31-
// Apollo Gateway will fail Federation v1 composition if it sees standard directive definitions.
32-
private static final Set<String> STANDARD_DIRECTIVES =
33-
new HashSet<>(Arrays.asList("deprecated", "include", "oneOf", "skip", "specifiedBy"));
34-
3531
private ServiceSDLPrinter() {
3632
// hidden constructor as this is static utility class
3733
}
@@ -44,11 +40,14 @@ private ServiceSDLPrinter() {
4440
* should be removed (at least a single query has to be present for graphql-java to consider
4541
* it as a valid schema)
4642
* @return SDL compatible with Federation v1
43+
* @deprecated Migrate to use Federation v2
4744
*/
45+
@Deprecated(since = "05/16/2024")
4846
public static String generateServiceSDL(GraphQLSchema schema, boolean queryTypeShouldBeEmpty) {
4947
// Gather directive definitions to hide.
5048
final Set<String> hiddenDirectiveDefinitions = new HashSet<>();
51-
hiddenDirectiveDefinitions.addAll(STANDARD_DIRECTIVES);
49+
// Apollo Gateway will fail Federation v1 composition if it sees standard directive definitions.
50+
hiddenDirectiveDefinitions.addAll(DirectiveInfo.GRAPHQL_SPECIFICATION_DIRECTIVE_MAP.keySet());
5251
hiddenDirectiveDefinitions.addAll(FederationDirectives.allNames);
5352

5453
// Gather type definitions to hide.
@@ -102,23 +101,18 @@ public GraphQLFieldDefinition getFieldDefinition(
102101
final GraphQLSchema federatedSchema =
103102
schema.transform(schemaBuilder -> schemaBuilder.codeRegistry(newCodeRegistry));
104103

105-
final Predicate<GraphQLSchemaElement> excludeFedTypeDefinitions =
106-
element ->
107-
!(element instanceof GraphQLNamedSchemaElement
108-
&& hiddenTypeDefinitions.contains(((GraphQLNamedSchemaElement) element).getName()));
109-
final Predicate<GraphQLSchemaElement> excludeFedDirectiveDefinitions =
104+
final Predicate<GraphQLSchemaElement> shouldIncludeSchemaElement =
110105
element ->
111106
!(element instanceof GraphQLDirective
112-
&& hiddenDirectiveDefinitions.contains(((GraphQLDirective) element).getName()));
107+
&& hiddenDirectiveDefinitions.contains(((GraphQLDirective) element).getName()))
108+
&& !(element instanceof GraphQLNamedSchemaElement
109+
&& hiddenTypeDefinitions.contains(
110+
((GraphQLNamedSchemaElement) element).getName()));
113111
final SchemaPrinter.Options options =
114112
SchemaPrinter.Options.defaultOptions()
115-
.includeScalarTypes(true)
116113
.includeSchemaDefinition(true)
117-
.includeDirectives(FederationDirectives.allNames::contains)
118-
.includeSchemaElement(
119-
element ->
120-
excludeFedTypeDefinitions.test(element)
121-
&& excludeFedDirectiveDefinitions.test(element));
114+
.includeSchemaElement(shouldIncludeSchemaElement);
115+
122116
return new SchemaPrinter(options).print(federatedSchema).trim();
123117
}
124118

@@ -130,11 +124,15 @@ public GraphQLFieldDefinition getFieldDefinition(
130124
*/
131125
public static String generateServiceSDLV2(GraphQLSchema schema) {
132126
// federation v2 SDL does not need to filter federation directive definitions
127+
final Predicate<GraphQLSchemaElement> excludeBuiltInDirectiveDefinitions =
128+
element ->
129+
!(element instanceof GraphQLDirective
130+
&& DirectiveInfo.isGraphqlSpecifiedDirective((GraphQLDirective) element));
133131
return new SchemaPrinter(
134132
SchemaPrinter.Options.defaultOptions()
135133
.includeSchemaDefinition(true)
136134
.includeScalarTypes(true)
137-
.includeDirectives(def -> !STANDARD_DIRECTIVES.contains(def)))
135+
.includeSchemaElement(excludeBuiltInDirectiveDefinitions))
138136
.print(schema)
139137
.trim();
140138
}

graphql-java-support/src/test/java/com/apollographql/federation/graphqljava/FederatedSchemaVerifier.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static graphql.ExecutionInput.newExecutionInput;
44
import static graphql.GraphQL.newGraphQL;
55
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
67
import static org.junit.jupiter.api.Assertions.assertNotNull;
78
import static org.junit.jupiter.api.Assertions.assertTrue;
89

@@ -14,15 +15,10 @@
1415
import graphql.schema.GraphQLSchema;
1516
import graphql.schema.GraphQLType;
1617
import graphql.schema.idl.SchemaPrinter;
17-
import java.util.Arrays;
18-
import java.util.HashSet;
1918
import java.util.Map;
20-
import java.util.Set;
2119
import org.junit.jupiter.api.Assertions;
2220

2321
final class FederatedSchemaVerifier {
24-
public static final Set<String> standardDirectives =
25-
new HashSet<>(Arrays.asList("deprecated", "include", "oneOf", "skip", "specifiedBy"));
2622

2723
private FederatedSchemaVerifier() {}
2824

@@ -35,18 +31,11 @@ static ExecutionResult execute(GraphQLSchema schema, String query) {
3531
*
3632
* @param schema test schema
3733
* @param expectedSchemaSDL expected SDL
38-
* @param isFederationV2 boolean flag indicating whether we are testing Federation v1 or v2
39-
* specification.
4034
*/
41-
public static void verifySchemaSDL(
42-
GraphQLSchema schema, String expectedSchemaSDL, boolean isFederationV2) {
35+
public static void verifyFullSchema(GraphQLSchema schema, String expectedSchemaSDL) {
4336
Assertions.assertEquals(
4437
expectedSchemaSDL.trim(),
45-
new SchemaPrinter(
46-
SchemaPrinter.Options.defaultOptions()
47-
.includeSchemaDefinition(isFederationV2)
48-
.includeScalarTypes(true)
49-
.includeDirectives(directive -> !standardDirectives.contains(directive)))
38+
new SchemaPrinter(SchemaPrinter.Options.defaultOptions().includeSchemaDefinition(true))
5039
.print(schema)
5140
.trim(),
5241
"Generated schema SDL should match expected one");
@@ -64,9 +53,9 @@ public static void verifySchemaContainsServiceFederationType(GraphQLSchema schem
6453
assertNotNull(serviceField, "_service field present");
6554
final GraphQLType serviceType = schema.getType("_Service");
6655
assertNotNull(serviceType, "_Service type present");
67-
assertTrue(serviceType instanceof GraphQLObjectType, "_Service type is object type");
68-
assertTrue(
69-
serviceField.getType() instanceof GraphQLNonNull, "_service returns non-nullable object");
56+
assertInstanceOf(GraphQLObjectType.class, serviceType, "_Service type is object type");
57+
assertInstanceOf(
58+
GraphQLNonNull.class, serviceField.getType(), "_service returns non-nullable object");
7059
final GraphQLNonNull nonNullableServiceType = (GraphQLNonNull) serviceField.getType();
7160
assertEquals(
7261
serviceType,

0 commit comments

Comments
 (0)