Getting started with Graphql

Posted by xdentan on Mon, 18 Oct 2021 21:40:21 +0200

preparation:

1. Import the open source demo of springboot integrated graphql spring-boot-graphql: spring-boot-graphql demo

2. Download postman

Next, you can learn graphql

The first step is to change the data source in springboot into its own test database

Step 2 pay attention to the package framed in the figure below

  First, how to integrate graphql in demo

1.pom

<!-- graphql -->
<dependency>
    <groupId>com.graphql-java</groupId>
    <artifactId>graphql-spring-boot-starter</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>com.graphql-java</groupId>
    <artifactId>graphql-java-tools</artifactId>
    <version>4.3.0</version>
</dependency>

2. Here are two documents

(1) schema.grapqls corresponds to the vo of the front end

  (2)root.graphqls defines the query and mutation objects, which contain the methods provided and called by the API

Therefore, root.grapqls corresponds to the methods in Query and Mutation defined by the view parser

definition:
type Query {
    findAllAuthors: [Author]!
    countAuthors: Long!
    findOneAuthor(id: Long!): Author

    findAllBooks: [Book]!
    countBooks: Long!
}

type Mutation {
    newAuthor(firstName: String!, lastName: String!) : Author!

    newBook(title: String!, isbn: String!, pageCount: Int, authorId: Long!) : Book!
    saveBook(input: BookInput!): Book!
    deleteBook(id: ID!) : Boolean
    updateBookPageCount(pageCount: Int!, id: Long!) : Book!
}

Mapping method:

public class Query implements GraphQLQueryResolver {

    private AuthorRepository authorRepository;

    private BookRepository bookRepository;

    public Author findOneAuthor(Long id) {
        Optional<Author> opt = authorRepository.findById(id);
        return opt.isPresent() ? opt.get() : null;
    }

    public List<Author> findAllAuthors() {
        return authorRepository.findAll();
    }

    public Long countAuthors() {
        return authorRepository.count();
    }

    public List<Book> findAllBooks() {
        return bookRepository.findAll();
    }

    public Long countBooks() {
        return bookRepository.count();
    }
}


public class Mutation implements GraphQLMutationResolver {

    private AuthorRepository authorRepository;

    private BookRepository bookRepository;


    public Author newAuthor(String firstName, String lastName) {
        Author author = new Author();
        author.setFirstName(firstName);
        author.setLastName(lastName);
        return authorRepository.save(author);
    }

    public Book newBook(String title, String isbn, int pageCount, long authorId) {
        Book book = new Book();
        book.setTitle(title);
        book.setIsbn(isbn);
        book.setPageCount(pageCount);
        book.setAuthorId(authorId);
        return bookRepository.save(book);
    }

    public Book saveBook(BookInput input) {
        Book book = new Book();
        book.setTitle(input.getTitle());
        book.setIsbn(input.getIsbn());
        book.setPageCount(input.getPageCount());
        book.setAuthorId(input.getAuthorId());
        return bookRepository.save(book);
    }

    public Boolean deleteBook(long id) {
        bookRepository.deleteById(id);
        return true;
    }

    public Book updateBookPageCount(int pageCount, long id) {
        Optional<Book> opt = bookRepository.findById(id);
        if (!opt.isPresent()) return null;

        Book book = opt.get();
        book.setPageCount(pageCount);
        return bookRepository.save(book);
    }
}




The attribute types contained in a single object are all non custom object types. If a single custom object also contains complex custom objects, a corresponding parser is required [mainly depends on whether schema.graphqls defines such objects]

As can be seen from the figure, the BOOK contains Author, and there is BOOK in Author

So you also need the corresponding parser

 

  As for the method name rule, a single object getX and multiple List objects correspond to getXs

The following is used with postman [postman integrates graphql]

 

This should be regarded as an introduction. As for mastery, I personally don't think it's necessary, because I think Graphql is not easy to use in the company. It's customized. The front and rear ends must be integrated before they can be used. Moreover, the communication data type is wonderful. The outer layer is a large JSON and the inner surface is a string. It's not a pure JSON data [you can see it yourself with packet capture], This impure communication coding protocol is still difficult to be universal in the industry. At the beginning, it was very new. After understanding it, I really want to say that the garbage framework is actually a view parsing, filtering and method binding framework, which is not universal (both front and rear ends have to be integrated)

Topics: Java Spring Spring Boot