BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Spring for GraphQL Version 1.0 Released

Spring for GraphQL Version 1.0 Released

This item in japanese

Lire ce contenu en français

Bookmarks

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

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • 2-3 years late...

    by Guy Katz,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I was a spring advocate in its early years. today, its just another 'old gen' framework which is pretty similar to my long beloved Jakarta EE.
    if anyone is looking to start a new project they should go with the 'next gen' frameworks such as helidon, micronaut & quarkus or even 'naked' toolkits like vert.x
    specifically for gql: micro profile gql has been out for 2.5 years already. personally I have been using graphql-java for the past 3.5 years. so spring now is the follower and not the leader any more...

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT