graphql API reference
A thin GraphQL client over http / rest. Point a graphql.Client at one endpoint URL, then graphql.query(client, query, variables) POSTs {"query": ..., "variables": ...} and returns the decoded JSON response as a json.Value (the result is under /data).
The query is an opaque string the caller supplies - GraphQL syntax is the server's job, not this module's - and a mutation is just a query string, so no separate verb is needed. The one GraphQL-specific rule this client gets right: a GraphQL execution error is an HTTP 200 with a top-level errors array, not a non-2xx status, so query inspects the payload and raises a graphql.GraphqlError (kind "graphql") carrying the server's messages rather than trusting the status line. A non-2xx response carrying such an envelope (a 4xx for an invalid query, under the GraphQL-over-HTTP media type) is treated the same; any other non-2xx is raised with the status and the start of the body. A 2xx body that is not a GraphQL response (not a JSON object with data and / or a non-empty errors list of objects carrying a string message) is an error too. Every error of kind "graphql" is a GraphqlError: reason says which case it is, and status, codes (each error's extensions.code) and errors (the server's array) carry the details. A 2xx body past json's decoding caps is kind "limit".
For a document that defines several named operations, queryNamed / tryQueryNamed add the operationName selector. To handle GraphQL errors yourself instead of catching a throw - branching on an error code, reading partial data - tryQuery / tryQueryNamed return the raw envelope (both /data and /errors) and raise only on an HTTP failure with no envelope; graphql.hasErrors and graphql.errorMessages inspect it.
The Client wraps a rest.Client, so it carries the endpoint URL, per-request headers, and (via rest's http.Options) TLS settings for a self-signed or private-CA host. A client carrying an Authorization header (bearer, basic, or header) refuses an http:// endpoint on a non-loopback host before connecting, unless its allowInsecureHttp field is set (plain HTTP on a trusted LAN only). Default jennifer binary only (net-backed via http / rest).
Import with import "graphql.j" as graphql;. See the graphql guide for prose and examples.
Contents
| Name | What it is |
|---|---|
basic | Return a copy of the client that sends HTTP Basic Authorization. |
bearer | Return a copy of the client that sends Authorization: Bearer <token>. |
client | A GraphQL client for one endpoint URL. |
errorCodes | Return each error's extensions.code (the machine-readable reason a server gives, such as... |
errorMessages | Join the message field of each entry in a response's errors array into one "; "-separated string. |
hasErrors | Report whether a decoded response carries a non-empty top-level errors array - the GraphQL error signal, ... |
header | Return a copy of the client with an arbitrary request header set. |
query | Run a GraphQL operation (query or mutation) and return the decoded JSON response as a json.Value; the res... |
queryNamed | Like query, but for a document that defines several named operations: the operationName selects which o... |
tryQuery | Run an operation and return the decoded response without raising on GraphQL errors - the envelope comes... |
tryQueryNamed | tryQuery (no raise on GraphQL errors) for a document with several named operations, selected by... |
withBackoff | Return a copy of the client with a base retry backoff in milliseconds (doubled each attempt; 0 = the 250 ms... |
withCA | Return a copy of the client that trusts a private-CA / self-signed certificate (PEM). |
withRetries | Return a copy of the client that retries a query answered 429 or 5xx up to maxRetries times, with exp... |
withSkipVerify | Return a copy of the client that skips TLS certificate verification. |
withTimeout | Return a copy of the client with a per-request idle timeout in milliseconds (0 = the 30 s default). |
Client | struct: A GraphQL client: an endpoint URL plus headers and TLS options, carried in a wrapped rest.Client. |
GraphqlError | struct: A failed GraphQL operation. |
Functions
basic
graphql.basic(c as Client, user as string, pass as string)Return a copy of the client that sends HTTP Basic Authorization.
c- the client
user- the username
pass- the password
- Returns
Client - a new client with the header set
bearer
graphql.bearer(c as Client, token as string)Return a copy of the client that sends Authorization: Bearer <token>.
c- the client
token- the bearer token
- Returns
Client - a new client with the header set
client
graphql.client(endpoint as string)A GraphQL client for one endpoint URL. The full endpoint is POSTed to verbatim (no path is appended), so pass the complete GraphQL URL (e.g. https://host/graphql).
endpoint- the GraphQL endpoint URL
- Returns
Client - a client with no auth and default (verifying) TLS
errorCodes
graphql.errorCodes(resp as json.Value)Return each error's extensions.code (the machine-readable reason a server gives, such as UNAUTHENTICATED or FORBIDDEN), in order, with "" for an error that has none. Empty when the response has no errors array.
resp- a decoded GraphQL response
- Returns
list of string - the codes, one per error
errorMessages
graphql.errorMessages(resp as json.Value)Join the message field of each entry in a response's errors array into one "; "-separated string. The GraphqlError that query raises carries the same messages, each followed by its extensions.code in brackets when it has one (see errorCodes). For structured error data - extensions.code, path, locations - read the errors array of the envelope directly with the json accessors, e.g. json.asString($resp, "/errors/0/extensions/code").
resp- a decoded GraphQL response with an
errorsarray - Returns
string - the joined error messages
hasErrors
graphql.hasErrors(resp as json.Value)Report whether a decoded response carries a non-empty top-level errors array - the GraphQL error signal, which the spec delivers on an HTTP 200. Use this on the envelope returned by tryQuery to branch without a try / catch.
resp- a decoded GraphQL response
- Returns
bool - true if the response reports one or more GraphQL errors
header
graphql.header(c as Client, name as string, value as string)Return a copy of the client with an arbitrary request header set.
c- the client
name- the header name
value- the header value
- Returns
Client - a new client with the header set
query
graphql.query(c as Client, query as string, variables as json.Value)Run a GraphQL operation (query or mutation) and return the decoded JSON response as a json.Value; the result is at /data. The variables is a json.Value object (use an empty json.map() when the operation takes none).
Raises a GraphqlError (kind "graphql") when the server reports GraphQL execution errors (an HTTP 200 with a top-level errors array - the messages are collected into the error), or when the request fails at the HTTP level (a non-2xx status without an envelope, carrying the status and the start of the body). A successful return therefore has no errors, so /data is present and complete. The error's codes and errors fields carry each error's code and the server's array; to read partial data alongside the errors, use tryQuery.
c- the client
query- the GraphQL query or mutation document
variables- the variables object (empty object for none)
- Returns
json.Value - the full decoded response (data under
/data) - Throws
GraphqlError - kind "graphql" on GraphQL errors, a non-2xx HTTP status, a body that is not a GraphQL response, or credentials over plain http (kind "limit" for one past
json's decoding caps)
queryNamed
graphql.queryNamed(
c as Client,
query as string,
variables as json.Value,
operationName as string)Like query, but for a document that defines several named operations: the operationName selects which one to run (some servers require it when more than one is present).
c- the client
query- the GraphQL document
variables- the variables object (empty object for none)
operationName- the name of the operation to execute
- Returns
json.Value - the full decoded response (data under
/data) - Throws
GraphqlError - kind "graphql" on GraphQL errors, a non-2xx HTTP status, a body that is not a GraphQL response, or credentials over plain http (kind "limit" for one past
json's decoding caps)
tryQuery
graphql.tryQuery(c as Client, query as string, variables as json.Value)Run an operation and return the decoded response without raising on GraphQL errors - the envelope comes back with both /data (possibly partial) and /errors, so you can inspect the errors yourself (graphql.hasErrors($resp), graphql.errorMessages($resp), or json.asString($resp, "/errors/0/extensions/code") for structured data). A non-2xx response with an errors envelope is returned too; one without (a proxy page, a 502) still raises, since there is nothing to return.
c- the client
query- the GraphQL query or mutation document
variables- the variables object (empty object for none)
- Returns
json.Value - the full decoded response (data under
/data, errors under/errors) - Throws
GraphqlError - kind "graphql" only on a non-2xx HTTP status without an errors array, a body that is not a GraphQL response, or credentials over plain http (kind "limit" for one past
json's decoding caps)
tryQueryNamed
graphql.tryQueryNamed(
c as Client,
query as string,
variables as json.Value,
operationName as string)tryQuery (no raise on GraphQL errors) for a document with several named operations, selected by operationName.
c- the client
query- the GraphQL document
variables- the variables object (empty object for none)
operationName- the name of the operation to execute
- Returns
json.Value - the full decoded response (data under
/data, errors under/errors) - Throws
GraphqlError - kind "graphql" only on a non-2xx HTTP status without an errors array, a body that is not a GraphQL response, or credentials over plain http (kind "limit" for one past
json's decoding caps)
withBackoff
graphql.withBackoff(c as Client, backoffMs as int)Return a copy of the client with a base retry backoff in milliseconds (doubled each attempt; 0 = the 250 ms default). Pairs with withRetries.
c- the client
backoffMs- the base backoff in milliseconds
- Returns
Client - a new client with the backoff set
withCA
graphql.withCA(c as Client, pem as bytes)Return a copy of the client that trusts a private-CA / self-signed certificate (PEM). Certificate verification stays on, against this CA.
c- the client
pem- the CA certificate in PEM form
- Returns
Client - a new client with the CA trusted
withRetries
graphql.withRetries(c as Client, maxRetries as int)Return a copy of the client that retries a query answered 429 or 5xx up to maxRetries times, with exponential backoff honouring a numeric Retry-After. A mutation or subscription is never retried: every GraphQL operation is a POST, and repeating a write the server may have done is not safe. A document counts as a query when it starts with { or query, or when operationName names a query operation in it; one that starts with a fragment definition is not retried.
c- the client
maxRetries- the retry limit for queries
- Returns
Client - a new client that retries queries
withSkipVerify
graphql.withSkipVerify(c as Client)Return a copy of the client that skips TLS certificate verification. Insecure - for a trusted network or a local test server only.
c- the client
- Returns
Client - a new client that accepts any server certificate
withTimeout
graphql.withTimeout(c as Client, timeoutMs as int)Return a copy of the client with a per-request idle timeout in milliseconds (0 = the 30 s default). It bounds each read, so a hung server fails rather than blocking.
c- the client
timeoutMs- the per-read idle timeout in milliseconds
- Returns
Client - a new client with the timeout set
Structs
Client
A GraphQL client: an endpoint URL plus headers and TLS options, carried in a wrapped rest.Client. Build one with graphql.client, then layer auth / TLS with the builders (each returns a new Client; value semantics, no mutation).
def struct Client {
rest as rest.Client,
allowInsecureHttp as bool
};rest- the underlying REST client (endpoint, headers, TLS)
allowInsecureHttp- allow an
Authorizationheader over plainhttp://to a non-loopback host (default false)
GraphqlError
A failed GraphQL operation. Every error of kind "graphql" is one, so its fields are safe to read once the kind matches.
def struct GraphqlError {
kind as string,
message as string,
reason as string,
status as int,
codes as list of string,
errors as json.Value
};kind- always
"graphql" message- the server's messages (each with its code in brackets), or the HTTP status and the start of the body
reason"graphql"(the server answered with anerrorsarray, at any status),"http"(a non-2xx without one),"json"(a 2xx whose body is not a JSON GraphQL response), or"insecure"(anAuthorizationheader refused over plainhttp://; status 0, nothing was sent)status- the HTTP status
codes- each error's
extensions.code(such asUNAUTHENTICATED), "" for one without; empty unlessreasonis"graphql" errors- the server's
errorsarray, withmessage,path,locationsandextensions; JSONnullunlessreasonis"graphql"