├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src ├── main └── java │ └── graphql │ └── execution │ ├── RxExecutionResult.java │ └── RxExecutionStrategy.java └── test └── groovy └── graphql └── HelloWorld.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .gradle 3 | build 4 | 5 | graphql-rxjava.iml 6 | 7 | .DS_Store 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | install: true 5 | sudo: false 6 | addons: 7 | apt: 8 | packages: 9 | - oracle-java8-installer 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 NFL 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # graphql-rxjava 2 | 3 | This is an execution strategy for [graphql-java](https://github.com/andimarek/graphql-java) that makes 4 | it easier to use rxjava's Observable. It currently requires Java8. 5 | 6 | [![Build Status](https://travis-ci.org/nfl/graphql-rxjava.svg)](https://travis-ci.org/nfl/graphql-rxjava) 7 | 8 | # Table of Contents 9 | 10 | - [Rx Hello World](#hello-world) 11 | - [License](#license) 12 | 13 | 14 | ### Rx Hello World 15 | 16 | This is the famous "hello world" in graphql-rxjava: 17 | 18 | ```java 19 | import graphql.schema.GraphQLObjectType; 20 | import graphql.schema.GraphQLSchema; 21 | 22 | import graphql.execution.RxExecutionStrategy; 23 | import graphql.execution.RxExecutionResult; 24 | 25 | import static graphql.Scalars.GraphQLString; 26 | import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; 27 | import static graphql.schema.GraphQLObjectType.newObject; 28 | 29 | public class HelloWorld { 30 | 31 | public static void main(String[] args) { 32 | 33 | GraphQLObjectType queryType = newObject() 34 | .name("helloWorldQuery") 35 | .field(newFieldDefinition() 36 | .type(GraphQLString) 37 | .name("hello") 38 | .staticValue(Observable.just("world"))) 39 | .build()) 40 | .build(); 41 | 42 | GraphQLSchema schema = GraphQLSchema.newSchema() 43 | .query(queryType) 44 | .build(); 45 | 46 | Observable result = ((RxExecutionResult)new GraphQL(schema, new RxExecutionStrategy()).execute("{hello}")).getDataObservable(); 47 | 48 | result.subscribe(System.out::println); 49 | // Prints: {hello=world} 50 | } 51 | } 52 | ``` 53 | 54 | ### Getting started with gradle 55 | 56 | Make sure `mavenCentral` is among your repos: 57 | 58 | ```groovy 59 | repositories { 60 | mavenCentral() 61 | } 62 | 63 | ``` 64 | Dependency: 65 | 66 | ```groovy 67 | dependencies { 68 | compile 'com.graphql-java:graphql-rxjava:0.0.1' 69 | } 70 | 71 | ``` 72 | 73 | ### License 74 | 75 | graphql-rxjava is licensed under the MIT License. See [LICENSE](LICENSE) for details. 76 | 77 | Copyright (c) 2015, NFL and [Contributors](https://github.com/nfl/graphql-java/graphs/contributors) 78 | 79 | [graphql-js License](https://github.com/graphql/graphql-js/blob/master/LICENSE) 80 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'maven' 3 | apply plugin: 'signing' 4 | 5 | sourceCompatibility = 1.8 6 | version = '0.0.1' 7 | group = 'com.graphql-java' 8 | 9 | 10 | repositories { 11 | maven { url "http://dl.bintray.com/andimarek/graphql-java" } 12 | mavenCentral() 13 | } 14 | 15 | apply plugin: 'groovy' 16 | 17 | jar { 18 | from "LICENSE" 19 | } 20 | 21 | dependencies { 22 | compile 'org.antlr:antlr4-runtime:4.5.1' 23 | compile 'org.slf4j:slf4j-api:1.7.12' 24 | compile 'com.graphql-java:graphql-java:2015-11-04T21-08-13' 25 | compile 'io.reactivex:rxjava:1.0.12' 26 | compile 'org.apache.commons:commons-lang3:3.1' 27 | 28 | testCompile group: 'junit', name: 'junit', version: '4.11' 29 | testCompile 'org.spockframework:spock-core:1.0-groovy-2.4' 30 | testCompile 'org.codehaus.groovy:groovy-all:2.4.4' 31 | testCompile 'cglib:cglib-nodep:3.1' 32 | testCompile 'org.objenesis:objenesis:2.1' 33 | } 34 | 35 | task sourcesJar(type: Jar) { 36 | dependsOn classes 37 | classifier 'sources' 38 | from sourceSets.main.allSource 39 | } 40 | 41 | task javadocJar(type: Jar, dependsOn: javadoc) { 42 | classifier = 'javadoc' 43 | from javadoc.destinationDir 44 | } 45 | 46 | 47 | artifacts { 48 | archives sourcesJar 49 | archives javadocJar 50 | } 51 | 52 | signing { 53 | sign configurations.archives 54 | } 55 | 56 | if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')) { 57 | uploadArchives { 58 | repositories { 59 | mavenDeployer { 60 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 61 | 62 | repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { 63 | authentication(userName: ossrhUsername, password: ossrhPassword) 64 | } 65 | 66 | snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { 67 | authentication(userName: ossrhUsername, password: ossrhPassword) 68 | } 69 | 70 | pom.project { 71 | name 'graphql-rxjava' 72 | description 'GraphQL Rx-Java' 73 | url "https://github.com/nfl/graphql-rxjava" 74 | scm { 75 | url "https://github.com/nfl/graphql-rxjava" 76 | connection "https://github.com/nfl/graphql-rxjava" 77 | developerConnection "https://github.com/nfl/graphql-rxjava" 78 | } 79 | licenses { 80 | license { 81 | name 'MIT' 82 | url 'https://github.com/nfl/graphql-rxjava/blob/master/LICENSE' 83 | distribution 'repo' 84 | } 85 | } 86 | developers { 87 | developer { 88 | id 'tberman' 89 | name 'Todd Berman' 90 | } 91 | } 92 | } 93 | } 94 | } 95 | } 96 | } 97 | 98 | task wrapper(type: Wrapper) { 99 | gradleVersion = '2.5' 100 | distributionUrl = "http://services.gradle.org/distributions/gradle-${gradleVersion}-all.zip" 101 | } 102 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.daemon=true 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nfl/graphql-rxjava/5639bd404846a2817ac210731b7bb522c05c8dbc/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Jul 15 03:11:00 CEST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /src/main/java/graphql/execution/RxExecutionResult.java: -------------------------------------------------------------------------------- 1 | package graphql.execution; 2 | 3 | import graphql.ExecutionResult; 4 | import graphql.GraphQLError; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import rx.Observable; 8 | 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | public class RxExecutionResult implements ExecutionResult { 13 | 14 | private static final Logger logger = LoggerFactory.getLogger(RxExecutionResult.class); 15 | 16 | private Observable dataObservable; 17 | 18 | private Observable> errorsObservable; 19 | 20 | public RxExecutionResult(Observable data, Observable> errors) { 21 | dataObservable = data; 22 | errorsObservable = errors; 23 | } 24 | 25 | public Observable getDataObservable() { 26 | return dataObservable; 27 | } 28 | 29 | public Observable> getErrorsObservable() { 30 | return errorsObservable; 31 | } 32 | 33 | @Override 34 | public Object getData() { 35 | logger.warn("getData() called instead of getDataObservable(), blocking (likely a bug)"); 36 | return dataObservable.toBlocking().first(); 37 | } 38 | 39 | @Override 40 | public List getErrors() { 41 | logger.warn("getErrors() called instead of getErrorsObservable(), blocking (likely a bug)"); 42 | return errorsObservable.toBlocking().first(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/graphql/execution/RxExecutionStrategy.java: -------------------------------------------------------------------------------- 1 | package graphql.execution; 2 | 3 | import graphql.ExecutionResult; 4 | import graphql.GraphQLException; 5 | import graphql.execution.ExecutionContext; 6 | import graphql.execution.ExecutionStrategy; 7 | import graphql.language.Field; 8 | import graphql.schema.GraphQLList; 9 | import graphql.schema.GraphQLObjectType; 10 | import graphql.schema.GraphQLType; 11 | import org.apache.commons.lang3.tuple.Pair; 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | import rx.Observable; 15 | 16 | import java.util.ArrayList; 17 | import java.util.Comparator; 18 | import java.util.List; 19 | import java.util.Map; 20 | import java.util.stream.Collectors; 21 | import java.util.stream.IntStream; 22 | 23 | 24 | public class RxExecutionStrategy extends ExecutionStrategy { 25 | 26 | private final static Logger logger = LoggerFactory.getLogger(RxExecutionStrategy.class); 27 | 28 | @Override 29 | public ExecutionResult execute(ExecutionContext executionContext, GraphQLObjectType parentType, Object source, Map> fields) { 30 | 31 | List>> observables = new ArrayList<>(); 32 | for (String fieldName : fields.keySet()) { 33 | final List fieldList = fields.get(fieldName); 34 | 35 | ExecutionResult executionResult = resolveField(executionContext, parentType, source, fieldList); 36 | 37 | if (executionResult instanceof RxExecutionResult) { 38 | RxExecutionResult rxResult = (RxExecutionResult)executionResult; 39 | Observable unwrapped = rxResult.getDataObservable().flatMap(potentialResult -> { 40 | if (potentialResult instanceof RxExecutionResult) { 41 | return ((RxExecutionResult) potentialResult).getDataObservable(); 42 | } 43 | 44 | if (potentialResult instanceof ExecutionResult) { 45 | return Observable.just(((ExecutionResult) potentialResult).getData()); 46 | } 47 | 48 | return Observable.just(potentialResult); 49 | }); 50 | 51 | observables.add(Observable.zip(Observable.just(fieldName), unwrapped, Pair::of)); 52 | } else { 53 | observables.add(Observable.just(Pair.of(fieldName, executionResult != null ? executionResult.getData() : null))); 54 | } 55 | } 56 | 57 | Observable> result = 58 | Observable.merge(observables) 59 | .toMap(Pair::getLeft, Pair::getRight); 60 | 61 | return new RxExecutionResult(result, Observable.just(executionContext.getErrors())); 62 | } 63 | 64 | @Override 65 | protected ExecutionResult completeValue(ExecutionContext executionContext, GraphQLType fieldType, List fields, Object result) { 66 | if (result instanceof Observable) { 67 | return new RxExecutionResult(((Observable) result).map(r -> super.completeValue(executionContext, fieldType, fields, r)), null); 68 | } 69 | return super.completeValue(executionContext, fieldType, fields, result); 70 | } 71 | 72 | @Override 73 | protected ExecutionResult completeValueForList(ExecutionContext executionContext, GraphQLList fieldType, List fields, List result) { 74 | Observable resultObservable = 75 | Observable.from( 76 | IntStream.range(0, result.size()) 77 | .mapToObj(idx -> new ListTuple(idx, result.get(idx))) 78 | .toArray(ListTuple[]::new) 79 | ) 80 | .flatMap(tuple -> { 81 | ExecutionResult executionResult = completeValue(executionContext, fieldType.getWrappedType(), fields, tuple.result); 82 | 83 | if (executionResult instanceof RxExecutionResult) { 84 | return Observable.zip(Observable.just(tuple.index), ((RxExecutionResult)executionResult).getDataObservable(), ListTuple::new); 85 | } 86 | return Observable.just(new ListTuple(tuple.index, executionResult.getData())); 87 | }) 88 | .toList() 89 | .map(listTuples -> { 90 | return listTuples.stream() 91 | .sorted(Comparator.comparingInt(x -> x.index)) 92 | .map(x -> x.result) 93 | .collect(Collectors.toList()); 94 | }); 95 | 96 | return new RxExecutionResult(resultObservable, null); 97 | } 98 | 99 | private class ListTuple { 100 | public int index; 101 | public Object result; 102 | 103 | public ListTuple(int index, Object result) { 104 | this.index = index; 105 | this.result = result; 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/test/groovy/graphql/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package graphql; 2 | 3 | 4 | import graphql.execution.RxExecutionResult; 5 | import graphql.execution.RxExecutionStrategy; 6 | import graphql.schema.GraphQLObjectType; 7 | import graphql.schema.GraphQLSchema; 8 | import org.junit.Test; 9 | import rx.Observable; 10 | import rx.observers.TestSubscriber; 11 | 12 | import java.util.Map; 13 | 14 | import static graphql.Scalars.GraphQLString; 15 | import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; 16 | import static graphql.schema.GraphQLObjectType.newObject; 17 | import static org.junit.Assert.assertEquals; 18 | 19 | public class HelloWorld { 20 | 21 | public static void main(String[] args) { 22 | GraphQLObjectType queryType = newObject() 23 | .name("helloWorldQuery") 24 | .field(newFieldDefinition() 25 | .type(GraphQLString) 26 | .name("hello") 27 | .staticValue(Observable.just("world")) 28 | .build()) 29 | .build(); 30 | 31 | GraphQLSchema schema = GraphQLSchema.newSchema() 32 | .query(queryType) 33 | .build(); 34 | 35 | Observable result = ((RxExecutionResult)new GraphQL(schema, new RxExecutionStrategy()).execute("{hello}")).getDataObservable(); 36 | 37 | result.subscribe(System.out::println); 38 | } 39 | 40 | @Test 41 | public void helloWorldTest() { 42 | GraphQLObjectType queryType = newObject() 43 | .name("helloWorldQuery") 44 | .field(newFieldDefinition() 45 | .type(GraphQLString) 46 | .name("hello") 47 | .staticValue(Observable.just("world")) 48 | .build()) 49 | .build(); 50 | 51 | GraphQLSchema schema = GraphQLSchema.newSchema() 52 | .query(queryType) 53 | .build(); 54 | 55 | RxExecutionResult executionResult = (RxExecutionResult)new GraphQL(schema, new RxExecutionStrategy()).execute("{hello}"); 56 | 57 | Observable> result = (Observable>)executionResult.getDataObservable(); 58 | 59 | TestSubscriber> testSubscriber = new TestSubscriber<>(); 60 | 61 | result.subscribe(testSubscriber); 62 | 63 | testSubscriber.awaitTerminalEvent(); 64 | 65 | testSubscriber.assertNoErrors(); 66 | 67 | Map response = testSubscriber.getOnNextEvents().get(0); 68 | 69 | assertEquals("world", response.get("hello")); 70 | } 71 | } 72 | --------------------------------------------------------------------------------