我试着使用GraphQL+SpringDataJDBC

我尝试了GraphQL

请注意,此文章是基于2020年9月的信息。

0. 参照

    初めてのGraphQL ―Webサービスを作って学ぶ新世代API
    Spring Boot + GraphQLでAPIを作成してみよう!

1. 什么是GraphQL?

    • 数学のグラフ理論に基づく(グラフ理論:一筆書き問題などの学問)

 

    • Web APIのためのクエリ言語

 

    • 様々な言語で実装可能

 

    • RESTの課題を解決しようとした

 

    Facebookが2015年に公開

在Instagram上的形象

image.png

在此之前,与REST API进行比较。

    スターウォーズAPI(REST版)
curl https://swapi.dev/api/people/1/

回应

{
    "name":  "Luke Skywalker",
    "height":  "172",
    "mass":  "77",
    "hair_color":  "blond",
~省略~
    "gender":  "male",
    "homeworld":  "http://swapi.dev/api/planets/1/",
    "films":  [
                  "http://swapi.dev/api/films/1/",
                  "http://swapi.dev/api/films/2/",
                  "http://swapi.dev/api/films/3/",
                  "http://swapi.dev/api/films/6/"
              ],
~省略~
    "url":  "http://swapi.dev/api/people/1/"
}

    スターウォーズAPI(GraphQL版)
curl https://swapi.apis.guru/ -ContentType application/json -Body '{"query":"{person(personID:1){name,gender,filmConnection{films{title}}}}"}' 

只需整理查询部分,这就是GraphQL的查询!

{query:
  {person(personID:1)
    {name,
      gender,
      filmConnection
        {films
          {title}
        }
     }
   }
}

回应

{
  "data":  {
     "person":  {
      "name":  "Luke Skywalker",
      “gender”: “male”,
      "filmConnection":  {
           "films":  [
             {
             "title":  "A New Hope"
             },
             {
             "title":  "The Empire Strikes Back"
             },
             {
             "title":  "Return of the Jedi"
             },
             {
             "title":  "Revenge of the Sith"
             }
             ]
           }
      }
   }
}

当比较REST和GraphQL时

    • RESTは多様なエンドポイントURI

 

    • RESTは過剰取得

 

    • GraphQLは階層構造も取得

 

    • GraphQLは日本語情報が少ない

 

    GraphQLはライブラリ・ツール類がReactなどのjs系が中心

2. GraphQL的语言规范

请您放心交给书籍,省略部分。

使用GraphQL和SpringDataJDBC从PostgreSQL获取数据。

依赖关系

    • SpringInitializrでBoot、Gradle、SpringDataJDBC

 

    • PostgreSQLのjdbc4.jar

 

    GraphQL系↓
// 必須
implementation "com.graphql-java-kickstart:graphql-spring-boot-starter:7.1.0“
// 任意(ブラウザ上でクエリを書ける便利ツール)
implementation "com.graphql-java-kickstart:graphiql-spring-boot-starter:7.1.0"

实体类

    Authテーブル
public class Auth {

    @Id
    private long auth_id;
    private long person_id;
    private String login_id;
    private String pass_modify_date;

    ~getter/setter~
}

仓库类

    GraphQL関係ない。SpringDataJDBCの書き方。実装クラス不要
public interface AuthRepository 
  extends CrudRepository<Auth, Long> {

}

解析器类

@Component
public class AuthResolver implements GraphQLQueryResolver {

    private final AuthRepository repository;

    @Autowired
    public AuthResolver(AuthRepository repository) {
        this.repository = repository;
    }

    public Auth getUserById(long auth_id) {

        // Spring Data JDBCでDBアクセス
        Optional<Auth> optAuth = repository.findById(auth_id);
        Auth result = optAuth.orElse(new Auth());
        return result;
    }

}

数据库模式定义

type Auth {
  auth_id: ID!
  person_id: Int
  login_id: String
  pass_modify_date: String
}

type Query {
  getUserById(auth_id: ID!): Auth
}

以下是简化版本的同义句:

执行实例

    実行クエリ
query{
  getUserById(auth_id:100) {
    auth_id
    person_id
    login_id
    pass_modify_date
  }
}
    レスポンス
{
  "data": {
    "getUserById": {
      "auth_id": "100",
      "person_id": 1111111111,
      "login_id": "kyotokyoto@test.test",
      "pass_modify_date": "2021-05-24 18:00:00.000"
    }
  }
}

4. 最后

    • (このレベルのデモであれば)割と簡単に実装できた

 

    • 実際にエンタープライズで導入する際は、色々カベはありそう

 

    • GraphQLのクエリを書くのは楽しい

 

    • RESTfull APIを設計する時の悩みは確かに解消される気がする

 

    • Javaだとまだライブラリや情報が少ないかも

 

    個人的にはもう少し流行って欲しいな~
广告
将在 10 秒后关闭
bannerAds