| 35 | import static graphql.StarWarsSchema.queryType; |
| 36 | |
| 37 | @SuppressWarnings({"unused", "UnnecessaryLocalVariable", "Convert2Lambda", "unused", "ClassCanBeStatic", "TypeParameterUnusedInFormals"}) |
| 38 | public class ExecutionExamples { |
| 39 | |
| 40 | public static void main(String[] args) throws Exception { |
| 41 | new ExecutionExamples().simpleQueryExecution(); |
| 42 | } |
| 43 | |
| 44 | private void simpleQueryExecution() throws Exception { |
| 45 | //::FigureA |
| 46 | GraphQLSchema schema = GraphQLSchema.newSchema() |
| 47 | .query(queryType) |
| 48 | .build(); |
| 49 | |
| 50 | GraphQL graphQL = GraphQL.newGraphQL(schema) |
| 51 | .build(); |
| 52 | |
| 53 | ExecutionInput executionInput = ExecutionInput.newExecutionInput().query("query { hero { name } }") |
| 54 | .build(); |
| 55 | |
| 56 | ExecutionResult executionResult = graphQL.execute(executionInput); |
| 57 | |
| 58 | Object data = executionResult.getData(); |
| 59 | List<GraphQLError> errors = executionResult.getErrors(); |
| 60 | //::/FigureA |
| 61 | } |
| 62 | |
| 63 | @SuppressWarnings({"Convert2MethodRef", "unused", "FutureReturnValueIgnored"}) |
| 64 | private void simpleAsyncQueryExecution() throws Exception { |
| 65 | //::FigureB |
| 66 | GraphQL graphQL = buildSchema(); |
| 67 | |
| 68 | ExecutionInput executionInput = ExecutionInput.newExecutionInput().query("query { hero { name } }") |
| 69 | .build(); |
| 70 | |
| 71 | CompletableFuture<ExecutionResult> promise = graphQL.executeAsync(executionInput); |
| 72 | |
| 73 | promise.thenAccept(executionResult -> { |
| 74 | // here you might send back the results as JSON over HTTP |
| 75 | encodeResultToJsonAndSendResponse(executionResult); |
| 76 | }); |
| 77 | |
| 78 | promise.join(); |
| 79 | //::/FigureB |
| 80 | } |
| 81 | |
| 82 | private GraphQL graphQL = buildSchema(); |
| 83 | private ExecutionInput executionInput = ExecutionInput.newExecutionInput().query("query { hero { name } }") |
| 84 | .build(); |
| 85 | |
| 86 | private void equivalentSerialAndAsyncQueryExecution() throws Exception { |
| 87 | //::FigureC |
| 88 | |
| 89 | ExecutionResult executionResult = graphQL.execute(executionInput); |
| 90 | |
| 91 | // the above is equivalent to the following code (in long hand) |
| 92 | |
| 93 | CompletableFuture<ExecutionResult> promise = graphQL.executeAsync(executionInput); |
| 94 | ExecutionResult executionResult2 = promise.join(); |
nothing calls this directly
no test coverage detected
searching dependent graphs…