6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

curlでGraphQLの長いqueryを投げる

Posted at

これはなに

curlでGraphQLの長めのqueryコマンドを投げて結果を得るスクリプト

コマンド

GITHUB_TOKENはここから用意してね

ためしにGitHubから JakeWharton のリポジトリを取得してみる。

query.sh
script='query User {
  user(login:\"JakeWharton\") {
    name
    repositories(privacy:PUBLIC,affiliations:OWNER,first:10) {
      edges {
        node {
          name
          description
          createdAt
        }
      }
    }
  }
}'
script="$(echo $script)" #改行を消して1行にする、改行あるとJSON parsingエラーが返ってくる

curl -H "Authorization: bearer GITHUB_TOKEN" \
    -H 'Content-Type: application/json' \
    -X POST -d "{ \"query\": \"$script\"}" https://api.github.com/graphql \
    | jq .

レスポンスをjqで整形して見やすくしている

レスポンス

超長いの途中で省略、こんな形で取得できる

{
  "data": {
    "user": {
      "name": "Jake Wharton",
      "repositories": {
        "edges": [
          {
            "node": {
              "name": "SMSMorse",
              "description": "An Android service that vibrates incoming SMS messages in Morse code.",
              "createdAt": "2009-03-27T03:26:46Z"
            }
          },
          {
            "node": {
              "name": "mkvdts2ac3",
              "description": "Bash script to convert DTS audio to AC3 within a matroska file.",
              "createdAt": "2009-03-27T03:35:57Z"
            }
          },
...

普通ならGraphQL対応のクライアントツールでqueryをなげるものだが、シェルスクリプトの場合はこんな形で使用できる。

6
3
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
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?