Description Value Public fields Methods Examples
R6 class for constructing GraphQL queries
a 'GraphqlClient' class (R6 class)
url
(character) list of fragments
headers
list of named headers
schema
holds schema
result
holds result from http request
fragments
(list) list of fragments
new()
Create a new 'GraphqlClient' object
GraphqlClient$new(url, headers)
url
(character) URL for the GraphQL schema
headers
Any acceptable headers, a named list. See examples
A new 'GraphqlClient' object
print()
print method for the 'GraphqlClient' class
GraphqlClient$print(x, ...)
x
self
...
ignored
ping()
ping the GraphQL server
GraphqlClient$ping(...)
...
curl options passed on to [crul::verb-HEAD]
'TRUE' if successful response, 'FALSE' otherwise
load_schema()
load schema, from URL or local file
GraphqlClient$load_schema(schema_url = NULL, schema_file = NULL, ...)
schema_url
(character) url for a schema file
schema_file
(character) path to a schema file
...
curl options passed on to [crul::verb-GET]
nothing, loads schema into '$schema' slot
dump_schema()
dump schema to a local file
GraphqlClient$dump_schema(file)
file
(character) path to a file
nothing, writes schema to 'file'
schema2json()
convert schema to JSON
GraphqlClient$schema2json(...)
...
options passed on to [jsonlite::toJSON()]
json
fragment()
load schema, from URL or local file
GraphqlClient$fragment(name, x)
name
(character) fragment name
x
(character) the fragment
nothing returned; sets fragments internally
exec()
execute the query
GraphqlClient$exec(query, variables, encoding = "UTF-8", ...)
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"
...
curl options passed on to [crul::verb-POST]
character string of response, if successful
prep_query()
not used right now
GraphqlClient$prep_query(query)
query
(character) a query, of class 'query' or 'fragment'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | x <- GraphqlClient$new()
x
## Not run:
# make a client
token <- Sys.getenv("GITHUB_GRAPHQL_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)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.