General purpose GraphQL R client
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

ghql

Build Status codecov.io

ghql - general purpose GraphQL client

GraphQL - http://graphql.org

ghql uses the rOpenSci's package graphql, GraphQL query parser.

Examples of GraphQL APIs:

Github Authentication

See https://developer.github.com/early-access/graphql/guides/accessing-graphql/ for getting an OAuth token.

Store the token in a env var called GITHUB_GRAPHQL_TOKEN before trying this pkg.

Install

Development version

devtools::install_github("ropensci/ghql")
library("ghql")
library("jsonlite")

initialize client

library("httr")
token <- Sys.getenv("GITHUB_GRAPHQL_TOKEN")
cli <- GraphqlClient$new(
  url = "https://api.github.com/graphql",
  headers = add_headers(Authorization = paste0("Bearer ", token))
)

load schema

Since not every GraphQL server has a schema at the base URL, have to manually load the schema in this case

cli$load_schema()

basic query

Make a Query class object

qry <- Query$new()
qry$query('myquery', 'query { }')
qry
#> <ghql: query>
#>   queries:
#>     myquery
qry$queries
#> $myquery
#>  
#>  query { }
qry$queries$myquery
#>  
#>  query { }
cli$exec(qry$queries$myquery)
#> [1] "{\"data\":null,\"errors\":[{\"message\":\"Parse error on \\\"}\\\" (RCURLY) at [1, 9]\",\"locations\":[{\"line\":1,\"column\":9}]}]}\n"

Gives back no result, as we didn't ask for anything :)

Get some actual data

qry$query('getdozedata', '{
  repositoryOwner(login:"sckott") {
    repositories(first: 5, orderBy: {field:PUSHED_AT,direction:DESC}, isFork:false) {
      edges {
        node {
          name
          stargazers {
            totalCount
          }
        }
      }
    }
  }
}')
qry
#> <ghql: query>
#>   queries:
#>     myquery    
#>     getdozedata
qry$queries$getdozedata
#>  
#>  {
#>   repositoryOwner(login:"sckott") {
#>     repositories(first: 5, orderBy: {field:PUSHED_AT,direction:DESC}, isFork:false) {
#>       edges {
#>         node {
#>           name
#>           stargazers {
#>             totalCount
#>           }
#>         }
#>       }
#>     }
#>   }
#> }
cli$exec(qry$queries$getdozedata)
#> [1] "{\"data\":{\"repositoryOwner\":{\"repositories\":{\"edges\":[{\"node\":{\"name\":\"ropenblog\",\"stargazers\":{\"totalCount\":2}}},{\"node\":{\"name\":\"Headstart\",\"stargazers\":{\"totalCount\":101}}},{\"node\":{\"name\":\"BacDiveR\",\"stargazers\":{\"totalCount\":2}}},{\"node\":{\"name\":\"makeregistry\",\"stargazers\":{\"totalCount\":0}}},{\"node\":{\"name\":\"usdaplantsapi\",\"stargazers\":{\"totalCount\":39}}}]}}}}\n"

run a local GraphQL server

  • Copy the server.js file from this package located at inst/server.js somewhere on your machine. Can locate it on your machine like system.file("js/server.js", package = "ghql"). Or you can run the file from where it's at, up to you.
  • Make sure node is installed. If not, see https://nodejs.org
  • Run node server.js
  • Navigate to your browser - go to http://localhost:4000/graphql
  • Back in R, user that URL to connect
(cli <- GraphqlClient$new("http://localhost:4000/graphql"))
#> <ghql client>
#>   url: http://localhost:4000/graphql
xxx <- Query$new()
xxx$query('query', '{
  __schema {
    queryType {
      name, 
      fields {
        name,
        description
      }
    }
  }
}')
cli$exec(xxx$queries$query)
#> $data
#> $data$`__schema`
#> $data$`__schema`$queryType
#> $data$`__schema`$queryType$name
#> [1] "Query"
#> 
#> $data$`__schema`$queryType$fields
#>    name description
#> 1 hello            
#> 2  name 

Meta

  • Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.