Contents

Overview

The Helidon GraphQL Server provides a framework for creating GraphQL applications that integrate with the Helidon WebServer. GraphQL is a query language to access server data. The Helidon GraphQL integration enables HTTP clients to issue queries over the network and retrieve data; it is an alternative to other protocols such as REST or GRPC.

Maven Coordinates

To enable GraphQL add the following dependency to your project’s pom.xml (see Managing Dependencies).

<dependency>
    <groupId>io.helidon.graphql</groupId>
    <artifactId>helidon-graphql-server</artifactId>
</dependency>
Copied

API

An instance of GraphQlSupport must be registered in the Helidon WebServer routes to enable GraphQL support in your application. In addition, a GraphQL schema needs to be specified to verify and execute queries.

The following code fragment creates an instance of GraphQlSupport and registers it in the Helidon WebServer.

    WebServer server = WebServer.builder()
            .routing(Routing.builder()
                            .register(GraphQlSupport.create(buildSchema()))
                            .build())
            .build();
Copied

By default, GraphQlSupport will reserve /graphql as the URI path to process queries. The buildSchema method creates the schema and defines 2 types of queries for this application:

private static GraphQLSchema buildSchema() {
    String schema = "type Query{\n"    
            + "hello: String \n"
            + "helloInDifferentLanguages: [String] \n"
            + "\n}";

    SchemaParser schemaParser = new SchemaParser();
    TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);

    // DataFetcher to return various hellos in difference languages  
    DataFetcher<List<String>> hellosDataFetcher = (DataFetcher<List<String>>) environment ->
            List.of("Bonjour", "Hola", "Zdravstvuyte", "Nǐn hǎo", "Salve", "Gudday",
                    "Konnichiwa", "Guten Tag");

    RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()  
            .type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world")))
            .type("Query", builder -> builder.dataFetcher("helloInDifferentLanguages", hellosDataFetcher))
            .build();

    SchemaGenerator schemaGenerator = new SchemaGenerator();
    return schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);  
}
Copied

The following is a description of each of these steps:

  • Define the GraphQL schema.
  • Create a DataFetcher to return a list of hellos in different languages.
  • Wire up the DataFetcher s.
  • Generate the GraphQL schema.

Configuration

The following configuration keys can be used to set up integration with WebServer:

keydefault valuedescription
graphql.web-context/graphqlContext that serves the GraphQL endpoint.
graphql.schema-uri/schema.graphqlURI that serves the schema (under web context)
graphql.cors CORS configuration for this service
graphql.executor-service Configuration of ServerThreadPoolSupplier used to set up executor service

The following configuration keys can be used to set up GraphQL invocation:

keydefault valuedescription
graphql.default-error-messageServer ErrorError message to send to caller in case of error
graphql.exception-white-list Array of checked exception classes that should return default error message
graphql.exception-black-list Array of unchecked exception classes that should return message to caller (instead of default error message)

Examples

Using the schema defined in Section , you can probe the following endpoints:

  1. Hello world endpoint
    curl -X POST http://127.0.0.1:PORT/graphql -d '{"query":"query { hello }"}'
    
    "data":{"hello":"world"}}
    Copied
  2. Hello in different languages
    curl -X POST http://127.0.0.1:PORT/graphql -d '{"query":"query { helloInDifferentLanguages }"}'
    
    {"data":{"helloInDifferentLanguages":["Bonjour","Hola","Zdravstvuyte","Nǐn hǎo","Salve","Gudday","Konnichiwa","Guten Tag"]}}
    Copied

Additional Information