Search in sources :

Example 6 with ExecutionResult

use of graphql.ExecutionResult in project graphql-java by graphql-java.

the class ExecutionExamples method equivalentSerialAndAsyncQueryExecution.

private void equivalentSerialAndAsyncQueryExecution() throws Exception {
    ExecutionResult executionResult = graphQL.execute(executionInput);
    // the above is equivalent to the following code (in long hand)
    CompletableFuture<ExecutionResult> promise = graphQL.executeAsync(executionInput);
    ExecutionResult executionResult2 = promise.join();
}
Also used : ExecutionResult(graphql.ExecutionResult)

Example 7 with ExecutionResult

use of graphql.ExecutionResult in project graphql-java by graphql-java.

the class BatchCompare method batchedRun.

void batchedRun() {
    System.out.println("=== BatchedExecutionStrategy ===");
    GraphQLSchema schema = buildBatchedSchema();
    GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new BatchedExecutionStrategy()).build();
    ExecutionInput executionInput = ExecutionInput.newExecutionInput().query("query { shops { id name departments { id name products { id name } } } }").build();
    ExecutionResult result = graphQL.execute(executionInput);
    System.out.println("\nExecutionResult: " + result.toSpecification());
}
Also used : GraphQL(graphql.GraphQL) ExecutionResult(graphql.ExecutionResult) ExecutionInput(graphql.ExecutionInput) GraphQLSchema(graphql.schema.GraphQLSchema) BatchedExecutionStrategy(graphql.execution.batched.BatchedExecutionStrategy)

Example 8 with ExecutionResult

use of graphql.ExecutionResult in project graphql-java by graphql-java.

the class HelloWorld method main.

public static void main(String[] args) {
    String schema = "type Query{hello: String}";
    SchemaParser schemaParser = new SchemaParser();
    TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);
    RuntimeWiring runtimeWiring = newRuntimeWiring().type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world"))).build();
    SchemaGenerator schemaGenerator = new SchemaGenerator();
    GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
    GraphQL build = GraphQL.newGraphQL(graphQLSchema).build();
    ExecutionResult executionResult = build.execute("{hello}");
    System.out.println(executionResult.getData().toString());
// Prints: {hello=world}
}
Also used : ExecutionResult(graphql.ExecutionResult) StaticDataFetcher(graphql.schema.StaticDataFetcher) GraphQL(graphql.GraphQL) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaParser(graphql.schema.idl.SchemaParser) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) GraphQLSchema(graphql.schema.GraphQLSchema) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) SchemaGenerator(graphql.schema.idl.SchemaGenerator) RuntimeWiring(graphql.schema.idl.RuntimeWiring) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) GraphQL(graphql.GraphQL) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) SchemaGenerator(graphql.schema.idl.SchemaGenerator) StaticDataFetcher(graphql.schema.StaticDataFetcher) ExecutionResult(graphql.ExecutionResult) SchemaParser(graphql.schema.idl.SchemaParser) GraphQLSchema(graphql.schema.GraphQLSchema)

Example 9 with ExecutionResult

use of graphql.ExecutionResult in project graphql-java by graphql-java.

the class HttpMain method handleStarWars.

private void handleStarWars(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
    // 
    // this builds out the parameters we need like the graphql query from the http request
    QueryParameters parameters = QueryParameters.from(httpRequest);
    if (parameters.getQuery() == null) {
        // 
        // how to handle nonsensical requests is up to your application
        httpResponse.setStatus(400);
        return;
    }
    ExecutionInput.Builder executionInput = newExecutionInput().query(parameters.getQuery()).operationName(parameters.getOperationName()).variables(parameters.getVariables());
    // 
    // This example uses the DataLoader technique to ensure that the most efficient
    // loading of data (in this case StarWars characters) happens.  We pass that to data
    // fetchers via the graphql context object.
    // 
    DataLoaderRegistry dataLoaderRegistry = buildDataLoaderRegistry();
    // 
    // the context object is something that means something to down stream code.  It is instructions
    // from yourself to your other code such as DataFetchers.  The engine passes this on unchanged and
    // makes it available to inner code
    // 
    // the graphql guidance says  :
    // 
    // - GraphQL should be placed after all authentication middleware, so that you
    // - have access to the same session and user information you would in your
    // - HTTP endpoint handlers.
    // 
    Map<String, Object> context = new HashMap<>();
    context.put("YouAppSecurityClearanceLevel", "CodeRed");
    context.put("YouAppExecutingUser", "Dr Nefarious");
    context.put("dataloaderRegistry", dataLoaderRegistry);
    executionInput.context(context);
    // 
    // you need a schema in order to execute queries
    GraphQLSchema schema = buildStarWarsSchema();
    DataLoaderDispatcherInstrumentation dlInstrumentation = new DataLoaderDispatcherInstrumentation(dataLoaderRegistry, newOptions().includeStatistics(true));
    Instrumentation instrumentation = new ChainedInstrumentation(asList(new TracingInstrumentation(), dlInstrumentation));
    // finally you build a runtime graphql object and execute the query
    GraphQL graphQL = GraphQL.newGraphQL(schema).instrumentation(instrumentation).build();
    ExecutionResult executionResult = graphQL.execute(executionInput.build());
    returnAsJson(httpResponse, executionResult);
}
Also used : ChainedInstrumentation(graphql.execution.instrumentation.ChainedInstrumentation) HashMap(java.util.HashMap) GraphQL(graphql.GraphQL) DataLoaderDispatcherInstrumentation(graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation) DataLoaderDispatcherInstrumentation(graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation) TracingInstrumentation(graphql.execution.instrumentation.tracing.TracingInstrumentation) ChainedInstrumentation(graphql.execution.instrumentation.ChainedInstrumentation) Instrumentation(graphql.execution.instrumentation.Instrumentation) ExecutionResult(graphql.ExecutionResult) GraphQLSchema(graphql.schema.GraphQLSchema) TracingInstrumentation(graphql.execution.instrumentation.tracing.TracingInstrumentation) DataLoaderRegistry(org.dataloader.DataLoaderRegistry) ExecutionInput.newExecutionInput(graphql.ExecutionInput.newExecutionInput) ExecutionInput(graphql.ExecutionInput)

Example 10 with ExecutionResult

use of graphql.ExecutionResult in project tutorials by eugenp.

the class UserHandler method handle.

@Override
public void handle(Context context) throws Exception {
    context.parse(Map.class).then(payload -> {
        Map<String, Object> parameters = (Map<String, Object>) payload.get("parameters");
        ExecutionResult executionResult = graphql.execute(payload.get(SchemaUtils.QUERY).toString(), null, this, parameters);
        Map<String, Object> result = new LinkedHashMap<>();
        if (executionResult.getErrors().isEmpty()) {
            result.put(SchemaUtils.DATA, executionResult.getData());
        } else {
            result.put(SchemaUtils.ERRORS, executionResult.getErrors());
            LOGGER.warning("Errors: " + executionResult.getErrors());
        }
        context.render(json(result));
    });
}
Also used : ExecutionResult(graphql.ExecutionResult) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ExecutionResult (graphql.ExecutionResult)42 GraphQL (graphql.GraphQL)17 List (java.util.List)10 GraphQLSchema (graphql.schema.GraphQLSchema)9 ExecutionInput (graphql.ExecutionInput)8 Instrumentation (graphql.execution.instrumentation.Instrumentation)8 Field (graphql.language.Field)8 ExecutionResultImpl (graphql.ExecutionResultImpl)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 GraphQLError (graphql.GraphQLError)6 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)6 LinkedHashMap (java.util.LinkedHashMap)6 GraphQLObjectType (graphql.schema.GraphQLObjectType)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 InstrumentationExecutionStrategyParameters (graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters)4 HashMap (java.util.HashMap)4 Collectors.toList (java.util.stream.Collectors.toList)3 InstrumentationFieldCompleteParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters)2 GraphQLList (graphql.schema.GraphQLList)2