Search in sources :

Example 1 with ExecutionResult

use of graphql.ExecutionResult in project admin-console-beta by connexta.

the class GraphQLServlet method query.

private void query(String query, String operationName, Map<String, Object> variables, GraphQLSchema schema, HttpServletRequest req, HttpServletResponse resp, GraphQLContext context) throws IOException {
    if (Subject.getSubject(AccessController.getContext()) == null && context.getSubject().isPresent()) {
        Subject.doAs(context.getSubject().get(), new PrivilegedAction<Void>() {

            @Override
            @SneakyThrows
            public Void run() {
                query(query, operationName, variables, schema, req, resp, context);
                return null;
            }
        });
    } else {
        runListeners(operationListeners, l -> runListener(l, it -> it.beforeGraphQLOperation(context, operationName, query, variables)));
        ExecutionResult executionResult = new GraphQL(schema, getQueryExecutionStrategy(), getMutationExecutionStrategy()).execute(query, operationName, context, transformVariables(schema, query, variables));
        List<GraphQLError> errors = executionResult.getErrors();
        Object data = executionResult.getData();
        String response = mapper.writeValueAsString(createResultFromDataAndErrors(data, errors));
        resp.setContentType(APPLICATION_JSON_UTF8);
        resp.setStatus(STATUS_OK);
        resp.getWriter().write(response);
        if (errorsPresent(errors)) {
            runListeners(operationListeners, l -> l.onFailedGraphQLOperation(context, operationName, query, variables, data, errors));
        } else {
            runListeners(operationListeners, l -> l.onSuccessfulGraphQLOperation(context, operationName, query, variables, data));
        }
    }
}
Also used : InvalidSyntaxError(graphql.InvalidSyntaxError) Setter(lombok.Setter) GraphQL(graphql.GraphQL) ExecutionStrategy(graphql.execution.ExecutionStrategy) Getter(lombok.Getter) SneakyThrows(lombok.SneakyThrows) ServletException(javax.servlet.ServletException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExecutionResult(graphql.ExecutionResult) HttpServletRequest(javax.servlet.http.HttpServletRequest) CharStreams(com.google.common.io.CharStreams) GraphQLError(graphql.GraphQLError) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) GraphQLSchema(graphql.schema.GraphQLSchema) TypeReference(com.fasterxml.jackson.core.type.TypeReference) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) RuntimeJsonMappingException(com.fasterxml.jackson.databind.RuntimeJsonMappingException) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) PrivilegedAction(java.security.PrivilegedAction) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) Subject(javax.security.auth.Subject) Consumer(java.util.function.Consumer) ValidationError(graphql.validation.ValidationError) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Part(javax.servlet.http.Part) Optional(java.util.Optional) AccessController(java.security.AccessController) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) InputStream(java.io.InputStream) GraphQL(graphql.GraphQL) SneakyThrows(lombok.SneakyThrows) GraphQLError(graphql.GraphQLError) ExecutionResult(graphql.ExecutionResult)

Example 2 with ExecutionResult

use of graphql.ExecutionResult in project admin-console-beta by connexta.

the class ExecutionStrategyImpl method execute.

@Override
public ExecutionResult execute(ExecutionContext executionContext, GraphQLObjectType parentType, Object source, Map<String, List<Field>> fields) {
    Map<String, Object> results = new LinkedHashMap<String, Object>();
    for (String fieldName : fields.keySet()) {
        List<Field> fieldList = fields.get(fieldName);
        ExecutionResult resolvedResult = resolveField(executionContext, parentType, source, fieldList);
        results.put(fieldName, resolvedResult != null ? resolvedResult.getData() : null);
    }
    return new ExecutionResultImpl(results, executionContext.getErrors());
}
Also used : Field(graphql.language.Field) ExecutionResultImpl(graphql.ExecutionResultImpl) ExecutionResult(graphql.ExecutionResult) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with ExecutionResult

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

the class DeferredExamples method basicExample.

void basicExample(HttpServletResponse httpServletResponse, String deferredQuery) {
    GraphQLSchema schema = buildSchemaWithDirective();
    GraphQL graphQL = GraphQL.newGraphQL(schema).build();
    // 
    // deferredQuery contains the query with @defer directives in it
    // 
    ExecutionResult initialResult = graphQL.execute(ExecutionInput.newExecutionInput().query(deferredQuery).build());
    // 
    // then initial results happen first, the deferred ones will begin AFTER these initial
    // results have completed
    // 
    sendResult(httpServletResponse, initialResult);
    Map<Object, Object> extensions = initialResult.getExtensions();
    Publisher<ExecutionResult> deferredResults = (Publisher<ExecutionResult>) extensions.get(GraphQL.DEFERRED_RESULTS);
    // 
    // you subscribe to the deferred results like any other reactive stream
    // 
    deferredResults.subscribe(new Subscriber<ExecutionResult>() {

        Subscription subscription;

        @Override
        public void onSubscribe(Subscription s) {
            subscription = s;
            // 
            // how many you request is up to you
            subscription.request(10);
        }

        @Override
        public void onNext(ExecutionResult executionResult) {
            // 
            // as each deferred result arrives, send it to where it needs to go
            // 
            sendResult(httpServletResponse, executionResult);
            subscription.request(10);
        }

        @Override
        public void onError(Throwable t) {
            handleError(httpServletResponse, t);
        }

        @Override
        public void onComplete() {
            completeResponse(httpServletResponse);
        }
    });
}
Also used : GraphQL(graphql.GraphQL) ExecutionResult(graphql.ExecutionResult) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription) GraphQLSchema(graphql.schema.GraphQLSchema)

Example 4 with ExecutionResult

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

the class ExecutionExamples method toSpec.

public void toSpec() throws Exception {
    ExecutionResult executionResult = graphQL.execute(executionInput);
    Map<String, Object> toSpecificationResult = executionResult.toSpecification();
    sendAsJson(toSpecificationResult);
}
Also used : ExecutionResult(graphql.ExecutionResult)

Example 5 with ExecutionResult

use of graphql.ExecutionResult 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)

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