ghql
- general purpose GraphQL client
GraphQL - http://graphql.org
Examples of GraphQL APIs:
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.
Development version
devtools::install_github("ropensci/ghql")
library("ghql")
library("jsonlite")
library("httr")
token <- Sys.getenv("GITHUB_GRAPHQL_TOKEN")
cli <- GraphqlClient$new(
url = "https://api.github.com/graphql",
headers = add_headers(Authorization = paste0("Bearer ", token))
)
Since not every GraphQL server has a schema at the base URL, have to manually load the schema in this case
cli$load_schema()
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)
#> $data
#> named list()
Gives back no result, as we didn't ask for anything :)
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)
#> $data
#> $data$repositoryOwner
#> $data$repositoryOwner$repositories
#> $data$repositoryOwner$repositories$edges
#> node.name node.totalCount
#> 1 sckott.github.com 8
#> 2 usdaplantsapistatus 0
#> 3 usdaplantsapi 19
#> 4 rforcats 34
#> 5 soylocs 2
- Copy the
server.js
file from this package located atinst/server.js
somewhere on your machine. Can locate it on your machine likesystem.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
- 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.