🙄
graphqurl(gq) で graphql api を歩く
graphqurl とは?
curl のように graphql api にリクエストできる便利ツール
graphqurlのInstall
$ npm install -g graphqurl
$ which graphqurl
$ $HOME/.nodebrew/current/bin/graphqurl
GitHub API を Call してみる
事前準備: PAT を取得
Fine-grained personal access token を取得します。
-q でクエリを指定する
$ gq https://api.github.com/graphql \
-H 'Authorization: bearer github_pat' \
-q 'query { user(login: "shase") { url } }' | jq .
(node:35404) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Executing query... done
{
"data": {
"user": {
"url": "https://github.com/shase"
}
}
}
-q をつけないとインタラクティブモード
$ gq https://api.github.com/graphql \
-H 'Authorization: bearer github_pat'
(node:39122) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Introspecting schema... done
Enter the query, use TAB to auto-complete, Ctrl+Q / Enter to execute, Ctrl+C to cancel
gql> query {user(login: "shase") { url } }
Waiting... done
{
"data": {
"user": {
"url": "https://github.com/shase"
}
}
}
クエリファイルを別途作成、指定して、引数も個別に指定する
$ cat sample.graphql
query GetUserUrl($target_id: String!) {
user(login: $target_id) {
url
}
}
$ gq https://api.github.com/graphql \
-H 'Authorization: bearer github_pat' \
--queryFile sample.graphql \
--operationName GetUserUrl \
--variablesFile <(echo '{"target_id":"shase"}')
(node:46538) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Executing query... done
{
"data": {
"user": {
"url": "https://github.com/shase"
}
}
}
余談:プログラムから扱う場合は、スキーマファイルを取得してそこからコード生成することも多い
get-graphql-schema を使う
$ npm install -g get-graphql-schema
$ get-graphql-schema -h -h 'Authorization=Bearer github_pat' https://api.github.com/graphql > schema.graphql
Discussion