sgqlc.endpoint.requests module

Synchronous HTTP Endpoint

This endpoint implements GraphQL client using the requests library.

This module provides command line utility:

$ python3 -m sgqlc.endpoint.requests '{ query { ... } }'
class sgqlc.endpoint.requests.RequestsEndpoint(url, base_headers=None, timeout=None, method='POST', auth=None)[source]

Bases: sgqlc.endpoint.base.BaseEndpoint

GraphQL access over HTTP.

This helper is very thin, just setups the correct HTTP request to GraphQL endpoint, handling logging of HTTP and GraphQL errors. The object is callable with parameters: query, variables, operation_name, extra_headers and timeout.

The user of this class should create GraphQL queries and interpret the resulting object, created from JSON data, with top level properties:

Data:object matching the GraphQL requests, or null if only errors were returned.
Errors:list of errors, which are objects with the key “message” and optionally others, such as “location” (for errors matching GraphQL input). Instead of raising exceptions, such as requests.exceptions.HTTPError or json.JSONDecodeError those are stored in the “exception” key.


Both data and errors may be returned, for instance if a null-able field fails, it will be returned as null (Python None) in data the associated error in the array.

The class has its own logging.Logger which is used to debug, info, warning and errors. Error logging and conversion to uniform data structure similar to GraphQL, with {"errors": [...]} is done by RequestsEndpoint._log_http_error() own method, BaseEndpoint._log_json_error() and BaseEndpoint._log_graphql_error(). This last one will show the snippets of GraphQL that failed execution.

__call__(query, variables=None, operation_name=None, extra_headers=None, timeout=None)[source]

Calls the GraphQL endpoint.

  • query (str or bytes.) – the GraphQL query or mutation to execute. Note that this is converted using bytes(), thus one may pass an object implementing __bytes__() method to return the query, eventually in more compact form (no indentation, etc).
  • variables (dict) – variables (dict) to use with query. This is only useful if the query or mutation contains $variableName.
  • operation_name (str) – if more than one operation is listed in query, then it should specify the one to be executed.
  • extra_headers (dict) – dict with extra HTTP headers to use.
  • timeout (float) – overrides the default timeout.

dict with optional fields data containing the GraphQL returned data as nested dict and errors with an array of errors. Note that both data and errors may be returned!

Return type:


__init__(url, base_headers=None, timeout=None, method='POST', auth=None)[source]
  • url (str) – the default GraphQL endpoint url.
  • base_headers (dict) – the base HTTP headers to include in every request.
  • timeout (float) – timeout in seconds to use with urllib.request.urlopen(). Optional (None uses default timeout).
  • method – HTTP Method to use for the request, POST is used by default
  • auth – requests.auth compatible authentication option. Optional.

Return str(self).

_log_http_error(query, req, exc)[source]

Log requests.exceptions.HTTPError, converting to GraphQL’s {"data": null, "errors": [{"message": str(exc)...}]}

  • query (str) – the GraphQL query that triggered the result.
  • req (requests.Request) – requests.Request instance that was opened.
  • exc (requests.exceptions.HTTPError) – requests.exceptions.HTTPError instance

GraphQL-compliant dict with keys data and errors.

Return type: