It would seem that mutations on proxy-models aren't working in my setup.
Can you see the mistake in my setup?
A simplified, reproducible example with details included:
Managers:
from django.db.models import Queryset, Manager
class ClientQueryset(Queryset):
def create(self, **kwargs):
kwargs.update({'is_client': True})
return super().create(**kwargs)
class ClientManager(Manager):
def get_queryset(self):
return ClientQueryset(self.model, using=self._db).\
filter(is_client=True)
Models:
from django.db import models
from .managers import ClientManger
class Company(models.Model):
name = models.CharField(max_length=100)
is_client = models.BooleanField(default=False)
class Client(Company):
objects = ClientManager()
class Meta:
proxy = True
Based on this setup, I'm perfectly capable of creating the client objects in the classic ORM way:
from contacts.models import Client
client = Client.objects.create(name='client orm')
print(client.is_client, client.name)
Next up, let's setup the strawberry mutations.
schema:
from strawberry import relay, auto
import strawberry
import strawberry_django
from typing import List
from .models import Company, Client
@strawberry_django.filter(Company)
class CompanyFilter:
id: auto
name: auto
@strawberry_django.filter(Client)
class ClientFilter:
id: auto
name: auto
@strawberry_django.input(Company, fields="__all__")
class CompanyInput:
pass
@strawberry_django.partial(Company, fields="__all__")
class CompanyPartialInput(strawberry_django.NodeInput):
pass
@strawberry_django.input(Client, fields="__all__")
class ClientInput:
pass
@strawberry_django.partial(Client, fields="__all__")
class ClientPartialInput(strawberry_django.NodeInput):
pass
@strawberry_django.order(Company)
class CompanyOrder:
name: auto
@strawberry_django.order(Client)
class ClientOrder:
name: auto
@strawberry_django.type(Company, filters=CompanyFilter, order=CompanyOrder, pagination=True, fields='__all__')
class CompanyType(strawberry.relay.Node):
pass
@strawberry_django.type(Client, filters=ClientFilter, order=ClientOrder, pagination=True, fields='__all__')
class ClientType(strawberry.relay.Node):
pass
@strawberry.type
class Mutation:
create_company: CompanyType = strawberry_django.mutations.create(CompanyInput)
create_companies: List[CompanyType] = strawberry_django.mutations.create(CompanyInput)
update_company: CompanyType = strawberry_django.mutations.update(CompanyPartialInput)
delete_company: CompanyType = strawberry_django.mutations.delete()
delete_companies: List[CompanyType] = strawberry_django.mutations.delete()
create_client: ClientType = strawberry_django.mutations.create(ClientInput)
create_client: List[ClientType] = strawberry_django.mutations.create(ClientInput)
update_client: ClientType = strawberry_django.mutations.update(ClientPartialInput)
delete_client: ClientType = strawberry_django.mutations.delete()
delete_client: List[ClientType] = strawberry_django.mutations.delete()
@strawberry.type
class Query:
company: CompanyType = strawberry_django.node()
companies: strawberry_django.relay.ListConnectionWithTotalCount[CompanyType] = strawberry_django.connection()
client: ClientType = strawberry_django.node()
clients: strawberry_django.relay.ListConnectionWithTotalCount[ClientType] = strawberry_django.connection()
schema = strawberry.Schema(
query=Query,
mutation=Mutation,
)
Now that the schema is setup, let's run some mutations:
First, the base model "Compay":
mutation createCompany{
createCompany(data:{name: "company"}){
name
isClient
}
}
yields, successfully
{
"data": {
"createCompany": {
"name": "company",
"isClient": false
}
}
}
Now, let's try the proxy-model:
mutation createClient{
createClient(data:{name: "client"}){
name
isClient
}
}
Yields, unsuccessfully:
{
"data": null,
"errors": [
{
"message": "Client matching query does not exist.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"createClient"
]
}
]
}
Can you see the mistake in my setup?
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