Search in sources :

Example 1 with ExecutionInput

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

the class ExecutionExamples method simpleQueryExecution.

private void simpleQueryExecution() throws Exception {
    GraphQLSchema schema = GraphQLSchema.newSchema().query(queryType).build();
    GraphQL graphQL = GraphQL.newGraphQL(schema).build();
    ExecutionInput executionInput = ExecutionInput.newExecutionInput().query("query { hero { name } }").build();
    ExecutionResult executionResult = graphQL.execute(executionInput);
    Object data = executionResult.getData();
    List<GraphQLError> errors = executionResult.getErrors();
}
Also used : GraphQL(graphql.GraphQL) GraphQLError(graphql.GraphQLError) ExecutionResult(graphql.ExecutionResult) ExecutionInput(graphql.ExecutionInput) GraphQLSchema(graphql.schema.GraphQLSchema)

Example 2 with ExecutionInput

use of graphql.ExecutionInput 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 3 with ExecutionInput

use of graphql.ExecutionInput 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 4 with ExecutionInput

use of graphql.ExecutionInput in project incubator-skywalking by apache.

the class GraphQLHandler method execute.

private JsonObject execute(String request, Map<String, Object> variables) {
    try {
        ExecutionInput executionInput = ExecutionInput.newExecutionInput().query(request).variables(variables).build();
        ExecutionResult executionResult = graphQL.execute(executionInput);
        logger.info("Execution result is {}", executionResult);
        Object data = executionResult.getData();
        List<GraphQLError> errors = executionResult.getErrors();
        JsonObject jsonObject = new JsonObject();
        if (data != null) {
            jsonObject.add(DATA, gson.fromJson(gson.toJson(data), JsonObject.class));
        }
        if (CollectionUtils.isNotEmpty(errors)) {
            JsonArray errorArray = new JsonArray();
            errors.forEach(error -> {
                JsonObject errorJson = new JsonObject();
                errorJson.addProperty(MESSAGE, error.getMessage());
                errorArray.add(errorJson);
            });
            jsonObject.add(ERRORS, errorArray);
        }
        return jsonObject;
    } catch (Throwable e) {
        logger.error(e.getMessage(), e);
        JsonObject jsonObject = new JsonObject();
        JsonArray errorArray = new JsonArray();
        JsonObject errorJson = new JsonObject();
        errorJson.addProperty(MESSAGE, e.getMessage());
        errorArray.add(errorJson);
        jsonObject.add(ERRORS, errorArray);
        return jsonObject;
    }
}
Also used : JsonArray(com.google.gson.JsonArray) GraphQLError(graphql.GraphQLError) JsonObject(com.google.gson.JsonObject) ExecutionResult(graphql.ExecutionResult) JsonObject(com.google.gson.JsonObject) ExecutionInput(graphql.ExecutionInput)

Example 5 with ExecutionInput

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

the class ConcernsExamples method contextHelper.

private void contextHelper() {
    // 
    // this could be code that authorises the user in some way and sets up enough context
    // that can be used later inside data fetchers allowing them
    // to do their job
    // 
    UserContext contextForUser = YourGraphqlContextBuilder.getContextForUser(getCurrentUser());
    ExecutionInput executionInput = ExecutionInput.newExecutionInput().context(contextForUser).build();
    ExecutionResult executionResult = graphQL.execute(executionInput);
    // ...
    // 
    // later you are able to use this context object when a data fetcher is invoked
    // 
    DataFetcher dataFetcher = new DataFetcher() {

        @Override
        public Object get(DataFetchingEnvironment environment) {
            UserContext userCtx = environment.getContext();
            Long businessObjId = environment.getArgument("businessObjId");
            return invokeBusinessLayerMethod(userCtx, businessObjId);
        }
    };
}
Also used : ExecutionResult(graphql.ExecutionResult) ExecutionInput(graphql.ExecutionInput) DataFetcher(graphql.schema.DataFetcher) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment)

Aggregations

ExecutionInput (graphql.ExecutionInput)7 ExecutionResult (graphql.ExecutionResult)7 GraphQL (graphql.GraphQL)5 GraphQLSchema (graphql.schema.GraphQLSchema)4 GraphQLError (graphql.GraphQLError)2 DataLoaderRegistry (org.dataloader.DataLoaderRegistry)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 ExecutionInput.newExecutionInput (graphql.ExecutionInput.newExecutionInput)1 BatchedExecutionStrategy (graphql.execution.batched.BatchedExecutionStrategy)1 ChainedInstrumentation (graphql.execution.instrumentation.ChainedInstrumentation)1 Instrumentation (graphql.execution.instrumentation.Instrumentation)1 DataLoaderDispatcherInstrumentation (graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation)1 TracingInstrumentation (graphql.execution.instrumentation.tracing.TracingInstrumentation)1 DataFetcher (graphql.schema.DataFetcher)1 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)1 HashMap (java.util.HashMap)1