gqlr (version 0.0.1)

server: Run basic GraphQL server

Description

Run a basic GraphQL server with the jug package. 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 NULL to not run the jug server and return it.

log

boolean that determines if server logging is done. Defaults to TRUE

initial_value

default value to use in execute_request()

Details

server() implements the basic necessities described in http://graphql.org/learn/serving-over-http/. There are three routes implemented:

'/'

GET. Returns a GraphQL formated 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/'

# GET R2-D2 and his friends' names curl '127.0.0.1:8000/graphql?query=

# POST for R2-D2 and his friends' names curl --data '{"query":"{hero{name}}"}' '127.0.0.1:8000/graphql' # defaults to parse as JSON curl --data '{"query":"{hero{name}}"}' '127.0.0.1:8000/graphql' --header "Content-Type:application/json" curl --data '{hero{name}}' '127.0.0.1:8000/graphql' --header "Content-Type:application/graphql"