How to use curl to access the github graphql API

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 a one-liner, without newlines

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

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


Since this is the first hit for 'graphql curl', here's a simple example:

$ curl \
  --request POST \
  --header 'Content-Type: application/json' \
  --data '{"query": "query { fish(key:\"838\") { name } }"}' \
  http://localhost:4001

{"data":{"fish":{"name":"plecy"}}}