Search in sources :

Example 21 with ExecutionResult

use of graphql.ExecutionResult in project nextprot-api by calipho-sib.

the class GraphQlExecutorImpl method executeRequest.

@Override
public Object executeRequest(Map requestBody) {
    ExecutionResult executionResult = graphQL.execute((String) requestBody.get("query"));
    HashMap result = new LinkedHashMap<String, Object>();
    if (executionResult.getErrors().size() > 0) {
        result.put("errors", executionResult.getErrors());
    // log.error("Errors: {}", executionResult.getErrors());
    }
    result.put("data", executionResult.getData());
    return result;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ExecutionResult(graphql.ExecutionResult) LinkedHashMap(java.util.LinkedHashMap)

Example 22 with ExecutionResult

use of graphql.ExecutionResult in project OpenTripPlanner by opentripplanner.

the class GraphIndexTest method testGraphQLNested.

public void testGraphQLNested() {
    String query = "query Agency{\n" + "    viewer {" + "    agency(id: \"agency\"){\n" + "        name\n" + "        routes{\n" + "            shortName" + "        }" + "    }}\n" + "}\n";
    ExecutionResult result = graph.index.graphQL.execute(query);
    assertTrue(result.getErrors().isEmpty());
    Map<String, Object> data = (Map<String, Object>) result.getData();
    assertEquals(18, ((List) ((Map) ((Map) data.get("viewer")).get("agency")).get("routes")).size());
}
Also used : ExecutionResult(graphql.ExecutionResult) Map(java.util.Map)

Example 23 with ExecutionResult

use of graphql.ExecutionResult in project OpenTripPlanner by opentripplanner.

the class GraphIndexTest method testGraphQLSimple.

public void testGraphQLSimple() {
    String query = "query Agency{" + "    agency(id: \"agency\"){" + "        name" + "    }" + "}";
    ExecutionResult result = graph.index.graphQL.execute(query);
    assertTrue(result.getErrors().isEmpty());
    Map<String, Object> data = (Map<String, Object>) result.getData();
    assertEquals("Fake Agency", ((Map) data.get("agency")).get("name"));
}
Also used : ExecutionResult(graphql.ExecutionResult) Map(java.util.Map)

Example 24 with ExecutionResult

use of graphql.ExecutionResult in project OpenTripPlanner by opentripplanner.

the class GraphIndexTest method testGraphQLIntrospectionQuery.

public void testGraphQLIntrospectionQuery() {
    String query = "  query IntrospectionQuery {\n" + "    __schema {\n" + "      queryType { name }\n" + "      mutationType { name }\n" + "      types {\n" + "        ...FullType\n" + "      }\n" + "      directives {\n" + "        name\n" + "        description\n" + "        args {\n" + "          ...InputValue\n" + "        }\n" + "        onOperation\n" + "        onFragment\n" + "        onField\n" + "      }\n" + "    }\n" + "  }\n" + "\n" + "  fragment FullType on __Type {\n" + "    kind\n" + "    name\n" + "    description\n" + "    fields {\n" + "      name\n" + "      description\n" + "      args {\n" + "        ...InputValue\n" + "      }\n" + "      type {\n" + "        ...TypeRef\n" + "      }\n" + "      isDeprecated\n" + "      deprecationReason\n" + "    }\n" + "    inputFields {\n" + "      ...InputValue\n" + "    }\n" + "    interfaces {\n" + "      ...TypeRef\n" + "    }\n" + "    enumValues {\n" + "      name\n" + "      description\n" + "      isDeprecated\n" + "      deprecationReason\n" + "    }\n" + "    possibleTypes {\n" + "      ...TypeRef\n" + "    }\n" + "  }\n" + "\n" + "  fragment InputValue on __InputValue {\n" + "    name\n" + "    description\n" + "    type { ...TypeRef }\n" + "    defaultValue\n" + "  }\n" + "\n" + "  fragment TypeRef on __Type {\n" + "    kind\n" + "    name\n" + "    ofType {\n" + "      kind\n" + "      name\n" + "      ofType {\n" + "        kind\n" + "        name\n" + "        ofType {\n" + "          kind\n" + "          name\n" + "        }\n" + "      }\n" + "    }\n" + "  }";
    ExecutionResult result = graph.index.graphQL.execute(query);
    assertTrue(result.getErrors().isEmpty());
}
Also used : ExecutionResult(graphql.ExecutionResult)

Example 25 with ExecutionResult

use of graphql.ExecutionResult in project carbon-apimgt by wso2.

the class QueryAnalyzer method analyseQueryDepth.

/**
 * This method analyses the query depth.
 *
 * @param maxQueryDepth maximum query depth
 * @param payload       payload of the request
 * @return true, if the query depth does not exceed the maximum value or false, if query depth exceeds the maximum
 */
public QueryAnalyzerResponseDTO analyseQueryDepth(int maxQueryDepth, String payload) {
    if (log.isDebugEnabled()) {
        log.debug("Analyzing query depth for " + payload + " and max query depth:" + maxQueryDepth);
    }
    QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = new QueryAnalyzerResponseDTO();
    // If maxQueryDepth is a positive value, perform the depth limitation check. Otherwise, bypass the check.
    if (maxQueryDepth > 0) {
        MaxQueryDepthInstrumentation maxQueryDepthInstrumentation = new MaxQueryDepthInstrumentation(maxQueryDepth);
        GraphQL runtime = GraphQL.newGraphQL(schema).instrumentation(maxQueryDepthInstrumentation).build();
        ExecutionResult executionResult = runtime.execute(payload);
        List<GraphQLError> errors = executionResult.getErrors();
        if (errors.size() > 0) {
            for (GraphQLError error : errors) {
                queryAnalyzerResponseDTO.addErrorToList((error.getMessage()));
            }
            // TODO: https://github.com/wso2/carbon-apimgt/issues/8147
            queryAnalyzerResponseDTO.getErrorList().removeIf(s -> s.contains("non-nullable"));
            if (queryAnalyzerResponseDTO.getErrorList().size() == 0) {
                if (log.isDebugEnabled()) {
                    log.debug("Maximum query depth of " + maxQueryDepth + " was not exceeded");
                }
                queryAnalyzerResponseDTO.setSuccess(true);
                return queryAnalyzerResponseDTO;
            }
            log.error(queryAnalyzerResponseDTO.getErrorList().toString());
            queryAnalyzerResponseDTO.setSuccess(false);
            return queryAnalyzerResponseDTO;
        }
    }
    queryAnalyzerResponseDTO.setSuccess(true);
    return queryAnalyzerResponseDTO;
}
Also used : MaxQueryDepthInstrumentation(graphql.analysis.MaxQueryDepthInstrumentation) GraphQL(graphql.GraphQL) GraphQLError(graphql.GraphQLError) ExecutionResult(graphql.ExecutionResult) QueryAnalyzerResponseDTO(org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO)

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