Search in sources :

Example 11 with GraphQL

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

the class GraphQLTest method main.

public static void main(String[] args) {
    String schema = "type Query{hello: String} schema{query: Query}";
    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 : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) StaticDataFetcher(graphql.schema.StaticDataFetcher) Arrays(java.util.Arrays) GraphQL(graphql.GraphQL) Entry(org.nextprot.api.core.domain.Entry) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) Autowired(org.springframework.beans.factory.annotation.Autowired) GraphQLQueryResolver(com.coxautodev.graphql.tools.GraphQLQueryResolver) ExecutionResult(graphql.ExecutionResult) Component(org.springframework.stereotype.Component) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaParser(graphql.schema.idl.SchemaParser) GraphQLSchema(graphql.schema.GraphQLSchema) SchemaGenerator(graphql.schema.idl.SchemaGenerator) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) RuntimeWiring(graphql.schema.idl.RuntimeWiring) 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 12 with GraphQL

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

the class GraphQlExecutorImpl method postConstruct.

@PostConstruct
private void postConstruct() {
    SchemaParser schemaParser = new SchemaParser();
    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("swapi.graphqls").getFile());
    TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(file);
    RuntimeWiring runtimeWiring = newRuntimeWiring().type("Query", builder -> builder.dataFetcher("entry", entryDataFetcher)).build();
    SchemaGenerator schemaGenerator = new SchemaGenerator();
    GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
    graphQL = GraphQL.newGraphQL(graphQLSchema).build();
}
Also used : GraphQL(graphql.GraphQL) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) HashMap(java.util.HashMap) File(java.io.File) ExecutionResult(graphql.ExecutionResult) LinkedHashMap(java.util.LinkedHashMap) Component(org.springframework.stereotype.Component) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaParser(graphql.schema.idl.SchemaParser) Map(java.util.Map) PostConstruct(javax.annotation.PostConstruct) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLException(graphql.GraphQLException) SchemaGenerator(graphql.schema.idl.SchemaGenerator) TypeReference(com.fasterxml.jackson.core.type.TypeReference) GraphQlExecutor(org.nextprot.api.web.service.GraphQlExecutor) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) RuntimeWiring(graphql.schema.idl.RuntimeWiring) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) SchemaGenerator(graphql.schema.idl.SchemaGenerator) SchemaParser(graphql.schema.idl.SchemaParser) File(java.io.File) GraphQLSchema(graphql.schema.GraphQLSchema) PostConstruct(javax.annotation.PostConstruct)

Example 13 with GraphQL

use of graphql.GraphQL in project engine by craftercms.

the class SiteContext method buildGraphQLSchema.

protected void buildGraphQLSchema() {
    logger.info("Starting GraphQL schema build for site '{}'", siteName);
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    publishEvent(new GraphQLBuildStartedEvent(this));
    try {
        GraphQL graphQL = graphQLFactory.getInstance(this);
        if (Objects.nonNull(graphQL)) {
            this.graphQL = graphQL;
            publishEvent(new GraphQLBuildCompletedEvent(this));
        }
    } catch (Exception e) {
        logger.error("Error building the GraphQL schema for site '" + siteName + "'", e);
    }
    stopWatch.stop();
    logger.info("GraphQL schema build completed for site '{}' in {} secs", siteName, stopWatch.getTime(TimeUnit.SECONDS));
}
Also used : GraphQL(graphql.GraphQL) SchedulerException(org.quartz.SchedulerException) CrafterException(org.craftercms.core.exception.CrafterException) SiteContextInitializationException(org.craftercms.engine.exception.SiteContextInitializationException) GraphQLBuildException(org.craftercms.engine.exception.GraphQLBuildException) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 14 with GraphQL

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

Example 15 with GraphQL

use of graphql.GraphQL in project vertx-examples by vert-x3.

the class Server method createGraphQL.

private GraphQL createGraphQL() {
    String schema = vertx.fileSystem().readFileBlocking("links.graphqls").toString();
    SchemaParser schemaParser = new SchemaParser();
    TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);
    RuntimeWiring runtimeWiring = newRuntimeWiring().type("Query", builder -> {
        VertxDataFetcher<List<Link>> getAllLinks = new VertxDataFetcher<>(this::getAllLinks);
        return builder.dataFetcher("allLinks", getAllLinks);
    }).build();
    SchemaGenerator schemaGenerator = new SchemaGenerator();
    GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
    return GraphQL.newGraphQL(graphQLSchema).build();
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) GraphQL(graphql.GraphQL) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) Promise(io.vertx.core.Promise) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) Router(io.vertx.ext.web.Router) ArrayList(java.util.ArrayList) GraphQLHandler(io.vertx.ext.web.handler.graphql.GraphQLHandler) Launcher(io.vertx.core.Launcher) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaParser(graphql.schema.idl.SchemaParser) VertxDataFetcher(io.vertx.ext.web.handler.graphql.VertxDataFetcher) AbstractVerticle(io.vertx.core.AbstractVerticle) GraphQLSchema(graphql.schema.GraphQLSchema) SchemaGenerator(graphql.schema.idl.SchemaGenerator) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) RuntimeWiring(graphql.schema.idl.RuntimeWiring) VertxDataFetcher(io.vertx.ext.web.handler.graphql.VertxDataFetcher) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) SchemaGenerator(graphql.schema.idl.SchemaGenerator) SchemaParser(graphql.schema.idl.SchemaParser) GraphQLSchema(graphql.schema.GraphQLSchema)

Aggregations

GraphQL (graphql.GraphQL)25 ExecutionResult (graphql.ExecutionResult)18 GraphQLSchema (graphql.schema.GraphQLSchema)13 ExecutionInput (graphql.ExecutionInput)6 RuntimeWiring (graphql.schema.idl.RuntimeWiring)5 RuntimeWiring.newRuntimeWiring (graphql.schema.idl.RuntimeWiring.newRuntimeWiring)5 SchemaGenerator (graphql.schema.idl.SchemaGenerator)5 SchemaParser (graphql.schema.idl.SchemaParser)5 TypeDefinitionRegistry (graphql.schema.idl.TypeDefinitionRegistry)5 GraphQLError (graphql.GraphQLError)4 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)4 List (java.util.List)4 DataLoaderRegistry (org.dataloader.DataLoaderRegistry)4 DataLoaderDispatcherInstrumentation (graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ExecutionInput.newExecutionInput (graphql.ExecutionInput.newExecutionInput)2 ExecutorServiceExecutionStrategy (graphql.execution.ExecutorServiceExecutionStrategy)2