graphqlize 灵活方便的jvm graphql library试用

graphqlize 官方文档已经很齐全了,以下是简单的试用(spring boot 项目)

项目使用mven 管理,同时包含了playgroup 以及voyager

基础环境准备

主要是pg 使用docker-compose 运行

  • docker-compose 文件
version: "3"
services:
   pg:
      image: postgres:11-alpine
      ports:
      - "5432:5432"
      environment:
      - "POSTGRES_PASSWORD=dalong"
  • 启动&&初始化数据
docker-compose up -d

导入sql-schema 目录下的 postgres-sakila-schema.sql 以及postgres-sakila-insert-data.sql

代码集成

因为graphqlize就是一个library,可以直接引用,按照要求添加配置即可

  • 项目结构
├── docker-compose.yaml
├── pom.xml
├── sql-schema
└── postgres-sakila-db
├── postgres-sakila-delete-data.sql
├── postgres-sakila-drop-objects.sql
├── postgres-sakila-insert-data.sql
└── postgres-sakila-schema.sql
└── src
    ├── main
    ├── java
    └── com
    └── example
    └── demo
    ├── DemoApplication.java
    ├── GraphQLController.java
    └── GraphQLizeResolverProvider.java
    └── resources
    ├── application.properties
    └── static
    ├── playground.html
    └── voyager.html
 
  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.2.5.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example</groupId>
 <artifactId>demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>demo</name>
 <description>Demo project for Spring Boot</description>
 <properties>
  <java.version>1.8</java.version>
 </properties>
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
   <groupId>org.graphqlize</groupId>
   <artifactId>graphqlize-java</artifactId>
   <version>0.1.0-alpha4</version>
  </dependency>
  <dependency>
   <groupId>org.postgresql</groupId>
   <artifactId>postgresql</artifactId>
   <version>42.2.10</version>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
   <exclusions>
    <exclusion>
     <groupId>org.junit.vintage</groupId>
     <artifactId>junit-vintage-engine</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>
  • 代码说明
    数据源配置 application.properties
 
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=dalong

graphqlize provider 配置 GraphQLizeResolverProvider.java

package com.example.demo;
import org.graphqlize.java.GraphQLizeResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.util.Map;
@Component
public class GraphQLizeResolverProvider {
    private final DataSource dataSource;
    private final GraphQLizeResolver graphQLizeResolver;
    public GraphQLizeResolverProvider(DataSource dataSource) {
        this.dataSource = dataSource;
        graphQLizeResolver = new GraphQLizeResolver(dataSource);
    }
    @Bean
    public GraphQLizeResolver graphQLizeResolver() {
        return this.graphQLizeResolver;
    }
}

graphql api endpoint 配置


package com.example.demo;
import org.graphqlize.java.GraphQLResolver;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
class GraphQLRequest {
    private String query;
    public String getQuery() {
        return query;
    }
    public void setQuery(String query) {
        this.query = query;
    }
    public Map<String, Object> getVariables() {
        return variables;
    }
    public void setVariables(Map<String, Object> variables) {
        this.variables = variables;
    }
    private Map<String, Object> variables;
}
@RestController
public class GraphQLController {
    private final GraphQLResolver graphQLResolver;
    public GraphQLController(GraphQLResolver graphQLResolver) {
        this.graphQLResolver = graphQLResolver;
    }
    @PostMapping("/graphql")
    public ResponseEntity handle(@RequestBody GraphQLRequest graphQLRequest) {
        String result =
                graphQLResolver.resolve(
                        graphQLRequest.getQuery(),
                        graphQLRequest.getVariables());
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_TYPE, "application/json")
                .body(result);
    }
}

启动&&使用

  • 启动
mvn spring-boot:run
  • playgroud 效果

voyager 效果

说明

graphqlize 是一个很值得使用的类似hasura 的graphql 工具

参考资料

https://www.graphqlize.org/docs/getting_started/java/springboot
https://github.com/rongfengliang/graphqlize-docker-compose
https://github.com/graphqlize/graphqlize

原文地址:https://www.cnblogs.com/rongfengliang/p/12523958.html