Skip to main content

links.transpile

TranspileHandler Objects

class TranspileHandler(BaseModel)

A Transpilation Handler

A TranspileHandler is a function that takes any Any and returns a GraphQLType. It is used to implement custom type resolution.

The default TranspileHandler is the identity function, which returns the type passed to it.

ListTranspileHandler Objects

class ListTranspileHandler(BaseModel)

A List Transpile Handler

Similar to a TranspileHandler, but takes act on GraphqQLList Type of that type

TranspilationError Objects

class TranspilationError(Exception)

A transpilation Exception

TranspilationHandlerException Objects

class TranspilationHandlerException(TranspilationError)

A transpilation Exception happening within a TranspileHandler

TranspileRegistry Objects

class TranspileRegistry(BaseModel)

A Registry to hold TranspileHandlers

register_item

def register_item(graphql_type: str,
predicate: Callable[[Any], bool],
name=None)

A Decorator for registering a TranspileHandler

If acting on a List of this type, the handle_list parameter should be set to True.

Example:

registry = TranspileRegistry()

@registry.register_item("ID", lambda x: isinstance(x, BaseModel))
def transpile_id_to_id(x):
return str(x.id)

Arguments:

  • graphql_type str - The graphql Type to act upon
  • predicate Callable[[Any], bool] - A predicate Function
  • name _type, optional_ - A name for this hanlder. Defaults to the function name.

register_list

def register_list(graphql_type: str,
predicate: Callable[[Any, str], bool],
name=None)

A Decorator for registering a TranspileHandler

If acting on a List of this type, the handle_list parameter should be set to True.

Example:

registry = TranspileRegistry()

@registry.register_list("InputVector", lambda x, listdepth: isinstance(x, np.ndarray))
def transpile_numpy_array_to_vectors(x, listdepth):
assert listdepth == 1, "Only one level of nesting is supported"
return [InputVector(x) for x in x]

Arguments:

  • graphql_type str - The graphql Type to act upon
  • predicate Callable[[Any], bool] - A predicate Function
  • handle_list bool, optional - Should we act on lists of this type. Defaults to False.
  • name _type, optional_ - A name for this hanlder. Defaults to the function name.

recurse_transpile

def recurse_transpile(key,
var: VariableNode,
value: Any,
registry: TranspileRegistry,
in_list=0,
strict=False)

Recurse Transpile a variable according to a registry and its definition

Arguments:

  • key type - The key of the variable
  • var VariableNode - The variable definition node correspoin to this variable
  • value Any - The to transpile valued
  • registry TranspileRegistry - The transpile registry to use
  • in_list bool, optional - Recursive Parameter. That will be set to the list depth. Defaults to False.
  • strict bool, optional - Should we error on predicate errors. Defaults to False.

Raises:

  • TranspilationError - description

Returns:

  • Any - The transpiled value or the original value if no handler matched

transpile

def transpile(op: OperationDefinitionNode,
variables: Dict[str, Any],
registry: TranspileRegistry,
strict=False) -> Dict[str, Any]

Transpile

Transpiles a operations variabels to a dictionary of variables, with json serializable values according to a transpile registry.

Arguments:

  • op OperationDefinitionNode - The operation definition node,
  • registry TranspileRegistry - The registry
  • strict bool, optional - Should we fail if a handler predicate fails. Defaults to False.

Returns:

  • Dict - The transpiled variables
class TranspileLink(ParsingLink)

Transpile Link

Transpile Link is a link that transpiles variables according to a transpile registry.

Attributes:

  • registry TranspileRegistry - The transpile registry to use
  • strict bool - Should we fail if a handler predicate fails. Defaults to False.