subgrounds.transform module
Subgrounds request/response transformation layers module
This module defines interfaces (abstract classes) for transformation layers.
Transformation layers, or transforms, can be applied to entire
requests (see RequestTransform
) or on a per-document basis (see
DocumentTransform
). Classes that implement either type of transforms
can be used to perform modifications to queries and their response data.
For example, the TypeTransform
class is used to tranform the response
data of BigInt
and BigDecimal
fields (which are represented as
strings in the response JSON data) to python int
and float
respectively (see the actual transforms in DEFAULT_SUBGRAPH_TRANSFORMS
).
Transforms are also used to apply SyntheticField
to queries and the
response data (see LocalSyntheticField
transform class). Each
SyntheticField
defined on a subgraph creates a new transformation layer
by instantiating a new LocalSyntheticField
object.
- subgrounds.transform.select_data(select, data)
- class subgrounds.transform.RequestTransform
Bases:
abc.ABC
Abstract class representing a transformation layer to be applied to entire
DataRequest
objects.- abstract transform_request(req)
Method that will be applied to all
DataRequest
objects that pass through the transformation layer.- Parameters
req (DataRequest) -- The initial request object
- Returns
The transformed request object
- Return type
- abstract transform_response(req, data)
Method to be applied to all response data
data
of requests that pass through the transformation layer.req
is the initialDataRequest
object that yielded the resulting JSON datadata
.
- class subgrounds.transform.DocumentTransform
Bases:
abc.ABC
Abstract class representing a transformation layer to be applied to
Document
objects.- abstract transform_document(doc)
Method that will be applied to all
Document
objects that pass through the transformation layer.
- abstract transform_response(req, data)
Method to be applied to all response data
data
of requests that pass through the transformation layer.doc
is the initialDocument
object that yielded the resulting JSON datadata
.
- class subgrounds.transform.TypeTransform(type_, f)
Bases:
subgrounds.transform.DocumentTransform
Transform to be applied to scalar fields on a per-type basis.
- type_
Type indicating which scalar values (i.e.: values of that type) should be transformed using the function
f
- Type
- f
Function to be applied to scalar values of type
type_
in the response data.- Type
Callable[[Any], Any]
- type_: subgrounds.schema.TypeRef.T
- f: Callable[[Any], Any]
- transform_document(doc)
Method that will be applied to all
Document
objects that pass through the transformation layer.
- transform_response(doc, data)
Method to be applied to all response data
data
of requests that pass through the transformation layer.doc
is the initialDocument
object that yielded the resulting JSON datadata
.
- class subgrounds.transform.LocalSyntheticField(subgraph, fmeta, type_, f, default, args)
Bases:
subgrounds.transform.DocumentTransform
Transform used to implement synthetic fields on GraphQL objects that only depend on values accessible from that object.
transform_document()
replaces the fieldfmeta
by the argument fields selectionsargs
if the synthetic fieldfmeta
is present in the document.transform_response()
appliedf
to the fields corresponding to the argument selectionsargs
and places the result in the response.- fmeta
The synthetic field
- Type
- type_
The object for which the synthetic field is defined
- f
The function to be applied to the argument fields
- Type
Callable
- default
The default value of the synthetic field used in case of exceptions (e.g.: division by zero)
- Type
Any
- args
The selections of the fields used as arguments to compute the synthetic field
- fmeta: TypeMeta.FieldMeta
- f: Callable
- default: Any
- transform_document(doc)
Method that will be applied to all
Document
objects that pass through the transformation layer.
- transform_response(doc, data)
Method to be applied to all response data
data
of requests that pass through the transformation layer.doc
is the initialDocument
object that yielded the resulting JSON datadata
.
- class subgrounds.transform.SplitTransform(query)
Bases:
subgrounds.transform.RequestTransform
- transform_request(req)
Method that will be applied to all
DataRequest
objects that pass through the transformation layer.- Parameters
req (DataRequest) -- The initial request object
- Returns
The transformed request object
- Return type
- transform_response(req, data)
Method to be applied to all response data
data
of requests that pass through the transformation layer.req
is the initialDataRequest
object that yielded the resulting JSON datadata
.