LoginSignup
4
0

CLIでGraphQL subscriptionの動作確認をする

Last updated at Posted at 2023-12-02

はじめに

この記事は一休.com Advent Calendar 2023 2日目の記事です

GraphQL subscrptionのフィールドの実装中、たいていGUIで動作確認すると思います。私は普段Apollo Studioで下記のような画面を眺めながら開発しています

スクリーンショット 2023-12-02 20.05.53.png

しかし、エンジニアたるものCLIでも確認したいですよね。というわけで今回は小ネタとしてCLIでGraphQL subscriptionの動作確認をする方法を紹介します

環境

  • GraphQLサーバーフレームワーク: Apollo Server
  • プロトコル: websocket
  • websocketライブラリ: graphql-ws

手順

  1. wscatで接続する
    wscat -s graphql-transport-ws -c ws://127.0.0.1/graphql
    Connected (press CTRL+C to quit)
    
  2. タイムアウトする前に急いでinitメッセージを送る
    > {"type": "connection_init"}
    < {"type":"connection_ack"}
    
  3. クエリを送信する
    > {"id":"hoge","type":"subscribe","payload":{"query":"subscription ($restaurantId: Int!, $groupId: String!, $date: Date!) {\n  group(groupId: $groupId) {\n    restaurant(restaurantId: $restaurantId) {\n      operatingDay(date: $date) {\n        reservations {\n          reservationId\n          visitDateTime {\n            begin\n            end\n          }\n          guest {\n            name\n          }\n        }\n      }\n    }\n  }\n}","variables":{"restaurantId":***,"groupId":"ikyu","date": "2023-12-02"}}}
    < {"id":"hoge","type":"next","payload":{"data":{"group":{"restaurant":{"operatingDay":{"reservations":[]}}}}}}
    
  4. publishを発火させると、更新された結果を受信する
    < {"id":"hoge","type":"next","payload":{"data":{"group":{"restaurant":{"operatingDay":{"reservations":[{"reservationId":"32a88f68-71fc-4492-aba5-4d2171b3f87e","visitDateTime":{"begin":"2023-12-02T11:00:00.000Z","end":"2023-12-02T13:00:00.000Z"},"guest":{"name":"ミーア・ルーナ・ティアムーン"}}]}}}}}}
    

所感

wscatは独特な対話インターフェースを持つプログラムなのでGraphQLの文脈で普段使いするのは難しいですが、ラップして透過的に扱えるようなツールがあればこうした用途でも十分使える気がするので、そういうものを知ってるよっていう方や作ったよっていう方がいれば教えてください

参考

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0