GraphQL Server Introduction

Helidon GraphQL Server provides a framework for creating GraphQL applications.

Experimental

The Helidon GraphQL feature is currently experimental and the APIs are subject to changes until GraphQL support is stabilized.

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

Quick Start

Here is the code for a minimalist GraphQL application that exposes 2 queries.

    public static void main(String[] args) {
        WebServer server = WebServer.builder()
                .routing(Routing.builder()
                                 .register(GraphQlSupport.create(buildSchema()))  
                                 .build())
                .build();

        server.start()  
               .thenApply(webServer -> {
                   String endpoint = "http://localhost:" + webServer.port();
                   System.out.println("GraphQL started on " + endpoint + "/graphql");
                   System.out.println("GraphQL schema availanle on " + endpoint + "/graphql/schema.graphql");
                   return null;
               });
    }

    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 hello's 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
  • Register GraphQL support.
  • Start the server.
  • Define the GraphQL schema.
  • Create a DataFetcher to return a List of Hellos in different languages.
  • Wire up the DataFetchers.
  • Generate the GraphQL schema.

The example above deploys a very simple service exposing the /graphql endpoint.

You can then probe the endpoints:

  1. Hello word 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