We are looking into moving away from graphene and strawberry looks like a very nice alternative!
Unfortunately we have some heavy integration with django-graphene-cud which offers a lot of useful features (some are too magic for my liking). I was unable to find a way to use this package in a way that offered us the hooks we needed for some of our mutations and ended up on a boilerplate heavy strawberry only PoC shown below.
Are we missing anything obvious with this library?
import strawberry
from typing import Optional
from django.db.models.query_utils import Q
from strawberry.permission import BasePermission
from strawberry.types.info import Info
from foo.graphql.types import FooNode
from foo.models import Foo
from project.graphql.context import PermissionsContext
class IsBar(BasePermission):
message = "User must be bar"
def has_permission(self, source, info: Info, **kwargs) -> bool:
return info.context.is_bar
@strawberry.interface
class FooFields:
code: Optional[str] = None
description: Optional[str] = None
@strawberry.input
class CreateFooInput(FooFields):
def save(self, info: Info):
fields = self.__dict__
fields["owner"] = info.context.user
foo = Foo.objects.create(**fields)
return foo
@strawberry.input
class PatchFooInput(FooFields):
def patch(self, info: Info, id: str):
fields = self.__dict__
permissions: PermissionsContext = info.context
filter = Q(owner=permissions.user)
if permissions.is_company_admin:
filter |= Q(owner__company_id=permissions.company_id)
queryset = Foo.objects.filter(id=id).filter(filter)
queryset.update(**fields)
return queryset.get()
@strawberry.type
class Mutation:
@strawberry.field(permission_classes=[IsEmployed])
def create_foo(self, info: Info, input: CreateFooInput) -> FooNode:
return input.save(info)
@strawberry.field(permission_classes=[IsEmployed])
def update_foo(self, info: Info, id: str, input: PatchFooInput) -> FooNode:
return input.patch(info, id)
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too