Add copy function for GrapheneGraphQLType

Fixes https://github.com/graphql-python/graphene/issues/1333

First found when using  with a Django project. Django's  tries to create a copy of a GrapheneGraphQLType and raises this KeyError
This commit is contained in:
Kristian Uzhca 2022-09-30 15:52:45 -04:00
parent ee1ff975d7
commit 1dbdb42dee
2 changed files with 20 additions and 0 deletions

View File

@ -20,6 +20,11 @@ class GrapheneGraphQLType:
self.graphene_type = kwargs.pop("graphene_type") self.graphene_type = kwargs.pop("graphene_type")
super(GrapheneGraphQLType, self).__init__(*args, **kwargs) super(GrapheneGraphQLType, self).__init__(*args, **kwargs)
def __copy__(self):
result = GrapheneGraphQLType(graphene_type=self.graphene_type)
result.__dict__.update(self.__dict__)
return result
class GrapheneInterfaceType(GrapheneGraphQLType, GraphQLInterfaceType): class GrapheneInterfaceType(GrapheneGraphQLType, GraphQLInterfaceType):
pass pass

View File

@ -1,4 +1,7 @@
import copy
from ..argument import Argument from ..argument import Argument
from ..definitions import GrapheneGraphQLType
from ..enum import Enum from ..enum import Enum
from ..field import Field from ..field import Field
from ..inputfield import InputField from ..inputfield import InputField
@ -312,3 +315,15 @@ def test_does_not_mutate_passed_field_definitions():
pass pass
assert TestInputObject1._meta.fields == TestInputObject2._meta.fields assert TestInputObject1._meta.fields == TestInputObject2._meta.fields
def test_graphene_graphql_type_can_be_copied():
class Query(ObjectType):
field = String()
def resolve_field(self, info):
return ''
schema = Schema(query=Query)
query_type_copy = copy.copy(schema.graphql_schema.query_type)
assert query_type_copy.__dict__ == schema.graphql_schema.query_type.__dict__
assert isinstance(schema.graphql_schema.query_type, GrapheneGraphQLType)