Spring GraphQL 1.0.0-M4 Released

Releases | Brian Clozel | December 14, 2021 | ...

The Spring GraphQL team has just released the 4th milestone towards a 1.0.0 release. Thanks to all contributors!

In this milestone, we have further improved the annotation programming model and extended the Spring Data support that were provided in the previous milestones.

Interface Projections for GraphQL Arguments

If you're familiar with Spring Data's Interface-based Projections, then this new feature will make perfect sense: you can use a well-defined interface to work with GraphQL arguments, without the need for any Object implementation.

For example:

@Controller
public class BookController {

    @MutationMapping
    public Book addBook(@Argument BookInputProjection bookInput) {
        // ...
    }
}

@ProjectedPayload
interface BookInputProjection {

    String getName();  // this maps to "name" argument

    @Value("#{target.author + ' ' + target.name})
    String getAuthorAndName();
}

You can learn more about this in the @ProjectedPayload Interface section of the documentation.

Query By Example Support

Spring GraphQL now supports use of the Query by Example, Spring Data, extension for data fetching. To use this extension, you don't need to write queries through store-specific query languages, but simply provide an example of a domain object with populated fields. The repository implementation will do the rest!

The QueryByExampleDataFetcher makes it easy to create a DataFetcher that binds GraphQL arguments onto an example object and then uses that to query for the data. There is also a feature for automated discovery of Query By Example repositories, based on the @GraphQlRepository annotation, and mapping to top-level GraphQL queries.

If your data store of choice supports it, this adds another way to leverage your Spring Data repositories in a Spring GraphQL application. See the documentation for more details.

Standard Bean Validation for GraphQL Arguments

On top of the existing constraints enforced at the schema level, you can now use Standard Bean Validation to validate GraphQL arguments that are declared as controller method parameters. If the argument doesn't match the defined constraints, a validation exception is thrown and written to the relevant "errors" section of the GraphQL response.

@Controller
public class BookController {

    @MutationMapping
    public Book addBook(@Argument @Valid BookInput bookInput) {
        // ...
    }
}

public class BookInput {

    @NotNull
    private String title;

    @NotNull
    @Size(max=13)
    private String isbn;
}

This is similar to the existing support in Spring MVC or Spring WebFlux, you can learn more about @Argument validation in the reference docs.

Context Access

A new @ContextValue annotation provides convenient access to values in the GraphQLContext from controller methods.

Controller methods can access the authenticated user by declaring a method argument of type javax.security.Principal.

@BatchMapping methods can access the same GraphQLContext that is also available for @SchemaMapping methods and to any DataFetcher.

More

More improvements and fixes made their way into their release and they're available right now in the new Spring GraphQL 1.0.0-M4 release from the Spring Milestone repository.

How can you help?

If you're interested in helping out, you can get started with Spring GraphQL and raise issues on our GitHub project. If you have general questions, please ask on stackoverflow.com using the spring-graphql tag.

Project Page | GitHub | Issues | Documentation | Stack Overflow

Get the Spring newsletter

Thank you for your interest. Someone will get back to you shortly.

Get ahead

VMware offers training and certification to turbo-charge your progress.

Learn more

Get support

Tanzu Spring Runtime offers support and binaries for OpenJDK™, Spring, and Apache Tomcat® in one simple subscription.

Learn more

Upcoming events

Check out all the upcoming events in the Spring community.

View all