R6 class for constructing GraphQL queries
Public fields
url
(character) list of fragments
headers
list of named headers
schema
holds schema
result
holds result from http request
fragments
(list) list of fragments
Methods
Method new()
Create a new `GraphqlClient` object
Usage
GraphqlClient$new(url, headers)
Method print()
print method for the `GraphqlClient` class
Method load_schema()
load schema, from URL or local file
Method exec()
execute the query
Arguments
query
(character) a query, of class `query` or `fragment`
variables
(list) named list with query variables values
encoding
(character) encoding to use to parse the response. passed on to [crul::HttpResponse] `$parse()` method. default: "UTF-8"
response_headers
If `TRUE`, include the response headers as an attribute of the return object.
...
curl options passed on to [crul::verb-POST]
Examples
x <- GraphqlClient$new()
x
#> <ghql client>
#> url:
if (FALSE) { # \dontrun{
# make a client
token <- Sys.getenv("GITHUB_TOKEN")
cli <- GraphqlClient$new(
url = "https://api.github.com/graphql",
headers = list(Authorization = paste0("Bearer ", token))
)
# if the GraphQL server has a schema, you can load it
cli$load_schema()
# dump schema to local file
f <- tempfile(fileext = ".json")
cli$dump_schema(file = f)
readLines(f)
jsonlite::fromJSON(readLines(f))
# after dumping to file, you can later read schema from file for faster loading
rm(cli)
cli <- GraphqlClient$new(
url = "https://api.github.com/graphql",
headers = list(Authorization = paste0("Bearer ", token))
)
cli$load_schema(schema_file = f)
# variables
cli$url
cli$schema
cli$schema$data
cli$schema$data$`__schema`
cli$schema$data$`__schema`$queryType
cli$schema$data$`__schema`$mutationType
cli$schema$data$`__schema`$subscriptionType
head(cli$schema$data$`__schema`$types)
cli$schema$data$`__schema`$directives
# methods
## ping - hopefully you get TRUE
cli$ping()
## dump schema
cli$schema2json()
## define query
### creat a query class first
qry <- Query$new()
## another
qry$query('repos', '{
viewer {
repositories(last: 10, isFork: false, privacy: PUBLIC) {
edges {
node {
isPrivate
id
name
}
}
}
}
}')
qry
qry$queries
qry$queries$repos
### execute the query
cli$exec(qry$queries$repos)
# query with a fragment
### define query without fragment, but referring to it
qry <- Query$new()
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
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$queryfrag
### execute query: we'll hook together the query and your fragment internally
cli$exec(qry$queries$queryfrag)
} # }