Skip to content

一个为 GraphQl 的 开发的spring boot 扩展, 相比目前的官方版本, 该版本主要提供自定义注解, 将方法自动转换为 DataFetcher 实现代码简化和逻辑复用.

Notifications You must be signed in to change notification settings

cokebook/graphql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL : GraphQL Spring Plugin

本项目是 GraphQL 的 spring 扩展应用, 其旨在为常规的 spring mvc OR  spring boot 项目提供快捷的 GraphQL 支持.

1 使用说明

  • 1 引入 graphql-spring 依赖

    本项目暂时没有MVN 中央仓库JAR, 你可以克隆本项目在本地运行打包!

  • 定义你的 graphql schema

    将 graphql-example/resources/schema.graphql 样例文件

  • 2 声明 Bean 的方法为TypeWiring :

@Service
public class BookService {

    @Autowired
    private AuthorService authorService;

    private static List<Book> books = Arrays.asList(
            new Book("book-1", "Harry Potter and the Philosopher's Stone", "223", "author-1"),
            new Book("book-2", "Roma", "30", "author-2"),
            new Book("book-3", "TLP", "10", "author-2"),
            new Book("book-4", "BUSHI", "12", "author-3")
    );
    // 作为查询的简写方案可以直接使用  @Query("book") 替代
    @TypeWiring(field = "book")
    public Book findById(@Param("id") String id) {
        return books.stream().filter(book -> book.getId().equals(id)).findFirst().orElse(null);
    }

    @TypeWiring(type = "Book", field = "author")
    public Author findByBook(Book book) {
        return authorService.findAuthorById(book.getAuthorId());
    }
}
  • 4 启动应用, 使用命令查询 :

1 使用通用风格查询模式

curl -L -X GET 'http://localhost:8080/graphql' \
-H 'Content-Type: application/json' \
--data-raw '{"query":"query test($id:ID) {\n  \n  book(id:$id) { name, id} \n\n}","variables":{"id":"2","index":1}}'

2 使用 Url 参数模式查询

curl -L -X GET 'http://localhost:8080/graphql/query?q=query%20test($id:ID)%20%7B%20book(id:$id)%20%7B%20name,%20id%7D%20%7D&id=2'
  • 5 结果演示
    {
        "code": 0,
        "message": null,
        "data": {
            "book": {
                "name": "Roma",
                "id": "2"
            }
        }
    }

2 Example

  • 你可以直接下载本项目运行 graphql-example 模块演示系统结果作为学习素材. 点击跳转到样例

  • 如果你想使用非 @TypeWiring 以及@Query / @Mutation 快捷注解声明 DataFetcher, 你可以使用常规 Spring bean 模式定义 DataFetcher 点击跳转到样例

3 参考文档

4 特别说明

  • 由于 spring-boot 1.x 和 2.x 版本之间部分接口被调整, 而当前项目基于 1.x 版本构建, 所有如果你想支持 2.x 版本 则你需要调整一些 GraphQlRunListener 类. 已支持
  • 考虑我使用的场景 目前主要集中开发针对查询的场景, 后续会考虑支持变更场景 已支持

About

一个为 GraphQl 的 开发的spring boot 扩展, 相比目前的官方版本, 该版本主要提供自定义注解, 将方法自动转换为 DataFetcher 实现代码简化和逻辑复用.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages