Skip to main content

Encapsulate your business logic in command classes.

Project description



Compose your business logic into commands that sanitize and validate input.


$ pip install mutations

How it Works:

  1. Subclass mutations.Mutation
  2. Define your inputs.
  3. Define an execute method in your command.
  4. Run it, like this:'bar')

To learn more, see this blog post.


import mutations

class UserSignup(mutations.Mutation):
    """Define the inputs to your mutation here. """
    email = mutations.fields.CharField(required=True)
    full_name = mutations.fields.CharField(required=True)
    send_welcome_email = mutations.fields.Boolean(required=False, default=True)

    def validate_email_address(self):
        """Custom validation for a field.

        If you encounter any validation errors and want to raise, you should
        raise mutation.ValidationError or some sublcass thereof. Otherwise, it
        assumes there were no problems.

        Any function beginning with `validate_` is assumed to be a validator
        function and will be run before the mutation can execute.
        if not
            raise mutations.ValidationError("email_not_valid", "Email is not valid.")

    def execute(self):
        """Executes the mutation.

        This method does the heavy lifting. You can call it by calling .run() on
        your mutation class.
        user = User.objects.create(, name=self.full_name)
        if self.send_welcome_email:
            EmailServer.deliver(recipient =
        return user

Calling Commands

>>> result =, full_name="Bob Boblob")
>>> result.success
>>> result.return_value
<User id=...>
>>> result.errors

result = ...
>>> result =
>>> result.success
>>> result.errors
  'email': ['email_not_valid']
>>> result.value


$ make tests


This project uses Semantic Versioning.


Thanks to Cypriss for the excellent Ruby Mutations Gem. I created this library because I was looking for something similar for Python.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mutations-0.3.1.tar.gz (5.3 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page