Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating graphql-java library to 3.0.0 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
.idea
.gradle
build
.classpath
.settings
.project

graphql-rxjava.iml

.DS_Store
/bin/
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jar {
dependencies {
compile 'org.antlr:antlr4-runtime:4.5.1'
compile 'org.slf4j:slf4j-api:1.7.12'
compile 'com.graphql-java:graphql-java:2015-11-04T21-08-13'
compile 'com.graphql-java:graphql-java:3.0.0'
compile 'io.reactivex:rxjava:1.0.12'
compile 'org.apache.commons:commons-lang3:3.1'

Expand Down
32 changes: 26 additions & 6 deletions src/main/java/graphql/execution/RxExecutionResult.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package graphql.execution;

import graphql.ExecutionResult;
import graphql.GraphQLError;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;

import java.util.List;
import java.util.Map;
import graphql.ExecutionResult;
import graphql.GraphQLError;
import rx.Observable;

public class RxExecutionResult implements ExecutionResult {

Expand All @@ -16,10 +18,17 @@ public class RxExecutionResult implements ExecutionResult {
private Observable<?> dataObservable;

private Observable<List<GraphQLError>> errorsObservable;

private Observable<Map<Object,Object>> extensionsObservable;

public RxExecutionResult(Observable<?> data, Observable<List<GraphQLError>> errors) {
this(data, errors, Observable.just(new HashMap<>()));
}

public RxExecutionResult(Observable<?> data, Observable<List<GraphQLError>> errors, Observable<Map<Object,Object>> extensions) {
dataObservable = data;
errorsObservable = errors;
extensionsObservable = extensions;
}

public Observable<?> getDataObservable() {
Expand All @@ -29,8 +38,13 @@ public Observable<?> getDataObservable() {
public Observable<List<GraphQLError>> getErrorsObservable() {
return errorsObservable;
}

public Observable<Map<Object, Object>> getExtensionsObservable() {
return extensionsObservable;
}

@Override
@SuppressWarnings("unchecked")
@Override
public Object getData() {
logger.warn("getData() called instead of getDataObservable(), blocking (likely a bug)");
return dataObservable.toBlocking().first();
Expand All @@ -41,4 +55,10 @@ public List<GraphQLError> getErrors() {
logger.warn("getErrors() called instead of getErrorsObservable(), blocking (likely a bug)");
return errorsObservable.toBlocking().first();
}

@Override
public Map<Object, Object> getExtensions() {
logger.warn("getExtensions() called instead of getExtensionsObservable(), blocking (likely a bug)");
return extensionsObservable.toBlocking().first();
}
}
83 changes: 49 additions & 34 deletions src/main/java/graphql/execution/RxExecutionStrategy.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
package graphql.execution;

import graphql.ExecutionResult;
import graphql.GraphQLException;
import graphql.execution.ExecutionContext;
import graphql.execution.ExecutionStrategy;
import graphql.language.Field;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLType;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;

import org.apache.commons.lang3.tuple.Pair;

public class RxExecutionStrategy extends ExecutionStrategy {
import graphql.ExecutionResult;
import graphql.language.Field;
import graphql.schema.GraphQLList;
import rx.Observable;

private final static Logger logger = LoggerFactory.getLogger(RxExecutionStrategy.class);
public class RxExecutionStrategy extends ExecutionStrategy {

@Override
public ExecutionResult execute(ExecutionContext executionContext, GraphQLObjectType parentType, Object source, Map<String, List<Field>> fields) {

List<Observable<Pair<String, ?>>> observables = new ArrayList<>();
public ExecutionResult execute(ExecutionContext executionContext, ExecutionParameters parameters)
throws NonNullableFieldWasNullException
{
List<Observable<Pair<String, ?>>> observables = new ArrayList<>();
Map<String, List<Field>> fields = parameters.fields();
for (String fieldName : fields.keySet()) {
final List<Field> fieldList = fields.get(fieldName);

ExecutionResult executionResult = resolveField(executionContext, parentType, source, fieldList);
ExecutionResult executionResult = resolveField(executionContext, parameters, fieldList);

if (executionResult instanceof RxExecutionResult) {
RxExecutionResult rxResult = (RxExecutionResult)executionResult;
Expand Down Expand Up @@ -60,26 +54,47 @@ public ExecutionResult execute(ExecutionContext executionContext, GraphQLObjectT

return new RxExecutionResult(result, Observable.just(executionContext.getErrors()));
}

@Override
protected ExecutionResult completeValue(ExecutionContext executionContext, GraphQLType fieldType, List<Field> fields, Object result) {
if (result instanceof Observable) {
return new RxExecutionResult(((Observable<?>) result).map(r -> super.completeValue(executionContext, fieldType, fields, r)), null);
protected ExecutionResult completeValue(ExecutionContext executionContext, ExecutionParameters parameters,
List<Field> fields)
{
Object result = parameters.source();
if (result instanceof Observable) {
return new RxExecutionResult(((Observable<?>) result).map(r -> {

ExecutionParameters newParameters = ExecutionParameters.newParameters()
.typeInfo(parameters.typeInfo())
.fields(parameters.fields())
.source(r).build();

return super.completeValue(executionContext, newParameters, fields);
}), null);
}
return super.completeValue(executionContext, fieldType, fields, result);
return super.completeValue(executionContext, parameters, fields);
}

@Override
protected ExecutionResult completeValueForList(ExecutionContext executionContext, GraphQLList fieldType, List<Field> fields, List<Object> result) {
Observable<?> resultObservable =
protected ExecutionResult completeValueForList(ExecutionContext executionContext, ExecutionParameters parameters,
List<Field> fields, Iterable<Object> result)
{
TypeInfo typeInfo = parameters.typeInfo();
GraphQLList fieldType = typeInfo.castType(GraphQLList.class);
AtomicInteger idx = new AtomicInteger(0);
Observable<?> resultObservable =
Observable.from(
IntStream.range(0, result.size())
.mapToObj(idx -> new ListTuple(idx, result.get(idx)))
.toArray(ListTuple[]::new)
StreamSupport.stream(result.spliterator(), false)
.map(i -> new ListTuple(idx.getAndIncrement(), i))
.toArray(ListTuple[]::new)
)
.flatMap(tuple -> {
ExecutionResult executionResult = completeValue(executionContext, fieldType.getWrappedType(), fields, tuple.result);

TypeInfo newType = typeInfo.asType(fieldType.getWrappedType());
ExecutionParameters newParameters = ExecutionParameters.newParameters()
.typeInfo(newType)
.fields(parameters.fields())
.source(tuple.result).build();

ExecutionResult executionResult = completeValue(executionContext, newParameters, fields);
if (executionResult instanceof RxExecutionResult) {
return Observable.zip(Observable.just(tuple.index), ((RxExecutionResult)executionResult).getDataObservable(), ListTuple::new);
}
Expand All @@ -95,7 +110,7 @@ protected ExecutionResult completeValueForList(ExecutionContext executionContext

return new RxExecutionResult(resultObservable, null);
}

private class ListTuple {
public int index;
public Object result;
Expand Down
16 changes: 13 additions & 3 deletions src/test/groovy/graphql/HelloWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public static void main(String[] args) {
.query(queryType)
.build();

Observable<?> result = ((RxExecutionResult)new GraphQL(schema, new RxExecutionStrategy()).execute("{hello}")).getDataObservable();
GraphQL graphQL = GraphQL.newGraphQL(schema)
.queryExecutionStrategy(new RxExecutionStrategy())
.mutationExecutionStrategy(new RxExecutionStrategy())
.build();
Observable<?> result = ((RxExecutionResult) graphQL.execute("{hello}")).getDataObservable();

result.subscribe(System.out::println);
}
Expand All @@ -52,9 +56,15 @@ public void helloWorldTest() {
.query(queryType)
.build();

RxExecutionResult executionResult = (RxExecutionResult)new GraphQL(schema, new RxExecutionStrategy()).execute("{hello}");
GraphQL graphQL = GraphQL.newGraphQL(schema)
.queryExecutionStrategy(new RxExecutionStrategy())
.mutationExecutionStrategy(new RxExecutionStrategy())
.build();

RxExecutionResult executionResult = (RxExecutionResult) graphQL.execute("{hello}");

Observable<Map<String, Object>> result = (Observable<Map<String, Object>>)executionResult.getDataObservable();
@SuppressWarnings("unchecked")
Observable<Map<String, Object>> result = (Observable<Map<String, Object>>) executionResult.getDataObservable();

TestSubscriber<Map<String, Object>> testSubscriber = new TestSubscriber<>();

Expand Down