Spring Boot + GraphQL创建API的开源案例

18-12-10 banq
         

这是用于处理作者/书籍数据的API,可以根据作者Id获取者信息和书籍信息,数据库:

作者:

CREATE TABLE `author` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `author_idx1` (`id`)
)

书籍表:

CREATE TABLE `book` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL,
  `author_id` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`author_id`) REFERENCES author(`id`),
  KEY `book_idx1` (`id`)
)

Schema定义:

type Author {
    id: ID!
    name: String!
    books: [Book]
}

type Book {
    id: ID!
    name: String!
}

type RegistAuthorResponse {
    name: String!,
    isRegist: Boolean!,
    errorMessage: String
}

type RegistBookResponse {
    authorId: ID!,
    name: String!,
    isRegist: Boolean!,
    errorMessage: String
}

解析器Resolver:

Resolver由GraphQL Java Tool提供。