MCPcopy Index your code
hub / github.com/graphql-java/graphql-java / ExecutionExamples

Class ExecutionExamples

src/test/groovy/readme/ExecutionExamples.java:37–318  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

35import static graphql.StarWarsSchema.queryType;
36
37@SuppressWarnings({"unused", "UnnecessaryLocalVariable", "Convert2Lambda", "unused", "ClassCanBeStatic", "TypeParameterUnusedInFormals"})
38public 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();

Callers

nothing calls this directly

Calls 5

buildSchemaMethod · 0.95
newExecutionInputMethod · 0.95
newSchemaMethod · 0.95
buildMethod · 0.65
queryMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…