Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into chore/m2m-throug…
Browse files Browse the repository at this point in the history
…h-docs
  • Loading branch information
Plebbimon committed Jan 15, 2024
2 parents 9e07fd0 + d3c6073 commit 52fbb6b
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 22 deletions.
2 changes: 1 addition & 1 deletion graphene_django_cud/mutations/batch_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init_subclass_with_meta__(
exclude_fields=(), # Deprecated in favor of `exclude`
optional_fields=(),
required_fields=(),
auto_context_fields={},
auto_context_fields=None,
return_field_name=None,
many_to_many_extras=None,
foreign_key_extras=None,
Expand Down
8 changes: 4 additions & 4 deletions graphene_django_cud/mutations/batch_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def __init_subclass_with_meta__(
cls,
_meta=None,
model=None,
optional_fields=None,
required_fields=None,
optional_fields=None,
required_fields=None,
type_name=None,
**kwargs,
):
Expand All @@ -35,8 +35,8 @@ def __init_subclass_with_meta__(
return super().__init_subclass_with_meta__(
_meta=_meta,
model=model,
optional_fields=optional_fields,
required_fields=required_fields,
optional_fields=optional_fields or (),
required_fields=required_fields or (),
type_name=input_type_name,
**kwargs,
)
2 changes: 1 addition & 1 deletion graphene_django_cud/mutations/batch_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init_subclass_with_meta__(
exclude_fields=(), # Deprecated in favor of `exclude`
optional_fields=(),
required_fields=(),
auto_context_fields={},
auto_context_fields=None,
return_field_name=None,
many_to_many_extras=None,
foreign_key_extras=None,
Expand Down
2 changes: 1 addition & 1 deletion graphene_django_cud/mutations/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init_subclass_with_meta__(
exclude_fields=(), # Deprecated in favor of `exclude`
optional_fields=(),
required_fields=(),
auto_context_fields={},
auto_context_fields=None,
return_field_name=None,
many_to_many_extras=None,
foreign_key_extras=None,
Expand Down
8 changes: 6 additions & 2 deletions graphene_django_cud/mutations/filter_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ def __init_subclass_with_meta__(
only_fields=(), # Deprecated in favor of `fields`
exclude=(),
exclude_fields=(), # Deprecated in favor of `exclude`
optional_fields=None,
optional_fields=None, # Explicitly defaulted to None here and handled below.
required_fields=(),
field_types=None,
auto_context_fields={},
auto_context_fields=None,
**kwargs,
):

registry = get_global_registry()
model_type = registry.get_type_for_model(model)

Expand All @@ -77,6 +78,9 @@ def __init_subclass_with_meta__(
DeprecationWarning,
)

if auto_context_fields is None:
auto_context_fields = {}

input_arguments = get_filter_fields_input_args(filter_fields, model)

FilterInputType = type(
Expand Down
8 changes: 4 additions & 4 deletions graphene_django_cud/mutations/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def __init_subclass_with_meta__(
cls,
_meta=None,
model=None,
optional_fields=None,
required_fields=None,
optional_fields=None,
required_fields=None,
type_name=None,
**kwargs,
):
Expand All @@ -35,8 +35,8 @@ def __init_subclass_with_meta__(
return super().__init_subclass_with_meta__(
_meta=_meta,
model=model,
optional_fields=optional_fields,
required_fields=required_fields,
optional_fields=optional_fields or (),
required_fields=required_fields or (),
type_name=input_type_name,
**kwargs,
)
16 changes: 15 additions & 1 deletion graphene_django_cud/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
from django.db import models
from factory.django import DjangoModelFactory

from graphene_django_cud.tests.models import User, Cat, Dog, Mouse, DogRegistration
from graphene_django_cud.tests.models import (
User,
Cat,
Dog,
Mouse,
DogRegistration,
Fish,
)


class UserFactory(DjangoModelFactory):
Expand Down Expand Up @@ -104,3 +111,10 @@ class Meta:
model = Mouse

name = "mouse"


class FishFactory(DjangoModelFactory):
class Meta:
model = Fish

name = "Koi"
26 changes: 26 additions & 0 deletions graphene_django_cud/tests/migrations/0009_auto_20231228_1921.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.2.20 on 2023-12-28 19:21

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

dependencies = [
('tests', '0008_auto_20220313_1242'),
]

operations = [
migrations.CreateModel(
name='Fish',
fields=[
('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)),
('name', models.CharField(max_length=40)),
],
),
migrations.AlterField(
model_name='user',
name='first_name',
field=models.CharField(blank=True, max_length=150, verbose_name='first name'),
),
]
6 changes: 6 additions & 0 deletions graphene_django_cud/tests/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import uuid
from django.contrib.auth.models import AbstractUser
from django.db import models

Expand Down Expand Up @@ -59,3 +60,8 @@ class CatUserRelation(models.Model):

class Meta:
unique_together = (("cat", "user"),)


class Fish(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
name = models.CharField(max_length=40, blank=False, null=False)
28 changes: 28 additions & 0 deletions graphene_django_cud/tests/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Mouse,
DogRegistration,
CatUserRelation,
Fish,
)


Expand Down Expand Up @@ -57,18 +58,26 @@ class Meta:
interfaces = (Node,)


class FishNode(DjangoObjectType):
class Meta:
model = Fish
interfaces = (Node,)


class Query(graphene.ObjectType):
user = Node.Field(UserNode)
cat = Node.Field(CatNode)
dog = Node.Field(DogNode)
mice = Node.Field(MouseNode)
cat_user_relation = Node.Field(CatUserRelationNode)
fish = Node.Field(FishNode)

all_users = DjangoConnectionField(UserNode)
all_cats = DjangoConnectionField(CatNode)
all_dogs = DjangoConnectionField(DogNode)
all_mice = DjangoConnectionField(MouseNode)
all_cat_user_relations = DjangoConnectionField(CatUserRelationNode)
all_fish = DjangoConnectionField(FishNode)


class CreateUserMutation(DjangoCreateMutation):
Expand Down Expand Up @@ -251,6 +260,21 @@ class Meta:
filter_fields = ("name", "name__startswith")


class CreateFishMutation(DjangoCreateMutation):
class Meta:
model = Fish


class UpdateFishMutation(DjangoUpdateMutation):
class Meta:
model = Fish


class DeleteFishMutation(DjangoDeleteMutation):
class Meta:
model = Fish


class Mutations(graphene.ObjectType):

create_user = CreateUserMutation.Field()
Expand Down Expand Up @@ -282,5 +306,9 @@ class Mutations(graphene.ObjectType):
delete_mouse = DeleteMouseMutation.Field()
batch_delete_mouse = FilterDeleteMouseMutation.Field()

create_fish = CreateFishMutation.Field()
update_fish = UpdateFishMutation.Field()
delete_fish = DeleteFishMutation.Field(0)


schema = Schema(query=Query, mutation=Mutations)
47 changes: 46 additions & 1 deletion graphene_django_cud/tests/test_create_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
UserFactory,
CatFactory,
DogFactory,
FishFactory,
)
from graphene_django_cud.tests.dummy_query import DummyQuery
from graphene_django_cud.tests.models import User, Cat, Dog, DogRegistration
from graphene_django_cud.tests.models import User, Cat, Dog, DogRegistration, Fish
from graphene_django_cud.util import disambiguate_id


Expand Down Expand Up @@ -736,3 +737,47 @@ class Mutations(graphene.ObjectType):
cat["catUserRelations"]["edges"][0]["node"]["user"]["id"],
to_global_id("UserNode", other_user.id),
)


class TestCreateUuidPk(TestCase):
def test__creating_a_record_with_uuid_pk(self):
# This register the FishNode type
from .schema import FishNode # noqa: F401

class CreateFishMutation(DjangoCreateMutation):
class Meta:
model = Fish

class Mutations(graphene.ObjectType):
create_fish = CreateFishMutation.Field()

user = UserFactory.create()
fish = FishFactory.build()

schema = Schema(query=DummyQuery, mutation=Mutations)
mutation = """
mutation CreateFish(
$input: CreateFishInput!
){
createFish(input: $input) {
fish {
id
name
}
}
}
"""

result = schema.execute(
mutation,
variables={
"input": {
"name": fish.name
}
},
context=Dict(user=user),
)
self.assertIsNone(result.errors)

data = Dict(result.data)
self.assertEqual(data.createFish.fish.name, fish.name)
41 changes: 40 additions & 1 deletion graphene_django_cud/tests/test_delete_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
UserWithPermissionsFactory,
CatFactory,
UserFactory,
FishFactory,
)
from graphene_django_cud.tests.dummy_query import DummyQuery
from graphene_django_cud.tests.models import Cat
from graphene_django_cud.tests.models import Cat, Fish
from graphene_django_cud.util import disambiguate_id


Expand Down Expand Up @@ -128,3 +129,41 @@ class Mutations(graphene.ObjectType):
)
self.assertIsNotNone(result.errors)
self.assertIn("Not permitted", str(result.errors))

def test__deleting_a_record_with_uuid_pk__with_pk_as_str(self):
# This register the FishNode type
from .schema import FishNode # noqa: F401

class DeleteFishMutation(DjangoDeleteMutation):
class Meta:
model = Fish

class Mutations(graphene.ObjectType):
delete_fish = DeleteFishMutation.Field()

user = UserFactory.create()
fish = FishFactory.create()

schema = Schema(query=DummyQuery, mutation=Mutations)
mutation = """
mutation DeleteFish(
$id: ID!
){
deleteFish(id: $id) {
found
deletedId
}
}
"""

# Excluded use of `to_global_id` and cast UUID to str to match some
# real-world mutation scenarios.
result = schema.execute(
mutation,
variables={
"id": str(fish.id)
},
context=Dict(user=user),
)
self.assertIsNone(result.errors)
self.assertEqual(Fish.objects.count(), 0)
Loading

0 comments on commit 52fbb6b

Please sign in to comment.