Skip to content

Latest commit

 

History

History
115 lines (91 loc) · 2.04 KB

dev-guide.adoc

File metadata and controls

115 lines (91 loc) · 2.04 KB

Development Guide

Table of Contents

Abstract

Some tips about Java GraphQL development using Spring-Boot.

Queries

In order to get graphql queries to work a root query must be defined in resource schema.graphql. Then a Resolver instance for that request must be proportioned.

public interface Resolver {
    public String getTypeName();
    public String getFieldName();
    public DataFetcher<?> getResolver();
}

For example, with the following schema:

type Query {
    bookById(id: ID): Book
}

type Book {
    id: ID!
    name: String
    pageCount: Int
    author: Author
}

type Author {
    id: ID
    firstName: String
    lastName: String
}

And defining its Resolver:

public class Resolvers implements Resolver {

    @Override
    public String getTypeName() {
        return "Query";
    }

    @Override
    public String getFieldName() {
        return "bookById";
    }

    @Override
    public DataFetcher<Future<Book>> getResolver() {
        return dataFetchingEnvironment -> {
            Author author = new Author();
            author.setId("author-1");
            author.setFirstName("John");
            author.setLastName("Foo");
            Book book = new Book();
            book.setId(bookId);
            book.setName("GraphQL song");
            book.setPageCount(666);
            book.setAuthor(author);

            return Mono.just(book).toFuture();
        };
    }
}

Finally expose the model, and lombok may remove boilerplate.

@Data
    public static class Author {
        @Getter
        @Setter
        String id;

        @Getter
        @Setter
        String firstName;

        @Getter
        @Setter
        String lastName;
    }

    @Data
    public static class Book {
        @Getter
        @Setter
        String id;

        @Getter
        @Setter
        String name;

        @Getter
        @Setter
        Integer pageCount;

        @Getter
        @Setter
        Author author;
    }

Get it up and running. Enjoy it!