From 720db1f9874b2a1e2727a66b27c5a634f57bfaaf Mon Sep 17 00:00:00 2001 From: Kien Dang Date: Fri, 11 Aug 2023 14:51:59 +0800 Subject: [PATCH 01/12] Only release on pypi after tests pass (#1452) --- .github/workflows/deploy.yml | 7 ++++++- .github/workflows/lint.yml | 1 + .github/workflows/tests.yml | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 5d5ae2724..770a20abc 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -6,8 +6,13 @@ on: - 'v*' jobs: - build: + lint: + uses: ./.github/workflows/lint.yml + tests: + uses: ./.github/workflows/tests.yml + release: runs-on: ubuntu-latest + needs: [lint, tests] steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 920ecf0db..f21811e8f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -4,6 +4,7 @@ on: push: branches: ["main"] pull_request: + workflow_call: jobs: build: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 17876a2f3..ee3af6a92 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -4,6 +4,7 @@ on: push: branches: ["main"] pull_request: + workflow_call: jobs: build: From 0473f1a9a3d2d7507aee1330ac474c3584d767d8 Mon Sep 17 00:00:00 2001 From: Thomas Leonard <64223923+tcleonard@users.noreply.github.com> Date: Fri, 11 Aug 2023 17:24:58 +0200 Subject: [PATCH 02/12] fix: empty list is not an empty value for list filters even when a custom filtering method is provided (#1450) Co-authored-by: Thomas Leonard --- graphene_django/compat.py | 24 ++- .../filter/filters/array_filter.py | 23 +++ graphene_django/filter/filters/list_filter.py | 24 +++ graphene_django/filter/tests/conftest.py | 152 ++++++++------ .../tests/test_array_field_contains_filter.py | 14 +- .../tests/test_array_field_custom_filter.py | 186 ++++++++++++++++++ .../tests/test_array_field_exact_filter.py | 19 +- .../tests/test_array_field_overlap_filter.py | 14 +- .../filter/tests/test_typed_filter.py | 88 ++++++++- 9 files changed, 450 insertions(+), 94 deletions(-) create mode 100644 graphene_django/filter/tests/test_array_field_custom_filter.py diff --git a/graphene_django/compat.py b/graphene_django/compat.py index fde632aa4..b3d160a13 100644 --- a/graphene_django/compat.py +++ b/graphene_django/compat.py @@ -1,3 +1,6 @@ +import sys +from pathlib import PurePath + # For backwards compatibility, we import JSONField to have it available for import via # this compat module (https://github.com/graphql-python/graphene-django/issues/1428). # Django's JSONField is available in Django 3.2+ (the minimum version we support) @@ -19,4 +22,23 @@ def __init__(self, *args, **kwargs): RangeField, ) except ImportError: - IntegerRangeField, ArrayField, HStoreField, RangeField = (MissingType,) * 4 + IntegerRangeField, HStoreField, RangeField = (MissingType,) * 3 + + # For unit tests we fake ArrayField using JSONFields + if any( + PurePath(sys.argv[0]).match(p) + for p in [ + "**/pytest", + "**/py.test", + "**/pytest/__main__.py", + ] + ): + + class ArrayField(JSONField): + def __init__(self, *args, **kwargs): + if len(args) > 0: + self.base_field = args[0] + super().__init__(**kwargs) + + else: + ArrayField = MissingType diff --git a/graphene_django/filter/filters/array_filter.py b/graphene_django/filter/filters/array_filter.py index b6f4808ec..a2fccda3a 100644 --- a/graphene_django/filter/filters/array_filter.py +++ b/graphene_django/filter/filters/array_filter.py @@ -1,13 +1,36 @@ from django_filters.constants import EMPTY_VALUES +from django_filters.filters import FilterMethod from .typed_filter import TypedFilter +class ArrayFilterMethod(FilterMethod): + def __call__(self, qs, value): + if value is None: + return qs + return self.method(qs, self.f.field_name, value) + + class ArrayFilter(TypedFilter): """ Filter made for PostgreSQL ArrayField. """ + @TypedFilter.method.setter + def method(self, value): + """ + Override method setter so that in case a custom `method` is provided + (see documentation https://django-filter.readthedocs.io/en/stable/ref/filters.html#method), + it doesn't fall back to checking if the value is in `EMPTY_VALUES` (from the `__call__` method + of the `FilterMethod` class) and instead use our ArrayFilterMethod that consider empty lists as values. + + Indeed when providing a `method` the `filter` method below is overridden and replaced by `FilterMethod(self)` + which means that the validation of the empty value is made by the `FilterMethod.__call__` method instead. + """ + TypedFilter.method.fset(self, value) + if value is not None: + self.filter = ArrayFilterMethod(self) + def filter(self, qs, value): """ Override the default filter class to check first whether the list is diff --git a/graphene_django/filter/filters/list_filter.py b/graphene_django/filter/filters/list_filter.py index 6689877cb..db91409c2 100644 --- a/graphene_django/filter/filters/list_filter.py +++ b/graphene_django/filter/filters/list_filter.py @@ -1,12 +1,36 @@ +from django_filters.filters import FilterMethod + from .typed_filter import TypedFilter +class ListFilterMethod(FilterMethod): + def __call__(self, qs, value): + if value is None: + return qs + return self.method(qs, self.f.field_name, value) + + class ListFilter(TypedFilter): """ Filter that takes a list of value as input. It is for example used for `__in` filters. """ + @TypedFilter.method.setter + def method(self, value): + """ + Override method setter so that in case a custom `method` is provided + (see documentation https://django-filter.readthedocs.io/en/stable/ref/filters.html#method), + it doesn't fall back to checking if the value is in `EMPTY_VALUES` (from the `__call__` method + of the `FilterMethod` class) and instead use our ListFilterMethod that consider empty lists as values. + + Indeed when providing a `method` the `filter` method below is overridden and replaced by `FilterMethod(self)` + which means that the validation of the empty value is made by the `FilterMethod.__call__` method instead. + """ + TypedFilter.method.fset(self, value) + if value is not None: + self.filter = ListFilterMethod(self) + def filter(self, qs, value): """ Override the default filter class to check first whether the list is diff --git a/graphene_django/filter/tests/conftest.py b/graphene_django/filter/tests/conftest.py index 1556f5457..9f5d36667 100644 --- a/graphene_django/filter/tests/conftest.py +++ b/graphene_django/filter/tests/conftest.py @@ -1,4 +1,4 @@ -from unittest.mock import MagicMock +from functools import reduce import pytest from django.db import models @@ -25,15 +25,15 @@ ) -STORE = {"events": []} - - class Event(models.Model): name = models.CharField(max_length=50) tags = ArrayField(models.CharField(max_length=50)) tag_ids = ArrayField(models.IntegerField()) random_field = ArrayField(models.BooleanField()) + def __repr__(self): + return f"Event [{self.name}]" + @pytest.fixture def EventFilterSet(): @@ -48,6 +48,14 @@ class Meta: tags__contains = ArrayFilter(field_name="tags", lookup_expr="contains") tags__overlap = ArrayFilter(field_name="tags", lookup_expr="overlap") tags = ArrayFilter(field_name="tags", lookup_expr="exact") + tags__len = ArrayFilter( + field_name="tags", lookup_expr="len", input_type=graphene.Int + ) + tags__len__in = ArrayFilter( + field_name="tags", + method="tags__len__in_filter", + input_type=graphene.List(graphene.Int), + ) # Those are actually not usable and only to check type declarations tags_ids__contains = ArrayFilter(field_name="tag_ids", lookup_expr="contains") @@ -61,6 +69,14 @@ class Meta: ) random_field = ArrayFilter(field_name="random_field", lookup_expr="exact") + def tags__len__in_filter(self, queryset, _name, value): + if not value: + return queryset.none() + return reduce( + lambda q1, q2: q1.union(q2), + [queryset.filter(tags__len=v) for v in value], + ).distinct() + return EventFilterSet @@ -83,68 +99,94 @@ def Query(EventType): we are running unit tests in sqlite which does not have ArrayFields. """ + events = [ + Event(name="Live Show", tags=["concert", "music", "rock"]), + Event(name="Musical", tags=["movie", "music"]), + Event(name="Ballet", tags=["concert", "dance"]), + Event(name="Speech", tags=[]), + ] + class Query(graphene.ObjectType): events = DjangoFilterConnectionField(EventType) def resolve_events(self, info, **kwargs): - events = [ - Event(name="Live Show", tags=["concert", "music", "rock"]), - Event(name="Musical", tags=["movie", "music"]), - Event(name="Ballet", tags=["concert", "dance"]), - Event(name="Speech", tags=[]), - ] - - STORE["events"] = events - - m_queryset = MagicMock(spec=QuerySet) - m_queryset.model = Event - - def filter_events(**kwargs): - if "tags__contains" in kwargs: - STORE["events"] = list( - filter( - lambda e: set(kwargs["tags__contains"]).issubset( - set(e.tags) - ), - STORE["events"], + class FakeQuerySet(QuerySet): + def __init__(self, model=None): + self.model = Event + self.__store = list(events) + + def all(self): + return self + + def filter(self, **kwargs): + queryset = FakeQuerySet() + queryset.__store = list(self.__store) + if "tags__contains" in kwargs: + queryset.__store = list( + filter( + lambda e: set(kwargs["tags__contains"]).issubset( + set(e.tags) + ), + queryset.__store, + ) + ) + if "tags__overlap" in kwargs: + queryset.__store = list( + filter( + lambda e: not set(kwargs["tags__overlap"]).isdisjoint( + set(e.tags) + ), + queryset.__store, + ) ) - ) - if "tags__overlap" in kwargs: - STORE["events"] = list( - filter( - lambda e: not set(kwargs["tags__overlap"]).isdisjoint( - set(e.tags) - ), - STORE["events"], + if "tags__exact" in kwargs: + queryset.__store = list( + filter( + lambda e: set(kwargs["tags__exact"]) == set(e.tags), + queryset.__store, + ) ) - ) - if "tags__exact" in kwargs: - STORE["events"] = list( - filter( - lambda e: set(kwargs["tags__exact"]) == set(e.tags), - STORE["events"], + if "tags__len" in kwargs: + queryset.__store = list( + filter( + lambda e: len(e.tags) == kwargs["tags__len"], + queryset.__store, + ) ) - ) + return queryset + + def union(self, *args): + queryset = FakeQuerySet() + queryset.__store = self.__store + for arg in args: + queryset.__store += arg.__store + return queryset - def mock_queryset_filter(*args, **kwargs): - filter_events(**kwargs) - return m_queryset + def none(self): + queryset = FakeQuerySet() + queryset.__store = [] + return queryset - def mock_queryset_none(*args, **kwargs): - STORE["events"] = [] - return m_queryset + def count(self): + return len(self.__store) - def mock_queryset_count(*args, **kwargs): - return len(STORE["events"]) + def distinct(self): + queryset = FakeQuerySet() + queryset.__store = [] + for event in self.__store: + if event not in queryset.__store: + queryset.__store.append(event) + queryset.__store = sorted(queryset.__store, key=lambda e: e.name) + return queryset - m_queryset.all.return_value = m_queryset - m_queryset.filter.side_effect = mock_queryset_filter - m_queryset.none.side_effect = mock_queryset_none - m_queryset.count.side_effect = mock_queryset_count - m_queryset.__getitem__.side_effect = lambda index: STORE[ - "events" - ].__getitem__(index) + def __getitem__(self, index): + return self.__store[index] - return m_queryset + return FakeQuerySet() return Query + + +@pytest.fixture +def schema(Query): + return graphene.Schema(query=Query) diff --git a/graphene_django/filter/tests/test_array_field_contains_filter.py b/graphene_django/filter/tests/test_array_field_contains_filter.py index 4144614c7..52a9f2418 100644 --- a/graphene_django/filter/tests/test_array_field_contains_filter.py +++ b/graphene_django/filter/tests/test_array_field_contains_filter.py @@ -1,18 +1,14 @@ import pytest -from graphene import Schema - from ...compat import ArrayField, MissingType @pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist") -def test_array_field_contains_multiple(Query): +def test_array_field_contains_multiple(schema): """ Test contains filter on a array field of string. """ - schema = Schema(query=Query) - query = """ query { events (tags_Contains: ["concert", "music"]) { @@ -32,13 +28,11 @@ def test_array_field_contains_multiple(Query): @pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist") -def test_array_field_contains_one(Query): +def test_array_field_contains_one(schema): """ Test contains filter on a array field of string. """ - schema = Schema(query=Query) - query = """ query { events (tags_Contains: ["music"]) { @@ -59,13 +53,11 @@ def test_array_field_contains_one(Query): @pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist") -def test_array_field_contains_empty_list(Query): +def test_array_field_contains_empty_list(schema): """ Test contains filter on a array field of string. """ - schema = Schema(query=Query) - query = """ query { events (tags_Contains: []) { diff --git a/graphene_django/filter/tests/test_array_field_custom_filter.py b/graphene_django/filter/tests/test_array_field_custom_filter.py new file mode 100644 index 000000000..3fdb992a3 --- /dev/null +++ b/graphene_django/filter/tests/test_array_field_custom_filter.py @@ -0,0 +1,186 @@ +import pytest + +from ...compat import ArrayField, MissingType + + +@pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist") +def test_array_field_len_filter(schema): + query = """ + query { + events (tags_Len: 2) { + edges { + node { + name + } + } + } + } + """ + result = schema.execute(query) + assert not result.errors + assert result.data["events"]["edges"] == [ + {"node": {"name": "Musical"}}, + {"node": {"name": "Ballet"}}, + ] + + query = """ + query { + events (tags_Len: 0) { + edges { + node { + name + } + } + } + } + """ + result = schema.execute(query) + assert not result.errors + assert result.data["events"]["edges"] == [ + {"node": {"name": "Speech"}}, + ] + + query = """ + query { + events (tags_Len: 10) { + edges { + node { + name + } + } + } + } + """ + result = schema.execute(query) + assert not result.errors + assert result.data["events"]["edges"] == [] + + query = """ + query { + events (tags_Len: "2") { + edges { + node { + name + } + } + } + } + """ + result = schema.execute(query) + assert len(result.errors) == 1 + assert result.errors[0].message == 'Int cannot represent non-integer value: "2"' + + query = """ + query { + events (tags_Len: True) { + edges { + node { + name + } + } + } + } + """ + result = schema.execute(query) + assert len(result.errors) == 1 + assert result.errors[0].message == "Int cannot represent non-integer value: True" + + +@pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist") +def test_array_field_custom_filter(schema): + query = """ + query { + events (tags_Len_In: 2) { + edges { + node { + name + } + } + } + } + """ + result = schema.execute(query) + assert not result.errors + assert result.data["events"]["edges"] == [ + {"node": {"name": "Ballet"}}, + {"node": {"name": "Musical"}}, + ] + + query = """ + query { + events (tags_Len_In: [0, 2]) { + edges { + node { + name + } + } + } + } + """ + result = schema.execute(query) + assert not result.errors + assert result.data["events"]["edges"] == [ + {"node": {"name": "Ballet"}}, + {"node": {"name": "Musical"}}, + {"node": {"name": "Speech"}}, + ] + + query = """ + query { + events (tags_Len_In: [10]) { + edges { + node { + name + } + } + } + } + """ + result = schema.execute(query) + assert not result.errors + assert result.data["events"]["edges"] == [] + + query = """ + query { + events (tags_Len_In: []) { + edges { + node { + name + } + } + } + } + """ + result = schema.execute(query) + assert not result.errors + assert result.data["events"]["edges"] == [] + + query = """ + query { + events (tags_Len_In: "12") { + edges { + node { + name + } + } + } + } + """ + result = schema.execute(query) + assert len(result.errors) == 1 + assert result.errors[0].message == 'Int cannot represent non-integer value: "12"' + + query = """ + query { + events (tags_Len_In: True) { + edges { + node { + name + } + } + } + } + """ + result = schema.execute(query) + assert len(result.errors) == 1 + assert result.errors[0].message == "Int cannot represent non-integer value: True" diff --git a/graphene_django/filter/tests/test_array_field_exact_filter.py b/graphene_django/filter/tests/test_array_field_exact_filter.py index 10e32ef73..5cba19372 100644 --- a/graphene_django/filter/tests/test_array_field_exact_filter.py +++ b/graphene_django/filter/tests/test_array_field_exact_filter.py @@ -1,18 +1,14 @@ import pytest -from graphene import Schema - from ...compat import ArrayField, MissingType @pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist") -def test_array_field_exact_no_match(Query): +def test_array_field_exact_no_match(schema): """ Test exact filter on a array field of string. """ - schema = Schema(query=Query) - query = """ query { events (tags: ["concert", "music"]) { @@ -30,13 +26,11 @@ def test_array_field_exact_no_match(Query): @pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist") -def test_array_field_exact_match(Query): +def test_array_field_exact_match(schema): """ Test exact filter on a array field of string. """ - schema = Schema(query=Query) - query = """ query { events (tags: ["movie", "music"]) { @@ -56,13 +50,11 @@ def test_array_field_exact_match(Query): @pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist") -def test_array_field_exact_empty_list(Query): +def test_array_field_exact_empty_list(schema): """ Test exact filter on a array field of string. """ - schema = Schema(query=Query) - query = """ query { events (tags: []) { @@ -82,11 +74,10 @@ def test_array_field_exact_empty_list(Query): @pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist") -def test_array_field_filter_schema_type(Query): +def test_array_field_filter_schema_type(schema): """ Check that the type in the filter is an array field like on the object type. """ - schema = Schema(query=Query) schema_str = str(schema) assert ( @@ -112,6 +103,8 @@ def test_array_field_filter_schema_type(Query): "tags_Contains": "[String!]", "tags_Overlap": "[String!]", "tags": "[String!]", + "tags_Len": "Int", + "tags_Len_In": "[Int]", "tagsIds_Contains": "[Int!]", "tagsIds_Overlap": "[Int!]", "tagsIds": "[Int!]", diff --git a/graphene_django/filter/tests/test_array_field_overlap_filter.py b/graphene_django/filter/tests/test_array_field_overlap_filter.py index 5ce1576b3..95d339c3b 100644 --- a/graphene_django/filter/tests/test_array_field_overlap_filter.py +++ b/graphene_django/filter/tests/test_array_field_overlap_filter.py @@ -1,18 +1,14 @@ import pytest -from graphene import Schema - from ...compat import ArrayField, MissingType @pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist") -def test_array_field_overlap_multiple(Query): +def test_array_field_overlap_multiple(schema): """ Test overlap filter on a array field of string. """ - schema = Schema(query=Query) - query = """ query { events (tags_Overlap: ["concert", "music"]) { @@ -34,13 +30,11 @@ def test_array_field_overlap_multiple(Query): @pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist") -def test_array_field_overlap_one(Query): +def test_array_field_overlap_one(schema): """ Test overlap filter on a array field of string. """ - schema = Schema(query=Query) - query = """ query { events (tags_Overlap: ["music"]) { @@ -61,13 +55,11 @@ def test_array_field_overlap_one(Query): @pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist") -def test_array_field_overlap_empty_list(Query): +def test_array_field_overlap_empty_list(schema): """ Test overlap filter on a array field of string. """ - schema = Schema(query=Query) - query = """ query { events (tags_Overlap: []) { diff --git a/graphene_django/filter/tests/test_typed_filter.py b/graphene_django/filter/tests/test_typed_filter.py index 084affada..b155385d5 100644 --- a/graphene_django/filter/tests/test_typed_filter.py +++ b/graphene_django/filter/tests/test_typed_filter.py @@ -1,4 +1,8 @@ +import operator +from functools import reduce + import pytest +from django.db.models import Q from django_filters import FilterSet import graphene @@ -44,6 +48,10 @@ class Meta: only_first = TypedFilter( input_type=graphene.Boolean, method="only_first_filter" ) + headline_search = ListFilter( + method="headline_search_filter", + input_type=graphene.List(graphene.String), + ) def first_n_filter(self, queryset, _name, value): return queryset[:value] @@ -54,6 +62,13 @@ def only_first_filter(self, queryset, _name, value): else: return queryset + def headline_search_filter(self, queryset, _name, value): + if not value: + return queryset.none() + return queryset.filter( + reduce(operator.or_, [Q(headline__icontains=v) for v in value]) + ) + class ArticleType(DjangoObjectType): class Meta: model = Article @@ -87,6 +102,7 @@ def test_typed_filter_schema(schema): "lang_InStr": "[String]", "firstN": "Int", "onlyFirst": "Boolean", + "headlineSearch": "[String]", } all_articles_filters = ( @@ -104,6 +120,40 @@ def test_typed_filters_work(schema): Article.objects.create(headline="A", reporter=reporter, editor=reporter, lang="es") Article.objects.create(headline="B", reporter=reporter, editor=reporter, lang="es") Article.objects.create(headline="C", reporter=reporter, editor=reporter, lang="en") + Article.objects.create(headline="AB", reporter=reporter, editor=reporter, lang="es") + + query = 'query { articles (lang_Contains: "n") { edges { node { headline } } } }' + + result = schema.execute(query) + assert not result.errors + assert result.data["articles"]["edges"] == [ + {"node": {"headline": "C"}}, + ] + + query = "query { articles (firstN: 2) { edges { node { headline } } } }" + + result = schema.execute(query) + assert not result.errors + assert result.data["articles"]["edges"] == [ + {"node": {"headline": "A"}}, + {"node": {"headline": "AB"}}, + ] + + query = "query { articles (onlyFirst: true) { edges { node { headline } } } }" + + result = schema.execute(query) + assert not result.errors + assert result.data["articles"]["edges"] == [ + {"node": {"headline": "A"}}, + ] + + +def test_list_filters_work(schema): + reporter = Reporter.objects.create(first_name="John", last_name="Doe", email="") + Article.objects.create(headline="A", reporter=reporter, editor=reporter, lang="es") + Article.objects.create(headline="B", reporter=reporter, editor=reporter, lang="es") + Article.objects.create(headline="C", reporter=reporter, editor=reporter, lang="en") + Article.objects.create(headline="AB", reporter=reporter, editor=reporter, lang="es") query = "query { articles (lang_In: [ES]) { edges { node { headline } } } }" @@ -111,6 +161,7 @@ def test_typed_filters_work(schema): assert not result.errors assert result.data["articles"]["edges"] == [ {"node": {"headline": "A"}}, + {"node": {"headline": "AB"}}, {"node": {"headline": "B"}}, ] @@ -120,30 +171,61 @@ def test_typed_filters_work(schema): assert not result.errors assert result.data["articles"]["edges"] == [ {"node": {"headline": "A"}}, + {"node": {"headline": "AB"}}, {"node": {"headline": "B"}}, ] - query = 'query { articles (lang_Contains: "n") { edges { node { headline } } } }' + query = "query { articles (lang_InStr: []) { edges { node { headline } } } }" + + result = schema.execute(query) + assert not result.errors + assert result.data["articles"]["edges"] == [] + + query = "query { articles (lang_InStr: null) { edges { node { headline } } } }" result = schema.execute(query) assert not result.errors assert result.data["articles"]["edges"] == [ + {"node": {"headline": "A"}}, + {"node": {"headline": "AB"}}, + {"node": {"headline": "B"}}, {"node": {"headline": "C"}}, ] - query = "query { articles (firstN: 2) { edges { node { headline } } } }" + query = 'query { articles (headlineSearch: ["a", "B"]) { edges { node { headline } } } }' result = schema.execute(query) assert not result.errors assert result.data["articles"]["edges"] == [ {"node": {"headline": "A"}}, + {"node": {"headline": "AB"}}, {"node": {"headline": "B"}}, ] - query = "query { articles (onlyFirst: true) { edges { node { headline } } } }" + query = "query { articles (headlineSearch: []) { edges { node { headline } } } }" + + result = schema.execute(query) + assert not result.errors + assert result.data["articles"]["edges"] == [] + + query = "query { articles (headlineSearch: null) { edges { node { headline } } } }" + + result = schema.execute(query) + assert not result.errors + assert result.data["articles"]["edges"] == [ + {"node": {"headline": "A"}}, + {"node": {"headline": "AB"}}, + {"node": {"headline": "B"}}, + {"node": {"headline": "C"}}, + ] + + query = 'query { articles (headlineSearch: [""]) { edges { node { headline } } } }' result = schema.execute(query) assert not result.errors assert result.data["articles"]["edges"] == [ {"node": {"headline": "A"}}, + {"node": {"headline": "AB"}}, + {"node": {"headline": "B"}}, + {"node": {"headline": "C"}}, ] From e49a01c1899639f4a4c142694dfb2e6d62741555 Mon Sep 17 00:00:00 2001 From: mahmoudmostafa0 <10292198+mahmoudmostafa0@users.noreply.github.com> Date: Mon, 28 Aug 2023 00:15:35 +0300 Subject: [PATCH 03/12] adding optional_field in Serializermutation to enfore some fields to be optional (#1455) * adding optional_fields to enforce fields to be optional * adding support for all * adding unit tests * Update graphene_django/rest_framework/mutation.py Co-authored-by: Kien Dang * linting * linting * add missing import --------- Co-authored-by: Kien Dang --- graphene_django/rest_framework/mutation.py | 10 +++++++++- .../rest_framework/serializer_converter.py | 9 +++++++-- .../rest_framework/tests/test_mutation.py | 12 +++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/graphene_django/rest_framework/mutation.py b/graphene_django/rest_framework/mutation.py index 9423d4f60..47e71861b 100644 --- a/graphene_django/rest_framework/mutation.py +++ b/graphene_django/rest_framework/mutation.py @@ -19,6 +19,7 @@ class SerializerMutationOptions(MutationOptions): model_class = None model_operations = ["create", "update"] serializer_class = None + optional_fields = () def fields_for_serializer( @@ -28,6 +29,7 @@ def fields_for_serializer( is_input=False, convert_choices_to_enum=True, lookup_field=None, + optional_fields=(), ): fields = OrderedDict() for name, field in serializer.fields.items(): @@ -48,9 +50,13 @@ def fields_for_serializer( if is_not_in_only or is_excluded: continue + is_optional = name in optional_fields or "__all__" in optional_fields fields[name] = convert_serializer_field( - field, is_input=is_input, convert_choices_to_enum=convert_choices_to_enum + field, + is_input=is_input, + convert_choices_to_enum=convert_choices_to_enum, + force_optional=is_optional, ) return fields @@ -74,6 +80,7 @@ def __init_subclass_with_meta__( exclude_fields=(), convert_choices_to_enum=True, _meta=None, + optional_fields=(), **options ): if not serializer_class: @@ -98,6 +105,7 @@ def __init_subclass_with_meta__( is_input=True, convert_choices_to_enum=convert_choices_to_enum, lookup_field=lookup_field, + optional_fields=optional_fields, ) output_fields = fields_for_serializer( serializer, diff --git a/graphene_django/rest_framework/serializer_converter.py b/graphene_django/rest_framework/serializer_converter.py index 328c46fd2..51695c5d0 100644 --- a/graphene_django/rest_framework/serializer_converter.py +++ b/graphene_django/rest_framework/serializer_converter.py @@ -18,7 +18,9 @@ def get_graphene_type_from_serializer_field(field): ) -def convert_serializer_field(field, is_input=True, convert_choices_to_enum=True): +def convert_serializer_field( + field, is_input=True, convert_choices_to_enum=True, force_optional=False +): """ Converts a django rest frameworks field to a graphql field and marks the field as required if we are creating an input type @@ -31,7 +33,10 @@ def convert_serializer_field(field, is_input=True, convert_choices_to_enum=True) graphql_type = get_graphene_type_from_serializer_field(field) args = [] - kwargs = {"description": field.help_text, "required": is_input and field.required} + kwargs = { + "description": field.help_text, + "required": is_input and field.required and not force_optional, + } # if it is a tuple or a list it means that we are returning # the graphql type and the child type diff --git a/graphene_django/rest_framework/tests/test_mutation.py b/graphene_django/rest_framework/tests/test_mutation.py index bfe53cc9a..58bc4ce6c 100644 --- a/graphene_django/rest_framework/tests/test_mutation.py +++ b/graphene_django/rest_framework/tests/test_mutation.py @@ -3,7 +3,7 @@ from pytest import raises from rest_framework import serializers -from graphene import Field, ResolveInfo +from graphene import Field, ResolveInfo, String from graphene.types.inputobjecttype import InputObjectType from ...types import DjangoObjectType @@ -105,6 +105,16 @@ class Meta: assert "created" not in MyMutation.Input._meta.fields +def test_model_serializer_optional_fields(): + class MyMutation(SerializerMutation): + class Meta: + serializer_class = MyModelSerializer + optional_fields = ("cool_name",) + + assert "cool_name" in MyMutation.Input._meta.fields + assert MyMutation.Input._meta.fields["cool_name"].type == String + + def test_write_only_field(): class WriteOnlyFieldModelSerializer(serializers.ModelSerializer): password = serializers.CharField(write_only=True) From 67def2e074c3c03f0fedff2416efe950bb44aefa Mon Sep 17 00:00:00 2001 From: lilac-supernova-2 <143229315+lilac-supernova-2@users.noreply.github.com> Date: Wed, 6 Sep 2023 03:29:58 -0400 Subject: [PATCH 04/12] Typo fixes (#1459) * Fix Star Wars spaceship name * Fix some typos in comments * Typo fixes * More typo fixes --- docs/settings.rst | 2 +- examples/cookbook/dummy_data.json | 2 +- examples/starwars/data.py | 2 +- examples/starwars/tests/test_mutation.py | 2 +- graphene_django/filter/tests/conftest.py | 2 +- graphene_django/filter/tests/test_fields.py | 4 ++-- graphene_django/filter/utils.py | 2 +- graphene_django/forms/types.py | 4 ++-- graphene_django/tests/models.py | 2 +- 9 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/settings.rst b/docs/settings.rst index d38d0c9c8..20cf04100 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -6,7 +6,7 @@ Graphene-Django can be customised using settings. This page explains each settin Usage ----- -Add settings to your Django project by creating a Dictonary with name ``GRAPHENE`` in the project's ``settings.py``: +Add settings to your Django project by creating a Dictionary with name ``GRAPHENE`` in the project's ``settings.py``: .. code:: python diff --git a/examples/cookbook/dummy_data.json b/examples/cookbook/dummy_data.json index c585bfcdf..c661846c2 100644 --- a/examples/cookbook/dummy_data.json +++ b/examples/cookbook/dummy_data.json @@ -231,7 +231,7 @@ "fields": { "category": 3, "name": "Newt", - "notes": "Braised and Confuesd" + "notes": "Braised and Confused" }, "model": "ingredients.ingredient", "pk": 5 diff --git a/examples/starwars/data.py b/examples/starwars/data.py index 6bdbf579c..bfac78b19 100644 --- a/examples/starwars/data.py +++ b/examples/starwars/data.py @@ -28,7 +28,7 @@ def initialize(): # Yeah, technically it's Corellian. But it flew in the service of the rebels, # so for the purposes of this demo it's a rebel ship. - falcon = Ship(id="4", name="Millenium Falcon", faction=rebels) + falcon = Ship(id="4", name="Millennium Falcon", faction=rebels) falcon.save() homeOne = Ship(id="5", name="Home One", faction=rebels) diff --git a/examples/starwars/tests/test_mutation.py b/examples/starwars/tests/test_mutation.py index e24bf8ae1..46b8fc351 100644 --- a/examples/starwars/tests/test_mutation.py +++ b/examples/starwars/tests/test_mutation.py @@ -40,7 +40,7 @@ def test_mutations(): {"node": {"id": "U2hpcDox", "name": "X-Wing"}}, {"node": {"id": "U2hpcDoy", "name": "Y-Wing"}}, {"node": {"id": "U2hpcDoz", "name": "A-Wing"}}, - {"node": {"id": "U2hpcDo0", "name": "Millenium Falcon"}}, + {"node": {"id": "U2hpcDo0", "name": "Millennium Falcon"}}, {"node": {"id": "U2hpcDo1", "name": "Home One"}}, {"node": {"id": "U2hpcDo5", "name": "Peter"}}, ] diff --git a/graphene_django/filter/tests/conftest.py b/graphene_django/filter/tests/conftest.py index 9f5d36667..a4097b183 100644 --- a/graphene_django/filter/tests/conftest.py +++ b/graphene_django/filter/tests/conftest.py @@ -44,7 +44,7 @@ class Meta: "name": ["exact", "contains"], } - # Those are actually usable with our Query fixture bellow + # Those are actually usable with our Query fixture below tags__contains = ArrayFilter(field_name="tags", lookup_expr="contains") tags__overlap = ArrayFilter(field_name="tags", lookup_expr="overlap") tags = ArrayFilter(field_name="tags", lookup_expr="exact") diff --git a/graphene_django/filter/tests/test_fields.py b/graphene_django/filter/tests/test_fields.py index df3b97acb..b9c8df465 100644 --- a/graphene_django/filter/tests/test_fields.py +++ b/graphene_django/filter/tests/test_fields.py @@ -789,7 +789,7 @@ class Query(ObjectType): query = """ query NodeFilteringQuery { - allReporters(orderBy: "-firtsnaMe") { + allReporters(orderBy: "-firstname") { edges { node { firstName @@ -802,7 +802,7 @@ class Query(ObjectType): assert result.errors -def test_order_by_is_perserved(): +def test_order_by_is_preserved(): class ReporterType(DjangoObjectType): class Meta: model = Reporter diff --git a/graphene_django/filter/utils.py b/graphene_django/filter/utils.py index 3dd835fc3..339bd48f9 100644 --- a/graphene_django/filter/utils.py +++ b/graphene_django/filter/utils.py @@ -43,7 +43,7 @@ def get_filtering_args_from_filterset(filterset_class, type): isinstance(filter_field, TypedFilter) and filter_field.input_type is not None ): - # First check if the filter input type has been explicitely given + # First check if the filter input type has been explicitly given field_type = filter_field.input_type else: if name not in filterset_class.declared_filters or isinstance( diff --git a/graphene_django/forms/types.py b/graphene_django/forms/types.py index b370afd84..0e311e5d6 100644 --- a/graphene_django/forms/types.py +++ b/graphene_django/forms/types.py @@ -4,7 +4,7 @@ from graphene.utils.str_converters import to_camel_case from ..converter import BlankValueField -from ..types import ErrorType # noqa Import ErrorType for backwards compatability +from ..types import ErrorType # noqa Import ErrorType for backwards compatibility from .mutation import fields_for_form @@ -60,7 +60,7 @@ def mutate(_root, _info, data): and isinstance(object_type._meta.fields[name], BlankValueField) ): # Field type BlankValueField here means that field - # with choises have been converted to enum + # with choices have been converted to enum # (BlankValueField is using only for that task ?) setattr(cls, name, cls.get_enum_cnv_cls_instance(name, object_type)) elif ( diff --git a/graphene_django/tests/models.py b/graphene_django/tests/models.py index 4afbbbce7..67e266731 100644 --- a/graphene_django/tests/models.py +++ b/graphene_django/tests/models.py @@ -97,7 +97,7 @@ class Meta: class APNewsReporter(Reporter): """ - This class only inherits from Reporter for testing multi table inheritence + This class only inherits from Reporter for testing multi table inheritance similar to what you'd see in django-polymorphic """ From ee7560f62949f300984927c1640bb2c1ebbde4c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20L=C3=A9tendart?= Date: Wed, 13 Sep 2023 08:49:01 +0200 Subject: [PATCH 05/12] Support displaying deprecated input fields in GraphiQL docs (#1458) * Update GraphiQL docs URL in docs/settings And deduplicate link declaration. * Support displaying deprecated input fields in GraphiQL docs --- docs/settings.rst | 39 ++++++++++++++++--- graphene_django/settings.py | 1 + .../static/graphene_django/graphiql.js | 1 + .../templates/graphene/graphiql.html | 1 + graphene_django/views.py | 1 + 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/docs/settings.rst b/docs/settings.rst index 20cf04100..e5f0faf25 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -197,9 +197,6 @@ Set to ``False`` if you want to disable GraphiQL headers editor tab for some rea This setting is passed to ``headerEditorEnabled`` GraphiQL options, for details refer to GraphiQLDocs_. -.. _GraphiQLDocs: https://github.com/graphql/graphiql/tree/main/packages/graphiql#options - - Default: ``True`` .. code:: python @@ -230,8 +227,6 @@ Set to ``True`` if you want to persist GraphiQL headers after refreshing the pag This setting is passed to ``shouldPersistHeaders`` GraphiQL options, for details refer to GraphiQLDocs_. -.. _GraphiQLDocs: https://github.com/graphql/graphiql/tree/main/packages/graphiql#options - Default: ``False`` @@ -240,3 +235,37 @@ Default: ``False`` GRAPHENE = { 'GRAPHIQL_SHOULD_PERSIST_HEADERS': False, } + + +``GRAPHIQL_INPUT_VALUE_DEPRECATION`` +------------------------------------ + +Set to ``True`` if you want GraphiQL to show any deprecated fields on input object types' docs. + +For example, having this schema: + +.. code:: python + + class MyMutationInputType(graphene.InputObjectType): + old_field = graphene.String(deprecation_reason="You should now use 'newField' instead.") + new_field = graphene.String() + + class MyMutation(graphene.Mutation): + class Arguments: + input = types.MyMutationInputType() + +GraphiQL will add a ``Show Deprecated Fields`` button to toggle information display on ``oldField`` and its deprecation +reason. Otherwise, you would get neither a button nor any information at all on ``oldField``. + +This setting is passed to ``inputValueDeprecation`` GraphiQL options, for details refer to GraphiQLDocs_. + +Default: ``False`` + +.. code:: python + + GRAPHENE = { + 'GRAPHIQL_INPUT_VALUE_DEPRECATION': False, + } + + +.. _GraphiQLDocs: https://graphiql-test.netlify.app/typedoc/modules/graphiql_react#graphiqlprovider-2 diff --git a/graphene_django/settings.py b/graphene_django/settings.py index d0ef16cf8..de2c52163 100644 --- a/graphene_django/settings.py +++ b/graphene_django/settings.py @@ -40,6 +40,7 @@ # https://github.com/graphql/graphiql/tree/main/packages/graphiql#options "GRAPHIQL_HEADER_EDITOR_ENABLED": True, "GRAPHIQL_SHOULD_PERSIST_HEADERS": False, + "GRAPHIQL_INPUT_VALUE_DEPRECATION": False, "ATOMIC_MUTATIONS": False, "TESTING_ENDPOINT": "/graphql", } diff --git a/graphene_django/static/graphene_django/graphiql.js b/graphene_django/static/graphene_django/graphiql.js index 901c9910b..737c42227 100644 --- a/graphene_django/static/graphene_django/graphiql.js +++ b/graphene_django/static/graphene_django/graphiql.js @@ -122,6 +122,7 @@ onEditOperationName: onEditOperationName, isHeadersEditorEnabled: GRAPHENE_SETTINGS.graphiqlHeaderEditorEnabled, shouldPersistHeaders: GRAPHENE_SETTINGS.graphiqlShouldPersistHeaders, + inputValueDeprecation: GRAPHENE_SETTINGS.graphiqlInputValueDeprecation, query: query, }; if (parameters.variables) { diff --git a/graphene_django/templates/graphene/graphiql.html b/graphene_django/templates/graphene/graphiql.html index 52421e868..8a4c3b6a1 100644 --- a/graphene_django/templates/graphene/graphiql.html +++ b/graphene_django/templates/graphene/graphiql.html @@ -54,6 +54,7 @@ {% endif %} graphiqlHeaderEditorEnabled: {{ graphiql_header_editor_enabled|yesno:"true,false" }}, graphiqlShouldPersistHeaders: {{ graphiql_should_persist_headers|yesno:"true,false" }}, + graphiqlInputValueDeprecation: {{ graphiql_input_value_deprecation|yesno:"true,false" }}, }; diff --git a/graphene_django/views.py b/graphene_django/views.py index ce08d26a9..71c087b3c 100644 --- a/graphene_django/views.py +++ b/graphene_django/views.py @@ -172,6 +172,7 @@ def dispatch(self, request, *args, **kwargs): # GraphiQL headers tab, graphiql_header_editor_enabled=graphene_settings.GRAPHIQL_HEADER_EDITOR_ENABLED, graphiql_should_persist_headers=graphene_settings.GRAPHIQL_SHOULD_PERSIST_HEADERS, + graphiql_input_value_deprecation=graphene_settings.GRAPHIQL_INPUT_VALUE_DEPRECATION, ) if self.batch: From 83d3d27f145a43a95cdedf4776c8aa8d6ee1f6a7 Mon Sep 17 00:00:00 2001 From: mnasiri Date: Wed, 13 Sep 2023 19:56:18 +0330 Subject: [PATCH 06/12] Fix graphiql explorer styles by sending graphiql_plugin_explorer_css_sri param to render_graphiql function of the GraphQlView (#1418) (#1460) --- graphene_django/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/graphene_django/views.py b/graphene_django/views.py index 71c087b3c..c6090b08c 100644 --- a/graphene_django/views.py +++ b/graphene_django/views.py @@ -167,6 +167,7 @@ def dispatch(self, request, *args, **kwargs): subscriptions_transport_ws_sri=self.subscriptions_transport_ws_sri, graphiql_plugin_explorer_version=self.graphiql_plugin_explorer_version, graphiql_plugin_explorer_sri=self.graphiql_plugin_explorer_sri, + graphiql_plugin_explorer_css_sri=self.graphiql_plugin_explorer_css_sri, # The SUBSCRIPTION_PATH setting. subscription_path=self.subscription_path, # GraphiQL headers tab, From e8f36b018db2db9b8fba980b5a2b2a680f851e16 Mon Sep 17 00:00:00 2001 From: Kien Dang Date: Mon, 18 Sep 2023 22:23:53 +0700 Subject: [PATCH 07/12] Fix test Client headers for Django 4.2 (#1465) * Fix test Client headers for Django 4.2 * Lazy import pkg_resources since it could be quite heavy * Remove use of pkg_resources altogether --- graphene_django/utils/testing.py | 9 ++++++++- graphene_django/utils/utils.py | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/graphene_django/utils/testing.py b/graphene_django/utils/testing.py index ad9ff35f8..6cd0e3ba3 100644 --- a/graphene_django/utils/testing.py +++ b/graphene_django/utils/testing.py @@ -4,6 +4,7 @@ from django.test import Client, TestCase, TransactionTestCase from graphene_django.settings import graphene_settings +from graphene_django.utils.utils import _DJANGO_VERSION_AT_LEAST_4_2 DEFAULT_GRAPHQL_URL = "/graphql" @@ -55,8 +56,14 @@ def graphql_query( else: body["variables"] = {"input": input_data} if headers: + header_params = ( + {"headers": headers} if _DJANGO_VERSION_AT_LEAST_4_2 else headers + ) resp = client.post( - graphql_url, json.dumps(body), content_type="application/json", **headers + graphql_url, + json.dumps(body), + content_type="application/json", + **header_params ) else: resp = client.post( diff --git a/graphene_django/utils/utils.py b/graphene_django/utils/utils.py index d7993e7b2..364eff9b4 100644 --- a/graphene_django/utils/utils.py +++ b/graphene_django/utils/utils.py @@ -1,5 +1,6 @@ import inspect +import django from django.db import connection, models, transaction from django.db.models.manager import Manager from django.utils.encoding import force_str @@ -145,3 +146,8 @@ def bypass_get_queryset(resolver): """ resolver._bypass_get_queryset = True return resolver + + +_DJANGO_VERSION_AT_LEAST_4_2 = django.VERSION[0] > 4 or ( + django.VERSION[0] >= 4 and django.VERSION[1] >= 2 +) From 36cf100e8bd0dbb9cf0f1aa252377acc21c54679 Mon Sep 17 00:00:00 2001 From: Kien Dang Date: Wed, 25 Oct 2023 16:33:00 +0800 Subject: [PATCH 08/12] Use ruff format to replace black (#1473) * Use ruff format to replace black * Adjust ruff config to be compatible with ruff-format https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules * Format * Replace black with ruff format in Makefile --- .pre-commit-config.yaml | 9 +++------ .ruff.toml | 2 +- Makefile | 2 +- graphene_django/fields.py | 2 +- graphene_django/filter/fields.py | 2 +- graphene_django/filter/utils.py | 4 ++-- graphene_django/forms/mutation.py | 3 +-- graphene_django/rest_framework/mutation.py | 2 +- graphene_django/rest_framework/tests/test_mutation.py | 2 +- graphene_django/types.py | 6 ++---- graphene_django/utils/testing.py | 2 +- setup.py | 3 +-- 12 files changed, 16 insertions(+), 23 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5174be3aa..653849ca4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ default_language_version: python: python3.11 repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: check-merge-conflict - id: check-json @@ -15,12 +15,9 @@ repos: - --autofix - id: trailing-whitespace exclude: README.md -- repo: https://github.com/psf/black - rev: 23.7.0 - hooks: - - id: black - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.0.283 + rev: v0.1.2 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix, --show-fixes] + - id: ruff-format diff --git a/.ruff.toml b/.ruff.toml index b24997ce2..bcb85c377 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -13,6 +13,7 @@ ignore = [ "B017", # pytest.raises(Exception) should be considered evil "B028", # warnings.warn called without an explicit stacklevel keyword argument "B904", # check for raise statements in exception handlers that lack a from clause + "W191", # https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules ] exclude = [ @@ -29,5 +30,4 @@ target-version = "py38" [isort] known-first-party = ["graphene", "graphene-django"] known-local-folder = ["cookbook"] -force-wrap-aliases = true combine-as-imports = true diff --git a/Makefile b/Makefile index 31e5c937c..633c83f3b 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ tests: .PHONY: format ## Format code format: - black graphene_django examples setup.py + ruff format graphene_django examples setup.py .PHONY: lint ## Lint code lint: diff --git a/graphene_django/fields.py b/graphene_django/fields.py index 3537da394..35bd3f028 100644 --- a/graphene_django/fields.py +++ b/graphene_django/fields.py @@ -194,7 +194,7 @@ def connection_resolver( enforce_first_or_last, root, info, - **args + **args, ): first = args.get("first") last = args.get("last") diff --git a/graphene_django/filter/fields.py b/graphene_django/filter/fields.py index f6ad911d2..2380632d8 100644 --- a/graphene_django/filter/fields.py +++ b/graphene_django/filter/fields.py @@ -36,7 +36,7 @@ def __init__( extra_filter_meta=None, filterset_class=None, *args, - **kwargs + **kwargs, ): self._fields = fields self._provided_filterset_class = filterset_class diff --git a/graphene_django/filter/utils.py b/graphene_django/filter/utils.py index 339bd48f9..9ffcc5cf3 100644 --- a/graphene_django/filter/utils.py +++ b/graphene_django/filter/utils.py @@ -145,7 +145,7 @@ def replace_csv_filters(filterset_class): label=filter_field.label, method=filter_field.method, exclude=filter_field.exclude, - **filter_field.extra + **filter_field.extra, ) elif filter_type == "range": filterset_class.base_filters[name] = RangeFilter( @@ -154,5 +154,5 @@ def replace_csv_filters(filterset_class): label=filter_field.label, method=filter_field.method, exclude=filter_field.exclude, - **filter_field.extra + **filter_field.extra, ) diff --git a/graphene_django/forms/mutation.py b/graphene_django/forms/mutation.py index 40d1d3c7c..30b9af4ce 100644 --- a/graphene_django/forms/mutation.py +++ b/graphene_django/forms/mutation.py @@ -23,8 +23,7 @@ def fields_for_form(form, only_fields, exclude_fields): for name, field in form.fields.items(): is_not_in_only = only_fields and name not in only_fields is_excluded = ( - name - in exclude_fields # or + name in exclude_fields # or # name in already_created_fields ) diff --git a/graphene_django/rest_framework/mutation.py b/graphene_django/rest_framework/mutation.py index 47e71861b..f1f126780 100644 --- a/graphene_django/rest_framework/mutation.py +++ b/graphene_django/rest_framework/mutation.py @@ -81,7 +81,7 @@ def __init_subclass_with_meta__( convert_choices_to_enum=True, _meta=None, optional_fields=(), - **options + **options, ): if not serializer_class: raise Exception("serializer_class is required for the SerializerMutation") diff --git a/graphene_django/rest_framework/tests/test_mutation.py b/graphene_django/rest_framework/tests/test_mutation.py index 58bc4ce6c..17546c6b4 100644 --- a/graphene_django/rest_framework/tests/test_mutation.py +++ b/graphene_django/rest_framework/tests/test_mutation.py @@ -275,7 +275,7 @@ class Meta: result = MyMethodMutation.mutate_and_get_payload( None, mock_info(), - **{"cool_name": "Narf", "last_edited": datetime.date(2020, 1, 4)} + **{"cool_name": "Narf", "last_edited": datetime.date(2020, 1, 4)}, ) assert result.errors is None diff --git a/graphene_django/types.py b/graphene_django/types.py index 163fe3f39..02b7693e3 100644 --- a/graphene_django/types.py +++ b/graphene_django/types.py @@ -102,10 +102,8 @@ def validate_fields(type_, model, fields, only_fields, exclude_fields): if name in all_field_names: # Field is a custom field warnings.warn( - ( - 'Excluding the custom field "{field_name}" on DjangoObjectType "{type_}" has no effect. ' - 'Either remove the custom field or remove the field from the "exclude" list.' - ).format(field_name=name, type_=type_) + f'Excluding the custom field "{name}" on DjangoObjectType "{type_}" has no effect. ' + 'Either remove the custom field or remove the field from the "exclude" list.' ) else: if not hasattr(model, name): diff --git a/graphene_django/utils/testing.py b/graphene_django/utils/testing.py index 6cd0e3ba3..2ca1de941 100644 --- a/graphene_django/utils/testing.py +++ b/graphene_django/utils/testing.py @@ -63,7 +63,7 @@ def graphql_query( graphql_url, json.dumps(body), content_type="application/json", - **header_params + **header_params, ) else: resp = client.post( diff --git a/setup.py b/setup.py index 51ed63779..7e35a141f 100644 --- a/setup.py +++ b/setup.py @@ -26,8 +26,7 @@ dev_requires = [ - "black==23.7.0", - "ruff==0.0.283", + "ruff==0.1.2", "pre-commit", ] + tests_require From e735f5dbdbe901dbd8d523710799628544b4537b Mon Sep 17 00:00:00 2001 From: danthewildcat Date: Sun, 29 Oct 2023 11:42:27 -0400 Subject: [PATCH 09/12] Optimize views (#1439) * Optimize execute_graphql_request * Require operation_ast to be found by view handler * Remove unused show_graphiql kwarg * Old style if syntax * Revert "Remove unused show_graphiql kwarg" This reverts commit 33b3426092a2c6ceea35026276087f9c203e53ab. * Add missing schema validation step * Pass args directly to improve clarity * Remove duplicated operation_ast not None check --------- Co-authored-by: Firas Kafri <3097061+firaskafri@users.noreply.github.com> Co-authored-by: Kien Dang --- graphene_django/views.py | 72 +++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/graphene_django/views.py b/graphene_django/views.py index c6090b08c..9fc617207 100644 --- a/graphene_django/views.py +++ b/graphene_django/views.py @@ -9,10 +9,17 @@ from django.utils.decorators import method_decorator from django.views.decorators.csrf import ensure_csrf_cookie from django.views.generic import View -from graphql import OperationType, get_operation_ast, parse +from graphql import ( + ExecutionResult, + OperationType, + execute, + get_operation_ast, + parse, + validate_schema, +) from graphql.error import GraphQLError -from graphql.execution import ExecutionResult from graphql.execution.middleware import MiddlewareManager +from graphql.validation import validate from graphene import Schema from graphene_django.constants import MUTATION_ERRORS_FLAG @@ -295,43 +302,56 @@ def execute_graphql_request( return None raise HttpError(HttpResponseBadRequest("Must provide query string.")) + schema = self.schema.graphql_schema + + schema_validation_errors = validate_schema(schema) + if schema_validation_errors: + return ExecutionResult(data=None, errors=schema_validation_errors) + try: document = parse(query) except Exception as e: return ExecutionResult(errors=[e]) - if request.method.lower() == "get": - operation_ast = get_operation_ast(document, operation_name) - if operation_ast and operation_ast.operation != OperationType.QUERY: - if show_graphiql: - return None + operation_ast = get_operation_ast(document, operation_name) - raise HttpError( - HttpResponseNotAllowed( - ["POST"], - "Can only perform a {} operation from a POST request.".format( - operation_ast.operation.value - ), - ) + if ( + request.method.lower() == "get" + and operation_ast is not None + and operation_ast.operation != OperationType.QUERY + ): + if show_graphiql: + return None + + raise HttpError( + HttpResponseNotAllowed( + ["POST"], + "Can only perform a {} operation from a POST request.".format( + operation_ast.operation.value + ), ) - try: - extra_options = {} - if self.execution_context_class: - extra_options["execution_context_class"] = self.execution_context_class + ) + + validation_errors = validate(schema, document) + + if validation_errors: + return ExecutionResult(data=None, errors=validation_errors) - options = { - "source": query, + try: + execute_options = { "root_value": self.get_root_value(request), + "context_value": self.get_context(request), "variable_values": variables, "operation_name": operation_name, - "context_value": self.get_context(request), "middleware": self.get_middleware(request), } - options.update(extra_options) + if self.execution_context_class: + execute_options[ + "execution_context_class" + ] = self.execution_context_class - operation_ast = get_operation_ast(document, operation_name) if ( - operation_ast + operation_ast is not None and operation_ast.operation == OperationType.MUTATION and ( graphene_settings.ATOMIC_MUTATIONS is True @@ -339,12 +359,12 @@ def execute_graphql_request( ) ): with transaction.atomic(): - result = self.schema.execute(**options) + result = execute(schema, document, **execute_options) if getattr(request, MUTATION_ERRORS_FLAG, False) is True: transaction.set_rollback(True) return result - return self.schema.execute(**options) + return execute(schema, document, **execute_options) except Exception as e: return ExecutionResult(errors=[e]) From 62126dd46753ecce4f2b95bf63c1a7d08b1a91a2 Mon Sep 17 00:00:00 2001 From: Kien Dang Date: Wed, 6 Dec 2023 03:11:00 +0800 Subject: [PATCH 10/12] Add Python 3.12 to CI (#1481) --- .github/workflows/tests.yml | 2 ++ setup.py | 1 + tox.ini | 2 ++ 3 files changed, 5 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ee3af6a92..3c1bfe061 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -19,6 +19,8 @@ jobs: python-version: "3.11" - django: "4.2" python-version: "3.11" + - django: "4.2" + python-version: "3.12" steps: - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} diff --git a/setup.py b/setup.py index 7e35a141f..2c07aba28 100644 --- a/setup.py +++ b/setup.py @@ -49,6 +49,7 @@ "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", "Programming Language :: Python :: Implementation :: PyPy", "Framework :: Django", "Framework :: Django :: 3.2", diff --git a/tox.ini b/tox.ini index 41586baab..b7b6c63e1 100644 --- a/tox.ini +++ b/tox.ini @@ -3,6 +3,7 @@ envlist = py{38,39,310}-django32 py{38,39}-django{41,42} py{310,311}-django{41,42,main} + py312-django{42,main} pre-commit [gh-actions] @@ -11,6 +12,7 @@ python = 3.9: py39 3.10: py310 3.11: py311 + 3.12: py312 [gh-actions:env] DJANGO = From db2d40ec94847146a0af089f7e76d1c8a09d284c Mon Sep 17 00:00:00 2001 From: Kien Dang Date: Thu, 14 Dec 2023 15:20:54 +0700 Subject: [PATCH 11/12] Remove Django 4.1 (EOL) and add Django 5.0 to CI (#1483) --- .github/workflows/tests.yml | 16 +++++++++------- tox.ini | 10 +++++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3c1bfe061..9b81501a6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -12,15 +12,17 @@ jobs: strategy: max-parallel: 4 matrix: - django: ["3.2", "4.1", "4.2"] - python-version: ["3.8", "3.9", "3.10"] - include: - - django: "4.1" + django: ["3.2", "4.2", "5.0"] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + exclude: + - django: "3.2" python-version: "3.11" - - django: "4.2" - python-version: "3.11" - - django: "4.2" + - django: "3.2" python-version: "3.12" + - django: "5.0" + python-version: "3.8" + - django: "5.0" + python-version: "3.9" steps: - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} diff --git a/tox.ini b/tox.ini index b7b6c63e1..9a9dc14af 100644 --- a/tox.ini +++ b/tox.ini @@ -1,9 +1,9 @@ [tox] envlist = py{38,39,310}-django32 - py{38,39}-django{41,42} - py{310,311}-django{41,42,main} - py312-django{42,main} + py{38,39}-django42 + py{310,311}-django{42,50,main} + py312-django{42,50,main} pre-commit [gh-actions] @@ -17,8 +17,8 @@ python = [gh-actions:env] DJANGO = 3.2: django32 - 4.1: django41 4.2: django42 + 5.0: django50 main: djangomain [testenv] @@ -31,8 +31,8 @@ deps = -e.[test] psycopg2-binary django32: Django>=3.2,<4.0 - django41: Django>=4.1,<4.2 django42: Django>=4.2,<4.3 + django50: Django>=5.0,<5.1 djangomain: https://github.com/django/django/archive/main.zip commands = {posargs:pytest --cov=graphene_django graphene_django examples} From 3a64994e5299402768730ced852cf0e0ea75c14c Mon Sep 17 00:00:00 2001 From: Firas Kafri <3097061+firaskafri@users.noreply.github.com> Date: Wed, 20 Dec 2023 12:44:40 +0300 Subject: [PATCH 12/12] Bump version (#1486) --- graphene_django/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphene_django/__init__.py b/graphene_django/__init__.py index 22a035d86..7aff915e1 100644 --- a/graphene_django/__init__.py +++ b/graphene_django/__init__.py @@ -2,7 +2,7 @@ from .types import DjangoObjectType from .utils import bypass_get_queryset -__version__ = "3.1.5" +__version__ = "3.1.6" __all__ = [ "__version__",