Spring-boot Integrated GRAPHQL Introductory Tutorial _GRAPHQL

Source: Internet
Author: User

https://my.oschina.net/genghz/blog/1789240


Absrtact: This is a Spring-boot integration graphql Introductory course, only for reference study, first write, please forgive me

This article describes a Spring-boot + graphql that is a graphql Java starter Project GRAPHQL what exactly is

GRAPHQL is an API query language for server-side execution of queries by defined type systems. GRAPHQL is not bound to any particular database or storage engine, but is supported by your code and data. (Official description)

Plainly is what you want, pass in what field, also will return what field, the specific field processing is provided by the server, and GRAPHQL does not care how the server how to deal with

For example:

Traditional REST API:/test/user/{id} return {ID, name, age} is immutable,

Graphql:findoneuser (ID:XX) return {ID, name} (Note: Transport parameter ID, specify returned field ID, name, of course, can also write {name, age}, depending entirely on front-end requirements) GRAPHQL Advantage

GRAPHQL greatly reduce the cost of communication, avoid each time the definition of the number of API fields, the front of their own choice, combination of the desired fields, generate data, GRAPHQL provides a convenient GUI can easily test, write GRAPHQL statements, view the services provided by Doc, for more information see HTTP s://graphql.cn/Project Construction

Spring-boot is based on 2.0.0 version, want to understand Spring-boot Https://gitee.com/geng_hz/Spring-Boot-Reference-Guide

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId> spring-boot-starter-parent</artifactid>
    <version>2.0.0.RELEASE</version>
</parent >

The great gods encapsulate the spring-boot dependence

<!--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>

The Directory of the project (a random packet will end up with a link code):

Root.graphqls is the GRAPHQL service entry 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!
}

Scheme.graphqls is the Query/mutation specific scheme definition field, type

Type Author {
    id:long!
    Createdtime:string
    firstname:string
    lastname:string Books
    : [Book]
}
input Bookinput {
    title:string!
    isbn:string!
    Pagecount:int
    authorid:long
}
type book {
    id:long!
    title:string!
    isbn:string!
    Pagecount:int
    Author:author
}

Query is an entry, mutation is a modified entry

Example: Findoneauthor passes in a long ID, returns a Author schema,

GRAPHQL entry definition, but this is just a description, we need to implement the description in Query/mutation

For example:

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 ();
    }
}

Implements all of the descriptions in query (all must be implemented)

Schema like the one in my directory authorresolver This is the implementation of the description in the schema

Note: query/mutation, like ordinary schemas, are just the portals of GRAPHQL services, resolver implementation description follows:

1. Method <name> (*args)

2.method is<name> (*args) only supports return Boolean

3.method get<name> (*args)

4.method getfield<name> (*args)

This is an implementation that takes precedence when providing resolver, followed by the class this.get<name> method

The Createdtime in Author.class is Date, but schema Author {createdtime:string}, so the Authorresovler generation Createdtime String is provided separately, and the His parameters are consistent with the schema author type, and using the Get method in author is sufficient

@Component
@AllArgsConstructor Public
class Authorresolver implements graphqlresolver<author> {

    Private static final SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
    Private Bookrepository bookrepository;

    Public String getcreatedtime (Author Author) {return
        Sdf.format (Author.getcreatedtime ());
    }
    Public list<book> getbooks (Author Author) {return
        Bookrepository.findbyauthorid (Author.getid ());
    }

Execute script:./run.sh startup Project, graphql default endpoint:/GRAPHQL


The graphql GUI, handy for writing test graphql and viewing current services provides the available methods, like this:

Demo code, GUI to serve:

Https://gitee.com/geng_hz/spring-boot-graphql

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.