server {gqlr} | R Documentation |
Run basic GraphQL server
Description
Run a basic GraphQL server using plumber. This server is provided to show basic interaction with GraphQL. The server will run until the function execution is canceled.
Usage
server(schema, port = 8000L, log = TRUE, initial_value = NULL)
Arguments
schema |
Schema object to use execute requests |
port |
web port to serve the server from. Set port to |
log |
boolean that determines if server logging is done. Defaults to TRUE |
initial_value |
default value to use in |
Details
server()
implements the basic necessities described in http://graphql.org/learn/serving-over-http/. There are three routes implemented:
'/'
GET. Returns a GraphQL formatted schema definition
'/graphql'
GET. Executes a query. The parameter
'query'
(which contains a GraphQL formatted query string) must be included. Optional parameters include:'variables'
a JSON string containing a dictionary of variables (defaults to an empty named list),'operationName'
name of the particular query operation to execute (defaults to NULL), and'pretty'
boolean to determine if the response should be compact (FALSE, default) or expanded (TRUE)'/graphql'
POST. Executes a query. Must provide Content-Type of either 'application/json' or 'application/graphql'.
If 'application/json' is provided, a named JSON list containing 'query', 'operationName' (optional, default =
NULL
), 'variables' (optional, default = list()) and 'pretty' (optional, default =TRUE
). The information will used just the same as the GET-'/graphql' route.If 'application/graphql' is provided, the POST body will be interpreted as the query string. All other possible parameters will take on their default value.
Using bash's curl, we can ask the server questions:
#R # load Star Wars schema from 'execute_request' example example(gqlr_schema) # run server server(star_wars_schema, port = 8000)
#bash # GET Schema definition curl '127.0.0.1:8000/' ## POST for R2-D2 and his friends' names # defaults to parse as JSON curl --data '{"query":"{hero{name, friends { name }}}", "pretty": true}' '127.0.0.1:8000/graphql' # send json header curl --data '{"query":"{hero{name, friends { name }}}"}' '127.0.0.1:8000/graphql' --header "Content-Type:application/json" # send graphql header curl --data '{hero{name, friends { name }}}' '127.0.0.1:8000/graphql' --header "Content-Type:application/graphql" # use variables curl --data '{"query":"query Droid($someId: String!) {droid(id: $someId) {name, friends { name }}}", "variables": {"someId": "2001"}}' '127.0.0.1:8000/graphql' # GET R2-D2 and his friends' names curl '127.0.0.1:8000/graphql?query= # ... using a variable curl '127.0.0.1:8000/graphql?query=query