GraphQL API
Documentation
GraphQL Types — Examples
zmag.Type(**kwargs)
zmag.Model(_id=None, id=None)
dataclass
zmag.BaseType(**kwargs)
GraphQL Inputs — Examples
zmag.Input
zmag.Form(data=SimpleNamespace(), errors=list(), is_valid=False, next=None)
dataclass
Represents an Input
response.
This class extends zmag.Input
and provides methods to handle form data,
including cleaning and converting it to a dictionary format.
Attributes:
-
data
(Any
) –The input data stored in a
SimpleNamespace
. -
errors
(list
) –A list of errors associated with the form.
-
is_valid
(bool
) –Indicates whether the form data is valid.
Example:
class Form(zmag.Input): ...
async def mutation(form: Form):
print(form.input)
print(form.input.clean())
print(form.input.dict(True))
dict(clean=False)
Convert the form data to a dict
.
Parameters:
-
clean
(bool
, default:False
) –If
True
, exclude keys withUNSET
values from the dictionary.
Returns:
-
data
(dict
) –The input data as a dictionary, optionally cleaned of
UNSET
values.
clean()
Convert the form data to a SimpleNamespace
, excluding UNSET
values.
Returns:
-
data
(SimpleNamespace
) –The input data as a
SimpleNamespace
, cleaned ofUNSET
values.
zmag.input(prefix=None, suffix=None)
Creates a decorator
for Inputs
, optionally adding a common prefix or suffix.
This function extends zmag.Input
.
Parameters:
-
prefix
(str | list[str] | None
, default:None
) –The prefix to be added to each field in the class.
-
suffix
(str | list[str] | None
, default:None
) –The suffix to be added to each field in the class.
Returns:
-
decorator
(Callable
) –To wrap GraphQL
Input
types.
Example:
zmag.value(default=UNSET, required=False, regex=None, rules=None, clean=None, deprecation_reason=None)
Configuration options for the Input
value.
This function extends zmag.Input
.
Parameters:
-
default
(Any
, default:UNSET
) –The default value for the field.
-
required
(bool
, default:False
) –Indicates whether the field is mandatory.
-
regex
(dict | None
, default:None
) –A dictionary defining regular expressions for field validation.
-
rules
(list | None
, default:None
) –A list of validation rules to apply to the field.
-
clean
(ValueCleaner | None
, default:None
) –A callable used to configure preprocess of the value.
-
deprecation_reason
(str | None
, default:None
) –A message explaining why the field is deprecated.
Example:
GraphQL: (form: {x: "required_field", email: "demo@helloworld.com"})
class MyForm(zmag.Input):
# A required field with no default value,
# ensuring the field must be filled.
x: str = zmag.value(required=True)
# A field with a default value,
# optionally using a function for initialization.
y: str = zmag.value(default="Some Value")
y: str = zmag.value(default=lambda: "Some Value")
# A deprecated field "not for use",
# in the near future or at all.
z: str = zmag.value(deprecation_reason="Value is deprecated")
# A complex field setup demonstrating validation,
# cleaning, and transformation of input data.
email: str = zmag.value(
regex={
# A field with regex validation.
r"[\w\.-]+@[\w\.-]+": "Invalid email address"
},
rules=[
# Additional custom validation rules.
(lambda v: v.startswith("demo") or "Invalid input")
],
clean=zmag.clean(
regex=[
# Replace using regex in the cleaning phase.
(r"^hello", "hola"),
(r"com", "api"),
],
rules=[
# Apply custom rules after regex replacements.
(lambda v: v.upper())
],
),
)
zmag.clean(regex=None, rules=None)
Creates a ValueCleaner
configuration with regex filters and custom rules.
This function extends zmag.value
.
Parameters:
-
regex
(list | None
, default:None
) –A list of regex patterns for filtering input value.
-
rules
(list | None
, default:None
) –A list of rules (functions or lambdas) to apply to input value.
Returns:
-
config
(ValueCleaner
) –A dictionary containing
regex
andrules
for input value cleaning.
Example:
GraphQL Operations — Examples
zmag.gql(cls=None)
This class decorator transforms a Python class into GraphQL operations.
When applied to a class, @zmag.gql
interprets inner classes named Query
and Mutation
as containers for GraphQL query and mutation methods, respectively.
These methods can then be invoked as part of a GraphQL API.
Attributes:
-
Meta
(class
) –Defines metadata for the GraphQL class, such as the associated
app
andmodel
. -
Query
(class
) –Contains methods that represent GraphQL queries. Each method should be an
async
function and can return various data types. -
Mutation
(class
) –Contains methods that represent GraphQL mutations. Each method should be an
async
function and can accept input data for modifying server-side state.
Meta class Attributes:
-
app
(str | bool | None
): Specifies a prefix for the GraphQL field names. If set toNone
, the prefix is omitted, and the field names are based directly on the method names. -
model
(str | type | None
): When specified, prefixes the GraphQL field names with the model name. This helps in creating more descriptive and structured GraphQL schemas.
Tip
This decorator allows you to seamlessly integrate Python classes with a GraphQL API.
Example:
GraphQL Output — Examples
zmag.Edge: TypeAlias = Connection[T, JSON]
module-attribute
zmag.edge(edges=None, count=0, pages=0, has_next=False, has_previous=False, computed=None)
zmag.Mutation(item=None, many=field(default_factory=list), error=None, deleted=0)
dataclass
zmag.Error(field=None, type=None, text=None)
dataclass
A structured format for GraphQL error messages to be used in Errors
responses.