common.graphql.mutations
RelayModelSerializerMutationOptions
class RelayModelSerializerMutationOptions(MutationOptions)
RelayModelSerializerMutationOptions
is a class that defines options for RelayModelSerializerMutation
.
Inherits from: MutationOptions
Properties:
model_class
: The Django model class that the mutation operates on.serializer_class
: The serializer class that is used to serialize the input data.edge_class
: The edge class for the mutation.edge_field_name
: The name of the edge field for the mutation.return_field_name
: The name of the field that is returned in the mutation payload.
RelayModelSerializerMutation
class RelayModelSerializerMutation(ClientIDMutation)
RelayModelSerializerMutation
is a class that allows you to create GraphQL mutations for creating, updating, and
deleting Django models using a serializer.
get_serializer_kwargs
@classmethod
def get_serializer_kwargs(cls, root, info, **input)
Return the keyword arguments that are passed to the serializer when an instance is created, updated, or deleted.
Arguments:
root
: root value passed to the mutationinfo
: GraphQL ResolveInfo object passed to the mutationinput
: dictionary of input values passed to the mutation
Raises:
None
:NotImplementedError
: This method must be overridden in a subclass
mutate_and_get_payload
@classmethod
def mutate_and_get_payload(cls, root, info, **input)
Perform the mutation and return the payload.
Arguments:
root
: root value passed to the mutationinfo
: GraphQL ResolveInfo object passed to the mutationinput
: dictionary of input values passed to the mutation
Returns:
result of the mutation
perform_mutate
@classmethod
def perform_mutate(cls, serializer, info)
Perform the mutation with the provided serializer and return the result.
Arguments:
serializer
: Django Rest Framework serializer to use for the mutationinfo
: GraphQL ResolveInfo object passed to the mutation
Returns:
result of the mutation
CreateModelMutation
class CreateModelMutation(RelayModelSerializerMutation)
CreateModelMutation
is a Relay mutation class that inherits from RelayModelSerializerMutation
.
It is used to create a new object of a specified model in the database.
__init_subclass_with_meta__
@classmethod
def __init_subclass_with_meta__(cls,
serializer_class=None,
only_fields=(),
exclude=(),
convert_choices_to_enum=False,
edge_class=None,
edge_field_name=None,
return_field_name=None,
_meta=None,
**options)
Create a new subclass of a specified class with a specified metaclass. It is used in the CreateModelMutation
class to define a new subclass of RelayModelSerializerMutation
with additional meta information.
Arguments:
serializer_class
: specifies the serializer class to use for the mutationonly_fields
: tuple of fields to include in the mutationexclude
: tuple of fields to exclude from the mutatioconvert_choices_to_enum
: boolean that specifies whether to convert serializer choices to an enumedge_class
: edge class to use in the mutationedge_field_name
: name of the edge field in the mutationreturn_field_name
: name of the return field in the mutation_meta
: metaclass to use for the mutationRelayModelSerializerMutation
0: dictionary of additional options to use in the mutation
Returns:
None
get_serializer_kwargs
@classmethod
def get_serializer_kwargs(cls, root, info, **input)
The get_serializer_kwargs
class method is a utility method that is used to generate keyword arguments for a
serializer instance. It is used in the CreateModelMutation
class to generate keyword arguments for the
serializer instance used in the mutation.
Arguments:
root
: root value passed to the mutationinfo
: GraphQL ResolveInfo object passed to the mutationinput
: dictionary of input values passed to the mutation
Returns:
dictionary of keyword arguments for the serializer instance
UpdateModelMutation
class UpdateModelMutation(RelayModelSerializerMutation)
UpdateModelMutation
is a Relay mutation class that inherits from RelayModelSerializerMutation
.
It is used to update an object of a specified model in the database.
__init_subclass_with_meta__
@classmethod
def __init_subclass_with_meta__(cls,
serializer_class=None,
model_class=None,
only_fields=(),
exclude=(),
convert_choices_to_enum=False,
edge_class=None,
edge_field_name=None,
return_field_name=None,
require_id_field=True,
_meta=None,
**options)
Create a new subclass of a specified class with a specified metaclass. It is used in the UpdateModelMutation
class to define a new subclass of RelayModelSerializerMutation
with additional meta information.
Arguments:
serializer_class
: specifies the serializer class to use for the mutationonly_fields
: tuple of fields to include in the mutationexclude
: tuple of fields to exclude from the mutatioconvert_choices_to_enum
: boolean that specifies whether to convert serializer choices to an enumedge_class
: edge class to use in the mutationedge_field_name
: name of the edge field in the mutationreturn_field_name
: name of the return field in the mutation_meta
: metaclass to use for the mutationRelayModelSerializerMutation
0: dictionary of additional options to use in the mutation
Returns:
None
get_serializer_kwargs
@classmethod
def get_serializer_kwargs(cls, root, info, **input)
The get_serializer_kwargs
class method is a utility method that is used to generate keyword arguments for a
serializer instance. It is used in the UpdateModelMutation
class to generate keyword arguments for the
serializer instance used in the mutation.
Arguments:
root
: root value passed to the mutationinfo
: GraphQL ResolveInfo object passed to the mutationinput
: dictionary of input values passed to the mutation
Returns:
dictionary of keyword arguments for the serializer instance
get_object
@classmethod
def get_object(cls, model_class, root, info, **input)
The get_object
class method is a utility method that is used to retrieve an object of a specified model by
its ID. It is used in the UpdateModelMutation
class to retrieve the object to be updated.
Arguments:
model_class
: model class to use to retrieve the objectroot
: root value passed to the mutationinfo
: GraphQL ResolveInfo object passed to the mutationinput
: dictionary of input values passed to the mutation
Returns:
object of the specified model with the specified ID
get_queryset
@classmethod
def get_queryset(cls, model_class, root, info, **input)
Return a queryset of all objects of the given model_class
.
Arguments:
model_class
: model class for which the queryset is to be returnedroot
: root object of the mutationinfo
: GraphQL ResolveInfo object passed to the mutationinput
: dictionary of input values passed to the mutation
Returns:
queryset of all objects of the given model_class
get_available_fields
@classmethod
def get_available_fields(cls, input_fields: OrderedDict, only_fields: tuple,
exclude: tuple) -> set
Return a set of available fields for a GraphQL mutation
Arguments:
input_fields
: OrderedDict containing all the input fields for the GraphQL mutationonly_fields
: tuple of field names to include in the available fields setexclude
: tuple of field names to exclude from the available fields set
Returns:
set of available fields
SerializerMutation
class SerializerMutation(ClientIDMutation)
SerializerMutation
is a class that allows you to create GraphQL mutations that serialize and deserialize data
using Django Rest Framework serializers.
__init_subclass_with_meta__
@classmethod
def __init_subclass_with_meta__(cls,
lookup_field=None,
serializer_class=None,
model_class=None,
model_operations=("create", "update"),
only_fields=(),
exclude=(),
convert_choices_to_enum=True,
_meta=None,
**options)
Create a new subclass of a specified class with a specified metaclass.
Arguments:
lookup_field
: field used to retrieve an existing model instanceserializer_class
: specifies the serializer class to use for the mutationmodel_class
: Django model class to usemodel_operations
: model operations to support, defaults to('create', 'update')
.only_fields
: fields to include in the mutation, if not provided, all fields will be included.exclude
: the fields to exclude from the mutation, if not provided, no fields will be excluded.convert_choices_to_enum
: boolean that specifies whether to convert serializer choices to an enum_meta
: metaclass to use for the mutationoptions
: dictionary of additional options to use in the mutation
Returns:
None
get_object
@classmethod
def get_object(cls, model_class, info, *args, **kwargs)
Retrieve a single object of the given model_class
using the provided *args
and **kwargs
.
Arguments:
model_class
: model class for which to retrieve an objectinfo
: GraphQL ResolveInfo object passed to the mutationargs
: positional arguments to pass to theget_object_or_404
functionkwargs
: keyword arguments to pass to theget_object_or_404
function
Returns:
object of the given model_class
get_serializer_kwargs
@classmethod
def get_serializer_kwargs(cls, root, info, **input)
The get_serializer_kwargs
class method is a utility method that is used to generate keyword arguments for a
serializer instance.
Arguments:
root
: root value passed to the mutationinfo
: GraphQL ResolveInfo object passed to the mutationinput
: dictionary of input values passed to the mutation
Returns:
dictionary of keyword arguments for the serializer instance
mutate_and_get_payload
@classmethod
def mutate_and_get_payload(cls, root, info, **input)
Perform the mutation and returns its result as a payload.
Arguments:
root
: root value passed to the mutationinfo
: GraphQL ResolveInfo object passed to the mutationinput
: dictionary of input values passed to the mutation
Returns:
result of the mutation
perform_mutate
@classmethod
def perform_mutate(cls, serializer, info)
Perform the mutation with the provided serializer and return the result.
Arguments:
serializer
: Django Rest Framework serializer to use for the mutationinfo
: GraphQL ResolveInfo object passed to the mutation
Returns:
result of the mutation
DeleteModelMutationOptions
class DeleteModelMutationOptions(MutationOptions)
DeleteModelMutationOptions
is a subclass of MutationOptions
that provides additional options for a GraphQL
mutation that deletes a Django model instance.
DeleteModelMutation
class DeleteModelMutation(ClientIDMutation)
DeleteModelMutation
is a class that allows you to create GraphQL mutations that delete a Django model instance.
__init_subclass_with_meta__
@classmethod
def __init_subclass_with_meta__(cls,
registry=None,
model=None,
_meta=None,
**options)
Create a new subclass of a specified class with a specified metaclass.
Arguments:
registry
: registry of the subclassmodel
: model for which to retrieve an object_meta
: metaclass to use for the mutationoptions
: dictionary of additional options to use in the mutation
Returns:
None
get_object
@classmethod
def get_object(cls, id, **kwargs)
Retrieve a single instance of a model based on id
Arguments:
id
: id of the model instance to retrievekwargs
: additional keyword arguments
Returns:
instance of the model with the specified id
mutate_and_get_payload
@classmethod
def mutate_and_get_payload(cls, root, info, id)
Perform a mutation to delete a model instance
Arguments:
root
: root value passed to the mutationinfo
: GraphQL ResolveInfo object passed to the mutationid
: id of the model instance to delete
Returns:
instance of the DeleteModelMutation class that contains a list of deleted IDs