Spring for GraphQL Version 1.0 Released

MMS Founder
MMS Andrea Messetti

Article originally posted on InfoQ. Visit InfoQ

Less than two years after its inception, Spring for GraphQL 1.0 has been released.
The project integrates Spring and GraphQL Java and was developed in collaboration between both teams.

GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data. It was developed by Facebook and publicly released in 2015.

GraphQL allows clients to define the structure of the required data, and the same structure of the data is returned from the server, therefore preventing excessively large amounts of data from being returned.

Spring for GraphQL can be configured using Spring Initalizr by simply adding the dependency graphql-spring-boot-starter in the recently released Spring Boot 2.7. InfoQ will follow up with a more detailed news story on Spring Boot 2.7.0.

Spring for GraphQL supports handling of GraphQL requests over HTTP, WebSocket, and RSocket both on the client and server sides.

Spring for GraphQL simplifies the development of a GraphQL project since: it enables auto-loading of schema files from a configurable location; provides an annotation-based programming model; registers a DataFetcherExceptionHandler to manage the exceptions; and configures metrics for GraphQL.

In the example shown below, Spring binds the method to a query and uses the RuntimeWiring.Builder class to register the above handler method as a DataFetcher for the query named “hello.”

@Controller
public class GreetingController {

        @QueryMapping 
        public String hello() { 
            return "Hello, world!";
        }

}

It is also possible to specify the parent type name and field name by simply using @SchemaMapping(typeName=”Book”, field=”author”) as in the example shown below:

@Controller
public class BookController {

    @SchemaMapping(typeName="Book", field="author")
    public Author getAuthor(Book book) {
        // ...
    }
}

Batch Loading is supported through the use of a DataLoader to defer the loading of individual entity instances.

Spring for GraphQL also leverages existing Spring technology to expose underlying data sources through GraphQL. It supports the use of Querydsl and Query by Example repositories to recover data to a DataFetcher.

Using the code shown below, it is possible to create a DataFetcher that can be registered through a RuntimeWiringConfigurer.

// For single result queries
DataFetcher<Account> dataFetcher = QuerydslDataFetcher.builder(repository).single();

// For multi-result queries
DataFetcher<Iterable<Account>> dataFetcher = QuerydslDataFetcher.builder(repository).many();

The DataFetcher builds a Querydsl Predicate from GraphQL request parameters and uses it to fetch data.

Adding Spring Security annotations, such as @PreAuthorize or @Secured, it is possible to apply fine-grained security by checking the authorization for methods retrieving data for specific parts of GraphQL response.

On the client-side, Interception and Subscription requests are supported. Currently, only the WebSocket transport supports GraphQL streams.

Still in its infancy, Spring for GraphQL will most likely evolve with new features and bug fixes, and could become an alternative to the popular DGS framework, developed at Netflix in the last three years.

About the Author

Subscribe for MMS Newsletter

By signing up, you will receive updates about our latest information.

  • This field is for validation purposes and should be left unchanged.