sgqlc.endpoint.base module

Base Endpoint

Base interface for endpoints.

class sgqlc.endpoint.base.BaseEndpoint[source]

Bases: object

GraphQL endpoint access.

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 json.JSONDecodeError those are stored in the “exception” key. Subclasses should extend errors providing meaningful messages and extra payload.


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. Note that subclasses may override this logger. Error logging and conversion to uniform data structure similar to GraphQL, with {"errors": [...]} is done by BaseEndpoint._log_json_error() and BaseEndpoint._log_graphql_error() methods. This last one will show the snippets of GraphQL that failed execution.

__call__(query, variables=None, operation_name=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.

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:



Subclasses must implement this method, should respect this base signature and may extend with extra parameters such as timeout, extra headers and so on.


list of weak references to the object (if defined)


Given a possible GraphQL error payload, make sure it’s in shape.

This will ensure the given data is in the shape:

{"errors": [{"message": "some string"}]}

If errors is not an array, it will be made into a single element array, with the object in that format, with its string representation being the message.

If an element of the errors array is not in the format, then it’s converted to the format, with its string representation being the message.

The input object is not changed, a copy is made if needed.

Returns:the given data formatted to the correct shape, a copy is made and returned if any fix up was needed.
Return type:dict
_log_graphql_error(query, data)[source]

Log a {"errors": [...]} GraphQL return and return itself.

  • query (str) – the GraphQL query that triggered the result.
  • data (dict) – the decoded JSON object.

the input data

Return type:


_log_json_error(body, exc)[source]

Log a json.JSONDecodeError, converting to GraphQL’s {"data": null, "errors": [{"message": str(exc)...}]}


GraphQL-compliant dict with keys data and errors.

Return type:


static snippet(code, locations, sep=' | ', colmark=('-', '^'), context=5)[source]

Given a code and list of locations, convert to snippet lines.

return will include line number, a separator (sep), then line contents.

At most context lines are shown before each location line.

After each location line, the column is marked using colmark. The first character is repeated up to column, the second character is used only once.

Returns:list of lines of sources or column markups.
Return type:list