Skip to content

Commit 4b54606

Browse files
committed
Upgrade to graphql-java 3.0.0
1 parent 9043ccf commit 4b54606

File tree

6 files changed

+61
-22
lines changed

6 files changed

+61
-22
lines changed

core/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
dependencies {
2-
compileOnly 'com.graphql-java:graphql-java:2.3.0'
2+
compileOnly 'com.graphql-java:graphql-java:3.0.0'
33

44
// TODO shade?
5-
compile 'io.reactivex.rxjava2:rxjava:2.0.5'
5+
compile 'io.reactivex.rxjava2:rxjava:2.1.0'
66

7-
testCompile 'com.graphql-java:graphql-java:2.3.0'
7+
testCompile 'com.graphql-java:graphql-java:3.0.0'
88
testCompile 'org.slf4j:slf4j-simple:1.7.24'
99
testCompile 'com.google.guava:guava:21.0'
1010
testCompile 'org.apache.commons:commons-lang3:3.5'

core/src/main/java/com/github/bsideup/graphql/reactive/ReactiveContext.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@ class ReactiveContext extends ExecutionContext {
99
final ExecutionContext parent;
1010

1111
public ReactiveContext(ExecutionContext context, Object key) {
12-
super(context.getGraphQLSchema(), context.getQueryStrategy(), context.getMutationStrategy(), context.getFragmentsByName(), context.getOperationDefinition(), context.getVariables(), context.getRoot());
12+
super(
13+
context.getInstrumentation(),
14+
context.getExecutionId(),
15+
context.getGraphQLSchema(),
16+
context.getQueryStrategy(),
17+
context.getMutationStrategy(),
18+
context.getSubscriptionStrategy(),
19+
context.getFragmentsByName(),
20+
context.getOperationDefinition(),
21+
context.getVariables(),
22+
context.getRoot()
23+
);
1324
this.key = key;
1425
this.parent = context;
1526
}

core/src/main/java/com/github/bsideup/graphql/reactive/ReactiveExecutionStrategy.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import graphql.ExecutionResult;
44
import graphql.ExecutionResultImpl;
5-
import graphql.execution.ExecutionContext;
6-
import graphql.execution.ExecutionStrategy;
5+
import graphql.execution.*;
76
import graphql.language.Field;
87
import graphql.schema.*;
98
import io.reactivex.Flowable;
@@ -21,17 +20,30 @@
2120
import java.util.stream.Stream;
2221
import java.util.stream.StreamSupport;
2322

23+
import static graphql.execution.ExecutionParameters.newParameters;
2424
import static java.util.stream.Collectors.toList;
2525

2626
public class ReactiveExecutionStrategy extends ExecutionStrategy {
2727

2828
@Override
29-
public ExecutionResult execute(ExecutionContext context, GraphQLObjectType parentType, Object source, Map<String, List<Field>> fields) {
29+
public ExecutionResult execute(ExecutionContext context, ExecutionParameters parameters) throws NonNullableFieldWasNullException {
30+
Map<String, List<Field>> fields = parameters.fields();
31+
Object source = parameters.source();
3032
return complexChangesFlow(
3133
context,
3234
source,
3335
__ -> fields.entrySet().stream(),
34-
(entry, sourceValue) -> resolveField(new ReactiveContext(context, entry.getKey()), parentType, source == null ? null : sourceValue, entry.getValue()),
36+
(entry, sourceValue) -> {
37+
ReactiveContext subContext = new ReactiveContext(context, entry.getKey());
38+
39+
ExecutionParameters newParameters = newParameters()
40+
.typeInfo(parameters.typeInfo())
41+
.fields(parameters.fields())
42+
.arguments(parameters.arguments())
43+
.source(source == null ? null : sourceValue)
44+
.build();
45+
return resolveField(subContext, newParameters, entry.getValue());
46+
},
3547
results -> {
3648
Map<Object, Object> result = new HashMap<>();
3749
for (Object entry : results) {
@@ -59,12 +71,21 @@ protected Object adapt(Object result) {
5971
}
6072

6173
@Override
62-
protected ExecutionResult completeValue(ExecutionContext context, GraphQLType fieldType, List<Field> fields, Object result) {
63-
result = adapt(result);
74+
protected ExecutionResult completeValue(ExecutionContext context, ExecutionParameters parameters, List<Field> fields) {
75+
Object result = adapt(parameters.source());
76+
GraphQLType fieldType = parameters.typeInfo().type();
6477

6578
if ((fieldType instanceof GraphQLScalarType || fieldType instanceof GraphQLEnumType) && result instanceof Publisher) {
6679
Flowable<Change> changesFlow = Flowable.fromPublisher((Publisher<?>) result)
67-
.map(it -> new Change(context, completeValue(context, fieldType, fields, it).getData()));
80+
.map(it -> {
81+
ExecutionParameters newParameters = newParameters()
82+
.source(it)
83+
.fields(parameters.fields())
84+
.typeInfo(parameters.typeInfo())
85+
.build();
86+
87+
return new Change(context, completeValue(context, newParameters, fields).getData());
88+
});
6889

6990
return new ExecutionResultImpl(changesFlow, null);
7091
}
@@ -86,7 +107,15 @@ protected ExecutionResult completeValue(ExecutionContext context, GraphQLType fi
86107
AtomicInteger i = new AtomicInteger();
87108
return stream.map(it -> new SimpleEntry<>(i.getAndIncrement(), adapt(it)));
88109
},
89-
(entry, __) -> completeValue(new ReactiveContext(context, entry.getKey()), wrappedType, fields, entry.getValue()),
110+
(entry, __) -> {
111+
ExecutionParameters newParameters = ExecutionParameters.newParameters()
112+
.source(entry.getValue())
113+
.fields(parameters.fields())
114+
.arguments(parameters.arguments())
115+
.typeInfo(parameters.typeInfo().asType(wrappedType))
116+
.build();
117+
return completeValue(new ReactiveContext(context, entry.getKey()), newParameters, fields);
118+
},
90119
results -> Stream.of(results)
91120
.map(SimpleEntry.class::cast)
92121
.sorted(Comparator.comparingInt(it -> (Integer) it.getKey()))
@@ -95,7 +124,7 @@ protected ExecutionResult completeValue(ExecutionContext context, GraphQLType fi
95124
);
96125
}
97126

98-
return super.completeValue(context, fieldType, fields, result);
127+
return super.completeValue(context, parameters, fields);
99128
}
100129

101130
protected <K, V> ExecutionResultImpl complexChangesFlow(

core/src/test/java/com/github/bsideup/graphql/reactive/ReactiveExecutionStrategyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void testPlainField() throws Exception {
3838

3939
assertThat(executionResult)
4040
.isNotNull()
41-
.satisfies(it -> assertThat(it.getData()).isNotNull().isInstanceOf(Publisher.class));
41+
.satisfies(it -> assertThat(it.<Publisher<Change>>getData()).isNotNull().isInstanceOf(Publisher.class));
4242

4343
Flowable.fromPublisher((Publisher<Change>) executionResult.getData()).timestamp(scheduler).subscribe(subscriber);
4444

examples/spring-boot/build.gradle

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
buildscript {
22
ext {
3-
springBootVersion = '2.0.0.BUILD-SNAPSHOT'
3+
springBootVersion = '2.0.0.M1'
44
}
55
repositories {
66
jcenter()
7-
maven { url "https://repo.spring.io/snapshot" }
87
maven { url "https://repo.spring.io/milestone" }
98
}
109
dependencies {
@@ -13,22 +12,21 @@ buildscript {
1312
}
1413

1514
apply plugin: 'java'
16-
apply plugin: 'eclipse'
1715
apply plugin: 'org.springframework.boot'
16+
apply plugin: 'io.spring.dependency-management'
1817

1918
sourceCompatibility = 1.8
2019

2120
repositories {
2221
jcenter()
23-
maven { url "https://repo.spring.io/snapshot" }
2422
maven { url "https://repo.spring.io/milestone" }
2523
}
2624

2725
dependencies {
2826
compile 'org.springframework.boot:spring-boot-starter-webflux'
2927

3028
compile project(":core")
31-
compile 'com.graphql-java:graphql-java:2.3.0'
29+
compile 'com.graphql-java:graphql-java:3.0.0'
3230
compile 'com.google.guava:guava:21.0'
3331

3432
compile 'org.webjars:webjars-locator:0.30'

examples/spring-boot/src/main/java/com/example/GraphQLController.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import reactor.core.publisher.Flux;
2020

2121
import java.lang.management.ManagementFactory;
22+
import java.time.Duration;
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.concurrent.TimeUnit;
@@ -49,7 +50,7 @@ public class GraphQLController {
4950
.name("cpu")
5051
.type(GraphQLFloat)
5152
.dataFetcher(env -> Flux
52-
.intervalMillis(1000)
53+
.interval(Duration.ofSeconds(1))
5354
.map(__ -> ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage())
5455
// Ignore
5556
.distinctUntilChanged(Double::intValue)
@@ -69,7 +70,7 @@ public class GraphQLController {
6970
.name("allocated")
7071
.type(GraphQLLong)
7172
.dataFetcher(env -> Flux
72-
.intervalMillis(1000)
73+
.interval(Duration.ofSeconds(1))
7374
.map(__ -> ((Runtime) env.getSource()).totalMemory())
7475
// Ignore changes less than 1Kb
7576
.distinctUntilChanged(it -> it.intValue() / 1000000)
@@ -79,7 +80,7 @@ public class GraphQLController {
7980
.name("free")
8081
.type(GraphQLLong)
8182
.dataFetcher(env -> Flux
82-
.intervalMillis(1000)
83+
.interval(Duration.ofSeconds(1))
8384
.map(__ -> ((Runtime) env.getSource()).freeMemory())
8485
// Ignore changes less than 1Mb
8586
.distinctUntilChanged(it -> it.intValue() / 1000000)

0 commit comments

Comments
 (0)