Skip to content

tOgg1/graphene-django-cud

Repository files navigation

Graphene Django CUD

Version Build status Documentation Status License

This package contains a number of helper mutations making it easy to construct create, update and delete mutations for django models.

The helper mutations are:

  • DjangoCreateMutation
  • DjangoPatchMutation
  • DjangoUpdateMutation
  • DjangoDeleteMutation
  • DjangoBatchCreateMutation
  • DjangoBatchPatchMutation
  • DjangoBatchUpdateMutation
  • DjangoBatchDeleteMutation
  • DjangoFilterUpdateMutation
  • DjangoFilterDeleteMutation

The package handles both regular ids and relay ids automatically.

Installation

pip install graphene_django_cud

Basic usage

To use, here illustrated by DjangoCreateMutation, simply create a new inherting class. Suppose we have the following model and Node.

class User(models.Model):
    name = models.CharField(max_length=255)
    address = models.TextField()

class UserNode(DjangoObjectType):
    class Meta:
        model = User
        interfaces = (Node,)

Then we can create a create mutation with the following schema

class CreateUserMutation(DjangoCreateMutation):
    class Meta:
        model = User


class Mutation(graphene.ObjectType):
    create_user = CreateUserMutation.Field()


class Query(graphene.ObjectType):
    user = graphene.Field(UserNode, id=graphene.String())

    def resolve_user(self, info, id):
        return User.objects.get(pk=id)


schema = Schema(query=Query, mutation=Mutation)

Note that the UserNode has to be registered as a field before the mutation is instantiated. This will be configurable in the future.

The input to the mutation is a single variable input which is automatically created with the models fields. An example mutation would then be

mutation {
    createUser(input: {name: "John Doe", address: "Downing Street 10"}){
        user{
            id
            name
            address
        }
    }
}

Documentation

The full documentation can be found at https://graphene-django-cud.readthedocs.io/en/latest/.