Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use curl to access the github graphql API

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

curl -i -H "Authorization: bearer myGithubAccessToken" -X POST -d '{"query": "query {repository(owner: "wso2", name: "product-is") {description}}"}' https://api.github.com/graphql 

but it gives me

problems parsing JSON

what I am doing wrong. I spent nearly 2 hours trying to figure it and tried different examples but none of them worked. Can you please be kind enough help me resolve this

like image 837
Kasun Siyambalapitiya Avatar asked Feb 03 '17 09:02

Kasun Siyambalapitiya


People also ask

How do I connect GraphQL TO REST API?

In three steps, you write the schema that describes your GraphQL endpoint. Define an interface and queries for a type that the front-end application will query. This could be a customer type, an order type, a ticket, whatever. Connect backends by creating one or more concrete types in support of the interface type.

How does GitHub use GraphQL?

GitHub chose GraphQL because it offers significantly more flexibility for our integrators. The ability to define precisely the data you want—and only the data you want—is a powerful advantage over traditional REST API endpoints.

How do I find my GraphQL API?

Using this interface, simply write queries using the GraphQL syntax and specify any variables used in the query in JSON format. You can then run the test step on its own, or add an assertion to verify a particular response using properties like contains, equals, counts and even regular expression matches.


2 Answers

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  
like image 114
Yuri Schimke Avatar answered Sep 22 '22 09:09

Yuri Schimke


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 
like image 35
danbst Avatar answered Sep 23 '22 09:09

danbst