Skip to content

pukkaone/grapid

Repository files navigation

Grapid  Maven Central

Grapid is an opinionated, schema-first framework for implementing GraphQL servers in Java. While the framework prescribes where you put GraphQL schema definition files and Java classes, you only need to implement the business logic for your API. The framework generates the code to wire GraphQL requests to your business logic.

GraphQL Server Quick Start Guide

Add this Spring Boot starter which auto-configures a GraphQL server accepting requests by HTTP. By default, the server URL path is /graphql relative to the context path.

<dependency>
  <groupId>com.github.pukkaone</groupId>
  <artifactId>grapid-web-spring-boot-starter</artifactId>
  <version>${grapid.version}</version>
</dependency>

Add this Maven plugin which compiles GraphQL schema definition files to Java source files.

<plugin>
  <groupId>com.github.pukkaone</groupId>
  <artifactId>grapid-maven-plugin</artifactId>
  <version>${grapid.version}</version>
  <configuration>
    <!-- The compiler generates Java classes under this Java package. -->
    <packagePrefix>com.example.graphql</packagePrefix>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>compile</goal>
      </goals>
    </execution>
  </executions>
</plugin>

An API version represents a set of types and operations defined by a GraphQL schema. A version identifier must be a valid Java identifier and not a Java keyword.

By convention, GraphQL schema definition files are located under a resources directory src/main/resources/graphql/version/ where version identifies an API version. Create the resources directory src/main/resources/graphql/v2018_12_31/. Add this GraphQL schema definition file in the directory. By convention, GraphQL schema definition file names end with the extension .graphqls.

Author.graphqls
type Author {
  id: ID!
  name: String!
}

type Query {
  author(id: ID!): Author
}

The GraphQL schema defines the root object type Query. The compiler appends the suffix Resolver to this root object type name to derive the Java class name QueryResolver. As an application developer, you must implement the QueryResolver class. By convention, this class is in the Java package named packagePrefix.resolver.

QueryResolver.java
package com.example.graphql.resolver;

import com.example.graphql.v2018_12_31.type.Author; // (1)
import org.springframework.stereotype.Component;

@Component
public class QueryResolver {

  public Author author(String id) { // (2)
    return Author.builder()
        .id(id)
        .name("NAME")
        .build();
  }
}
  1. The compiler generated the simple Java data class Author from the GraphQL object type Author.

  2. The compiler translated this Java method signature from the field author of the GraphQL root object type Query.

Run the application. In GraphQL Playground, connect to http://localhost:8080/graphql/v2018_12_31 to send a GraphQL query to the server.

For more details, see the Getting Started Guide.

Interface Stability

Major version zero (0.x.x) is for initial development. Anything may change at any time. The public API should not be considered stable.

Building

Prerequisites are:

  • Java 17

  • Gradle

To compile the project and run integration tests:

gradle publishToMavenLocal check