1
3

More than 1 year has passed since last update.

Curl: GitHub で GraphQL を使う

Last updated at Posted at 2019-04-08

GitHub で GraphQL を使う方法です。
次のページを参考にしました。
GraphQL入門 - 使いたくなるGraphQL

  1. GitHub にログインして、アクセストークンを作成します。
  2. 方法は次のページで説明されています。 [Creating a personal access token for the command line](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line)
  3. ログインID からユーザー名を取得します。
  4.  私の ekzemplaro を例に取ります。
    get_name.sh
    #! /bin/bash
    #
    #       get_name.sh
    #
    #                                               Apr/8/2019
    # 
    ACCESS_TOKEN="*******your_access_token**********"
    #
    URL="https://api.github.com/graphql"
    #
    curl -H "Authorization: bearer "$ACCESS_TOKEN -X POST -d@query.json $URL
    #
    
    query.json
    {
     "query": "query { user(login: \"ekzemplaro\") { name }}"
    }
    

    実行結果

    $ ./get_name.sh 
    {"data":{"user":{"name":"Uchida Masatomo"}}}
    
  5. クエリーをもう少し複雑にしてみます。
  6. query02.json
    {
     "query": "query { user(login: \"ekzemplaro\") { name location url }}"
    }
    

    実行結果

    {
      "data": {
        "user": {
          "name": "Uchida Masatomo",
          "location": "Tochigi,Japan",
          "url": "https://github.com/ekzemplaro"
        }
      }
    }
    
  7. クエリーを変更します。
  8. query03.json
    {
     "query": "query {viewer {
        repositories(first: 1) { edges { node { name description } } }
        }
      }"
    }
    

    実行結果

    {
      "data": {
        "viewer": {
          "repositories": {
            "edges": [
              {
                "node": {
                  "name": "librivox_catalog",
                  "description": "LibriVox's catalog page"
                }
              }
            ]
          }
        }
      }
    }
    
  9. 最新2つのリボジトリーを取得してみます。
  10. query04.json
    {
     "query": "query {viewer {
    repositories(last: 2) { edges { node { name description } } }
        }
      }"
    }
    

    実行結果

    {
      "data": {
        "viewer": {
          "repositories": {
            "edges": [
              {
                "node": {
                  "name": "django_plural_table",
                  "description": "Django sample"
                }
              },
              {
                "node": {
                  "name": "django_member_shozoku",
                  "description": "Django Example"
                }
              }
            ]
          }
        }
      }
    }
    

確認したバージョン

$ curl --version
curl 8.0.1 (x86_64-pc-linux-gnu) libcurl/8.0.1 OpenSSL/3.0.8 zlib/1.2.13 brotli/1.0.9 zstd/1.5.4 libidn2/2.3.4 libpsl/0.21.2 (+libidn2/2.3.4) libssh2/1.10.0 nghttp2/1.52.0
Release-Date: [unreleased]
Protocols: dict file ftp ftps gopher gophers http https imap imaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL threadsafe TLS-SRP UnixSockets zstd

参考

GitHub GraphQL API Explorer の使い方

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