From 28bc8584440fd17cc3cc8741a1038ff10b6be00b Mon Sep 17 00:00:00 2001 From: Firas Kafri Date: Thu, 10 Aug 2023 09:53:04 +0300 Subject: [PATCH] resolve conflicts and fix format --- examples/cookbook/cookbook/recipes/schema.py | 3 +- examples/cookbook/cookbook/urls.py | 4 +-- graphene_django/debug/middleware.py | 12 +++---- graphene_django/fields.py | 7 ++-- graphene_django/filter/fields.py | 3 +- graphene_django/rest_framework/mutation.py | 4 +-- graphene_django/tests/test_fields.py | 6 ++-- graphene_django/tests/test_query.py | 36 +++++++++++--------- graphene_django/types.py | 2 +- graphene_django/utils/__init__.py | 4 +-- graphene_django/utils/utils.py | 1 + graphene_django/views.py | 6 ++-- 12 files changed, 40 insertions(+), 48 deletions(-) diff --git a/examples/cookbook/cookbook/recipes/schema.py b/examples/cookbook/cookbook/recipes/schema.py index 19fbcc6..2f2fc03 100644 --- a/examples/cookbook/cookbook/recipes/schema.py +++ b/examples/cookbook/cookbook/recipes/schema.py @@ -2,8 +2,7 @@ import asyncio from asgiref.sync import sync_to_async -from cookbook.recipes.models import Recipe, RecipeIngredient -from graphene import Node, String, Field +from graphene import Field, Node, String from graphene_django.filter import DjangoFilterConnectionField from graphene_django.types import DjangoObjectType diff --git a/examples/cookbook/cookbook/urls.py b/examples/cookbook/cookbook/urls.py index 57687da..c0f6fdf 100644 --- a/examples/cookbook/cookbook/urls.py +++ b/examples/cookbook/cookbook/urls.py @@ -1,8 +1,8 @@ -from django.urls import re_path from django.contrib import admin +from django.urls import re_path from django.views.decorators.csrf import csrf_exempt + from graphene_django.views import AsyncGraphQLView -from graphene_django.views import GraphQLView urlpatterns = [ re_path(r"^admin/", admin.site.urls), diff --git a/graphene_django/debug/middleware.py b/graphene_django/debug/middleware.py index 1e7bcf6..16589eb 100644 --- a/graphene_django/debug/middleware.py +++ b/graphene_django/debug/middleware.py @@ -1,15 +1,11 @@ -from django.db import connections - from asgiref.sync import sync_to_async -import inspect -from .sql.tracking import unwrap_cursor, wrap_cursor -from .exception.formating import wrap_exception -from .types import DjangoDebug +from django.db import connections from graphql.type.definition import GraphQLNonNull -from django.db.models import QuerySet - from ..utils import is_sync_function +from .exception.formating import wrap_exception +from .sql.tracking import unwrap_cursor, wrap_cursor +from .types import DjangoDebug class DjangoDebugContext: diff --git a/graphene_django/fields.py b/graphene_django/fields.py index 50a80dc..d5ed468 100644 --- a/graphene_django/fields.py +++ b/graphene_django/fields.py @@ -1,6 +1,6 @@ -import inspect from functools import partial +from asgiref.sync import sync_to_async from django.db.models.query import QuerySet from graphql_relay import ( connection_from_array_slice, @@ -9,16 +9,13 @@ from graphql_relay import ( offset_to_cursor, ) -from asgiref.sync import sync_to_async -from promise import Promise - from graphene import Int, NonNull from graphene.relay import ConnectionField from graphene.relay.connection import connection_adapter, page_info_adapter from graphene.types import Field, List from .settings import graphene_settings -from .utils import maybe_queryset, is_sync_function, is_running_async +from .utils import is_running_async, is_sync_function, maybe_queryset class DjangoListField(Field): diff --git a/graphene_django/filter/fields.py b/graphene_django/filter/fields.py index 4c8d826..a7be720 100644 --- a/graphene_django/filter/fields.py +++ b/graphene_django/filter/fields.py @@ -1,14 +1,13 @@ from collections import OrderedDict from functools import partial +from asgiref.sync import sync_to_async from django.core.exceptions import ValidationError from graphene.types.argument import to_arguments from graphene.types.enum import EnumType from graphene.utils.str_converters import to_snake_case -from asgiref.sync import sync_to_async - from ..fields import DjangoConnectionField from .utils import get_filtering_args_from_filterset, get_filterset_class diff --git a/graphene_django/rest_framework/mutation.py b/graphene_django/rest_framework/mutation.py index 1057299..f5f9b4e 100644 --- a/graphene_django/rest_framework/mutation.py +++ b/graphene_django/rest_framework/mutation.py @@ -1,9 +1,9 @@ from collections import OrderedDict from enum import Enum +from asgiref.sync import sync_to_async from django.shortcuts import get_object_or_404 from rest_framework import serializers -from asgiref.sync import sync_to_async import graphene from graphene.relay.mutation import ClientIDMutation @@ -12,8 +12,8 @@ from graphene.types.mutation import MutationOptions from graphene.types.objecttype import yank_fields_from_attrs from ..types import ErrorType -from .serializer_converter import convert_serializer_field from ..utils import is_running_async +from .serializer_converter import convert_serializer_field class SerializerMutationOptions(MutationOptions): diff --git a/graphene_django/tests/test_fields.py b/graphene_django/tests/test_fields.py index eae5d5b..3f67a9f 100644 --- a/graphene_django/tests/test_fields.py +++ b/graphene_django/tests/test_fields.py @@ -1,21 +1,21 @@ import datetime import re -from django.db.models import Count, Prefetch -from asgiref.sync import sync_to_async, async_to_sync + import pytest +from asgiref.sync import async_to_sync from django.db.models import Count, Prefetch from graphene import List, NonNull, ObjectType, Schema, String from ..fields import DjangoListField from ..types import DjangoObjectType +from .async_test_helper import assert_async_result_equal from .models import ( Article as ArticleModel, Film as FilmModel, FilmDetails as FilmDetailsModel, Reporter as ReporterModel, ) -from .async_test_helper import assert_async_result_equal class TestDjangoListField: diff --git a/graphene_django/tests/test_query.py b/graphene_django/tests/test_query.py index 4dd83e9..e8aa1f7 100644 --- a/graphene_django/tests/test_query.py +++ b/graphene_django/tests/test_query.py @@ -2,12 +2,12 @@ import base64 import datetime import pytest +from asgiref.sync import async_to_sync from django.db import models from django.db.models import Q from django.utils.functional import SimpleLazyObject from graphql_relay import to_global_id from pytest import raises -from asgiref.sync import sync_to_async, async_to_sync import graphene from graphene.relay import Node @@ -28,6 +28,7 @@ from .models import ( Reporter, ) + def test_should_query_only_fields(): with raises(Exception): @@ -1569,7 +1570,7 @@ def test_connection_should_limit_after_to_list_length(): expected = {"allReporters": {"edges": []}} assert not result.errors assert result.data == expected - assert_async_result_equal(schema, query, result, variable_values=dict(after=after)) + assert_async_result_equal(schema, query, result, variable_values={"after": after}) REPORTERS = [ @@ -1667,7 +1668,7 @@ def test_should_have_next_page(graphene_settings): gql_reporter["node"]["id"] for gql_reporter in gql_reporters } assert_async_result_equal( - schema, query, result2, variable_values=dict(first=4, after=last_result) + schema, query, result2, variable_values={"first": 4, "after": last_result} ) @@ -1736,7 +1737,7 @@ class TestBackwardPagination: def test_query_first_last_and_after(self, graphene_settings, max_limit): schema = self.setup_schema(graphene_settings, max_limit=max_limit) - query = """ + query_first_last_and_after = """ query queryAfter($after: String) { allReporters(first: 4, last: 3, after: $after) { edges { @@ -1759,12 +1760,12 @@ class TestBackwardPagination: e["node"]["firstName"] for e in result.data["allReporters"]["edges"] ] == ["First 2", "First 3", "First 4"] assert_async_result_equal( - schema, query, result, variable_values=dict(after=after) + schema, query_first_last_and_after, result, variable_values={"after": after} ) def test_query_last_and_before(self, graphene_settings, max_limit): schema = self.setup_schema(graphene_settings, max_limit=max_limit) - query = """ + query_first_last_and_after = """ query queryAfter($before: String) { allReporters(last: 1, before: $before) { edges { @@ -1777,12 +1778,12 @@ class TestBackwardPagination: """ result = schema.execute( - query, + query_first_last_and_after, ) assert not result.errors assert len(result.data["allReporters"]["edges"]) == 1 assert result.data["allReporters"]["edges"][0]["node"]["firstName"] == "First 5" - assert_async_result_equal(schema, query, result) + assert_async_result_equal(schema, query_first_last_and_after, result) before = base64.b64encode(b"arrayconnection:5").decode() result = schema.execute( @@ -1793,7 +1794,10 @@ class TestBackwardPagination: assert len(result.data["allReporters"]["edges"]) == 1 assert result.data["allReporters"]["edges"][0]["node"]["firstName"] == "First 4" assert_async_result_equal( - schema, query, result, variable_values=dict(before=before) + schema, + query_first_last_and_after, + result, + variable_values={"before": before}, ) @@ -2021,9 +2025,7 @@ def test_connection_should_forbid_offset_filtering_with_before(): expected_error = "You can't provide a `before` value at the same time as an `offset` value to properly paginate the `allReporters` connection." assert len(result.errors) == 1 assert result.errors[0].message == expected_error - assert_async_result_equal( - schema, query, result, variable_values=dict(before=before) - ) + assert_async_result_equal(schema, query, result, variable_values={"before": before}) def test_connection_should_allow_offset_filtering_with_after(): @@ -2066,7 +2068,7 @@ def test_connection_should_allow_offset_filtering_with_after(): } } assert result.data == expected - assert_async_result_equal(schema, query, result, variable_values=dict(after=after)) + assert_async_result_equal(schema, query, result, variable_values={"after": after}) def test_connection_should_succeed_if_last_higher_than_number_of_objects(): @@ -2097,7 +2099,7 @@ def test_connection_should_succeed_if_last_higher_than_number_of_objects(): assert not result.errors expected = {"allReporters": {"edges": []}} assert result.data == expected - assert_async_result_equal(schema, query, result, variable_values=dict(last=2)) + assert_async_result_equal(schema, query, result, variable_values={"last": 2}) Reporter.objects.create(first_name="John", last_name="Doe") Reporter.objects.create(first_name="Some", last_name="Guy") @@ -2115,7 +2117,7 @@ def test_connection_should_succeed_if_last_higher_than_number_of_objects(): } } assert result.data == expected - assert_async_result_equal(schema, query, result, variable_values=dict(last=2)) + assert_async_result_equal(schema, query, result, variable_values={"last": 2}) result = schema.execute(query, variable_values={"last": 4}) assert not result.errors @@ -2130,7 +2132,7 @@ def test_connection_should_succeed_if_last_higher_than_number_of_objects(): } } assert result.data == expected - assert_async_result_equal(schema, query, result, variable_values=dict(last=4)) + assert_async_result_equal(schema, query, result, variable_values={"last": 4}) result = schema.execute(query, variable_values={"last": 20}) assert not result.errors @@ -2145,7 +2147,7 @@ def test_connection_should_succeed_if_last_higher_than_number_of_objects(): } } assert result.data == expected - assert_async_result_equal(schema, query, result, variable_values=dict(last=20)) + assert_async_result_equal(schema, query, result, variable_values={"last": 20}) def test_should_query_nullable_foreign_key(): diff --git a/graphene_django/types.py b/graphene_django/types.py index 5dfbe55..0f94fa2 100644 --- a/graphene_django/types.py +++ b/graphene_django/types.py @@ -16,8 +16,8 @@ from .utils import ( DJANGO_FILTER_INSTALLED, camelize, get_model_fields, - is_valid_django_model, is_running_async, + is_valid_django_model, ) ALL_FIELDS = "__all__" diff --git a/graphene_django/utils/__init__.py b/graphene_django/utils/__init__.py index b1a11cf..609da96 100644 --- a/graphene_django/utils/__init__.py +++ b/graphene_django/utils/__init__.py @@ -5,10 +5,10 @@ from .utils import ( camelize, get_model_fields, get_reverse_fields, + is_running_async, + is_sync_function, is_valid_django_model, maybe_queryset, - is_sync_function, - is_running_async, ) __all__ = [ diff --git a/graphene_django/utils/utils.py b/graphene_django/utils/utils.py index 1b9760a..7cdaf06 100644 --- a/graphene_django/utils/utils.py +++ b/graphene_django/utils/utils.py @@ -153,6 +153,7 @@ def is_sync_function(func): func ) + def bypass_get_queryset(resolver): """ Adds a bypass_get_queryset attribute to the resolver, which is used to diff --git a/graphene_django/views.py b/graphene_django/views.py index 1f3a90b..2a63d23 100644 --- a/graphene_django/views.py +++ b/graphene_django/views.py @@ -2,16 +2,14 @@ import inspect import json import re import traceback - -from asyncio import gather, coroutines +from asyncio import coroutines, gather from django.db import connection, transaction from django.http import HttpResponse, HttpResponseNotAllowed from django.http.response import HttpResponseBadRequest from django.shortcuts import render -from django.utils.decorators import method_decorator +from django.utils.decorators import classonlymethod, method_decorator from django.views.decorators.csrf import ensure_csrf_cookie -from django.utils.decorators import classonlymethod from django.views.generic import View from graphql import OperationType, get_operation_ast, parse from graphql.error import GraphQLError