ghql query class
Note
we run an internal method `check_query()` that runs the public method `parse2json()` - if the query doesn't pass the libgraphqlparser parser, we return the error message
Methods
Method print()
print method for the `Query` class
Method add_fragment()
add a fragment to a query
Examples
# make a client
qry <- Query$new()
## define query
qry$query('query2', '{
viewer {
repositories(last: 10, isFork: false, privacy: PUBLIC) {
edges {
node {
isPrivate
id
name
}
}
}
}
}')
qry
#> <ghql: query>
#> queries:
#> query2
qry$queries
#> $query2
#>
#> {
#> viewer {
#> repositories(last: 10, isFork: false, privacy: PUBLIC) {
#> edges {
#> node {
#> isPrivate
#> id
#> name
#> }
#> }
#> }
#> }
#> }
#>
qry$queries$query2
#>
#> {
#> viewer {
#> repositories(last: 10, isFork: false, privacy: PUBLIC) {
#> edges {
#> node {
#> isPrivate
#> id
#> name
#> }
#> }
#> }
#> }
#> }
# fragments
## by hand
qry$query('querywithfrag', '{
ropensci: repositoryOwner(login:"ropensci") {
repositories(first: 3) {
edges {
node {
...Watchers
}
}
}
}
ropenscilabs: repositoryOwner(login:"ropenscilabs") {
repositories(first: 3) {
edges {
node {
...Watchers
}
}
}
}
}
fragment Watchers on Repository {
watchers(first: 3) {
edges {
node {
name
}
}
}
}')
qry
#> <ghql: query>
#> queries:
#> query2
#> querywithfrag
qry$queries
#> $query2
#>
#> {
#> viewer {
#> repositories(last: 10, isFork: false, privacy: PUBLIC) {
#> edges {
#> node {
#> isPrivate
#> id
#> name
#> }
#> }
#> }
#> }
#> }
#>
#> $querywithfrag
#>
#> {
#> ropensci: repositoryOwner(login:"ropensci") {
#> repositories(first: 3) {
#> edges {
#> node {
#> ...Watchers
#> }
#> }
#> }
#> }
#> ropenscilabs: repositoryOwner(login:"ropenscilabs") {
#> repositories(first: 3) {
#> edges {
#> node {
#> ...Watchers
#> }
#> }
#> }
#> }
#> }
#> fragment Watchers on Repository {
#> watchers(first: 3) {
#> edges {
#> node {
#> name
#> }
#> }
#> }
#> }
#>
qry$queries$querywithfrag
#>
#> {
#> ropensci: repositoryOwner(login:"ropensci") {
#> repositories(first: 3) {
#> edges {
#> node {
#> ...Watchers
#> }
#> }
#> }
#> }
#> ropenscilabs: repositoryOwner(login:"ropenscilabs") {
#> repositories(first: 3) {
#> edges {
#> node {
#> ...Watchers
#> }
#> }
#> }
#> }
#> }
#> fragment Watchers on Repository {
#> watchers(first: 3) {
#> edges {
#> node {
#> name
#> }
#> }
#> }
#> }
if (FALSE) { # \dontrun{
token <- Sys.getenv("GITHUB_TOKEN")
con <- GraphqlClient$new(
url = "https://api.github.com/graphql",
headers = list(Authorization = paste0("Bearer ", token))
)
jsonlite::fromJSON(con$exec(qry$queries$querywithfrag))
## use Fragment class fragments generator
### define query without fragment, but referring to it
qry$query('queryfrag', '{
ropensci: repositoryOwner(login:"ropensci") {
repositories(first: 3) {
edges {
node {
...Watchers
}
}
}
}
ropenscilabs: repositoryOwner(login:"ropenscilabs") {
repositories(first: 3) {
edges {
node {
...Watchers
}
}
}
}
}')
### define a fragment, and use it later
frag <- Fragment$new()
frag$fragment('Watchers', '
fragment on Repository {
watchers(first: 3) {
edges {
node {
name
}
}
}
}')
frag$fragments
frag$fragments$Watchers
### add the fragment to the query 'queryfrag'
qry$add_fragment('queryfrag', frag$fragments$Watchers)
qry
qry$queries
qry$queries$queryfrag
} # }