ghql
- a GraphQL client for R
GraphQL - https://graphql.org
Examples of GraphQL APIs:
Other GraphQL R packages:
Note: To be clear, this R package isn’t just for the GitHub GraphQL API, but it is the most public GraphQL API we can think of, so is used in examples throughout here.
See https://developer.github.com/v4/guides/intro-to-graphql/ for getting an OAuth token.
Store the token in a env var called
GITHUB_GRAPHQL_TOKEN
CRAN version
install.packages("ghql")
Development version
::install_github("ropensci/ghql") remotes
library("ghql")
library("jsonlite")
<- Sys.getenv("GITHUB_GRAPHQL_TOKEN")
token <- GraphqlClient$new(
con url = "https://api.github.com/graphql",
headers = list(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
$load_schema() con
Make a Query
class object
<- Query$new() qry
When you construct queries we check that they are properly formatted using the graphql that leverages the libgraphqlparser C++ parser. If the query is malformed, we return a message as to why the query is malformed.
Get some stargazer counts
$query('mydata', '{
qry repositoryOwner(login:"sckott") {
repositories(first: 5, orderBy: {field:PUSHED_AT,direction:DESC}, isFork:false) {
edges {
node {
name
stargazers {
totalCount
}
}
}
}
}
}')
qry#> <ghql: query>
#> queries:
#> mydata
$queries$mydata
qry#>
#> {
#> repositoryOwner(login:"sckott") {
#> repositories(first: 5, orderBy: {field:PUSHED_AT,direction:DESC}, isFork:false) {
#> edges {
#> node {
#> name
#> stargazers {
#> totalCount
#> }
#> }
#> }
#> }
#> }
#> }
# returns json
<- con$exec(qry$queries$mydata))
(x #> [1] "{\"data\":{\"repositoryOwner\":{\"repositories\":{\"edges\":[{\"node\":{\"name\":\"Headstart\",\"stargazers\":{\"totalCount\":124}}},{\"node\":{\"name\":\"extcite\",\"stargazers\":{\"totalCount\":5}}},{\"node\":{\"name\":\"serrano\",\"stargazers\":{\"totalCount\":19}}},{\"node\":{\"name\":\"soylocs\",\"stargazers\":{\"totalCount\":2}}},{\"node\":{\"name\":\"makeregistry\",\"stargazers\":{\"totalCount\":3}}}]}}}}\n"
# parse to an R list
::fromJSON(x)
jsonlite#> $data
#> $data$repositoryOwner
#> $data$repositoryOwner$repositories
#> $data$repositoryOwner$repositories$edges
#> node.name node.totalCount
#> 1 Headstart 124
#> 2 extcite 5
#> 3 serrano 19
#> 4 soylocs 2
#> 5 makeregistry 3
Define a query
<- Query$new()
qry $query('getgeninfo', 'query getGeneInfo($genId: String!){
qry geneInfo(geneId: $genId) {
id
symbol
chromosome
start
end
bioType
__typename
}
}')
Define a variable as a named list
<- list(genId = 'ENSG00000137033') variables
Creat a clint and make a request, passing in the query and then the variables
<- GraphqlClient$new('https://genetics-api.opentargets.io/graphql')
con <- con$exec(qry$queries$getgeninfo, variables)
res ::fromJSON(res)
jsonlite#> $data
#> $data$geneInfo
#> $data$geneInfo$id
#> [1] "ENSG00000137033"
#>
#> $data$geneInfo$symbol
#> [1] "IL33"
#>
#> $data$geneInfo$chromosome
#> [1] "9"
#>
#> $data$geneInfo$start
#> [1] 6215786
#>
#> $data$geneInfo$end
#> [1] 6257983
#>
#> $data$geneInfo$bioType
#> [1] "protein_coding"
#>
#> $data$geneInfo$`__typename`
#> [1] "Gene"
Datacite provides DOIs for research data. Check out the Datacite GraphQL docs to get started. A minimal example:
<- GraphqlClient$new("https://api.datacite.org/graphql")
con <- Query$new()
qry $query('dc', '{
qry publications(query: "climate") {
totalCount
nodes {
id
titles {
title
}
descriptions {
description
}
creators {
name
familyName
}
fundingReferences {
funderIdentifier
funderName
awardTitle
awardNumber
}
}
}
}')
<- con$exec(qry$queries$dc)
res head(jsonlite::fromJSON(res)$data$publications$nodes)
#> id
#> 1 https://doi.org/10.7915/cig1zc7s1
#> 2 https://doi.org/10.7915/cig3804z3
#> 3 https://doi.org/10.7915/cig56d5q6
#> 4 https://doi.org/10.7915/cig4jm245
#> 5 https://doi.org/10.7915/cig0ns0kz
#> 6 https://doi.org/10.7915/cig0xp6v4
#> titles
#> 1 Forest Growth and Climate Change
#> 2 Forest Fire and Climate
#> 3 Climate and Water Policy Workshop: Executive Summary
#> 4 Forest Change
#> 5 Impacts of Climate Change on PNW Timber Production
#> 6 HB 1303 Interim Report: A Comprehensive Assessment of the Impacts of Climate Change on the State of Washington
#> descriptions creators fundingReferences
#> 1 NULL Climate Impacts Group, NA NULL
#> 2 NULL Climate Impacts Group, NA NULL
#> 3 NULL Climate Impacts Group, NA NULL
#> 4 NULL Climate Impacts Group, NA NULL
#> 5 NULL Climate Impacts Group, NA NULL
#> 6 NULL Climate Impacts Group, NA NULL
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.node server.js
<- GraphqlClient$new("http://localhost:4000/graphql"))
(con #> <ghql client>
#> url: http://localhost:4000/graphql
<- Query$new()
xxx $query('query', '{
xxx __schema {
queryType {
name,
fields {
name,
description
}
}
}
}')
$exec(xxx$queries$query)
con#> $data
#> $data$`__schema`
#> $data$`__schema`$queryType
#> $data$`__schema`$queryType$name
#> [1] "Query"
#>
#> $data$`__schema`$queryType$fields
#> name description
#> 1 hello
#> 2 name