How to use curl to access the github graphql API

前端 未结 3 1198
旧时难觅i
旧时难觅i 2020-12-29 05:51

After referring this guide I needed to access the github graphql by using curl for a testing purpose. I tried this simple command

         


        
相关标签:
3条回答
  • 2020-12-29 05:58

    You just need to escape the double quotes that are inside the JSON as the query

    $ curl -i -H 'Content-Type: application/json' -H "Authorization: bearer myGithubAccessToken" -X POST -d '{"query": "query {repository(owner: \"wso2\", name: \"product-is\") {description}}"}' https://api.github.com/graphql
    
    
    0 讨论(0)
  • 2020-12-29 06:04

    I recommend storing the graphql in one file, and script for processing it in a separate file, and then combining the two at the prompt.

    This lets you use graphql syntax highlighting plugins and graphql pretty printers while editing examplequery.gql in your favorite editor. While also preserving the ability to make use of your cli toolkit for cases where your graphql-fu isn't up to the task.

    Usage:

    ❯ ./ghgql.sh examplequery.gql
    
        {"data":{"user":{"repositories":{"nodes":[{"name":"firstrepo","languages":{"nodes":[]}},{"name":"secondrepo","languages":{"nodes":[{"name":"Shell"},{"name":"Vim script"}]}},{"name":"thirdrepo","languages":{"nodes":[{"name":"TeX"}]}}]}}}}
    
    ❯ ./ghgql.sh examplequery.gql \
        | jq -c '.data.user.repositories.nodes | to_entries | .[]' \
        | grep 'TeX' \
        | jq -r '.value.name'
    
        thirdrepo
    
    

    ghgql.sh

    #!/usr/bin/env bash
    
    if [ ! -f $1 ] || [ $# -ne 1 ]
    then
        echo Queries the github graphql API
        echo "Usage:"
        echo
        echo "$0 somefile.gql"
    fi
    
    # read the gql query from the file named in the argument
    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
    TOKEN=$(cat $DIR/token)
    QUERY=$(jq -n \
               --arg q "$(cat $1 | tr -d '\n')" \
               '{ query: $q }')
    
    # do the query
    curl -s -X POST \
      -H "Content-Type: application/json" \
      -H "Authorization: bearer $TOKEN" \
      --data "$QUERY" \
      https://api.github.com/graphql
    

    examplequery.gql

    {
      user(login: "MatrixManAtYrService") {
        repositories(first: 3) {
          nodes {
            name
            languages(first: 3) {
              nodes {
                name
              }
            }
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-29 06:06

    If you want your queries to stay nice and multiline, you may do like this:

    script='query {
      repositoryOwner(login:\"danbst\") {
        repositories(first: 100) {
          edges {
            node {
              nameWithOwner
              pullRequests(last: 100, states: OPEN) {
                edges {
                  node {
                    title
                    url
                    author {
                      login
                    }
                    labels(first: 20) {
                      edges {
                        node {
                          name
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }'
    script="$(echo $script)"   # the query should be onliner, without newlines
    
    curl -i -H 'Content-Type: application/json' \
       -H "Authorization: bearer ........." \
       -X POST -d "{ \"query\": \"$script\"}" https://api.github.com/graphql
    
    0 讨论(0)
提交回复
热议问题