mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-23 07:00:56 +03:00
Allow setting unmounted types with custom attributes that get carried over.
This commit is contained in:
parent
88ccaec8fa
commit
8fa8fba718
|
@ -44,15 +44,30 @@ def test_mutation_raises_exception_if_no_mutate():
|
||||||
|
|
||||||
|
|
||||||
def test_mutation_execution():
|
def test_mutation_execution():
|
||||||
|
def with_metadata(gql_type, data):
|
||||||
|
setattr(gql_type, 'metadata', data)
|
||||||
|
return gql_type
|
||||||
|
|
||||||
class CreateUser(Mutation):
|
class CreateUser(Mutation):
|
||||||
class Input:
|
class Input:
|
||||||
name = String()
|
name = String()
|
||||||
|
metadata = with_metadata(String(), 'service_key')
|
||||||
|
|
||||||
|
_Input = Input
|
||||||
|
|
||||||
name = String()
|
name = String()
|
||||||
|
external = String()
|
||||||
|
|
||||||
def mutate(self, args, context, info):
|
@classmethod
|
||||||
|
def mutate(cls, root, args, context, info):
|
||||||
name = args.get('name')
|
name = args.get('name')
|
||||||
return CreateUser(name=name)
|
external_data = {
|
||||||
|
'service_key': 'new_data'
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata_type = dict(vars(cls._Input))['metadata']
|
||||||
|
data = external_data[getattr(metadata_type, 'metadata')]
|
||||||
|
return CreateUser(name=name, external=data)
|
||||||
|
|
||||||
class Query(ObjectType):
|
class Query(ObjectType):
|
||||||
a = String()
|
a = String()
|
||||||
|
@ -64,12 +79,14 @@ def test_mutation_execution():
|
||||||
result = schema.execute(''' mutation mymutation {
|
result = schema.execute(''' mutation mymutation {
|
||||||
createUser(name:"Peter") {
|
createUser(name:"Peter") {
|
||||||
name
|
name
|
||||||
|
external
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
''')
|
''')
|
||||||
assert not result.errors
|
assert not result.errors
|
||||||
assert result.data == {
|
assert result.data == {
|
||||||
'createUser': {
|
'createUser': {
|
||||||
'name': "Peter"
|
'name': 'Peter',
|
||||||
|
'external': 'new_data',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,36 @@ def test_query_wrong_default_value():
|
||||||
assert executed.data == {'hello': None}
|
assert executed.data == {'hello': None}
|
||||||
|
|
||||||
|
|
||||||
|
def test_query_unmounted_type_with_custom_attributes():
|
||||||
|
def set_custom_metadata(gql_type):
|
||||||
|
setattr(gql_type, 'metadata', 'some_key')
|
||||||
|
return gql_type
|
||||||
|
|
||||||
|
class MyType(ObjectType):
|
||||||
|
field = set_custom_metadata(String())
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_metadata(cls):
|
||||||
|
my_type = cls()
|
||||||
|
external_data = {
|
||||||
|
'some_key': 'my_data'
|
||||||
|
}
|
||||||
|
for field_name, field_value in cls._meta.fields.items():
|
||||||
|
data = external_data[getattr(field_value, 'metadata')]
|
||||||
|
setattr(my_type, field_name, data)
|
||||||
|
|
||||||
|
return my_type
|
||||||
|
|
||||||
|
class Query(ObjectType):
|
||||||
|
hello = Field(MyType, resolver=lambda *_: MyType.from_metadata())
|
||||||
|
|
||||||
|
hello_schema = Schema(Query)
|
||||||
|
|
||||||
|
executed = hello_schema.execute('{ hello { field } }')
|
||||||
|
assert not executed.errors
|
||||||
|
assert executed.data == {'hello': {'field': 'my_data'}}
|
||||||
|
|
||||||
|
|
||||||
def test_query_default_value_ignored_by_resolver():
|
def test_query_default_value_ignored_by_resolver():
|
||||||
class MyType(ObjectType):
|
class MyType(ObjectType):
|
||||||
field = String()
|
field = String()
|
||||||
|
|
|
@ -19,34 +19,52 @@ class UnmountedType(OrderedType):
|
||||||
super(UnmountedType, self).__init__()
|
super(UnmountedType, self).__init__()
|
||||||
self.args = args
|
self.args = args
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
|
self.custom_attributes = {}
|
||||||
|
|
||||||
def get_type(self):
|
def get_type(self):
|
||||||
raise NotImplementedError("get_type not implemented in {}".format(self))
|
raise NotImplementedError("get_type not implemented in {}".format(self))
|
||||||
|
|
||||||
|
def __setattr__(self, name, value):
|
||||||
|
if hasattr(self, 'custom_attributes'):
|
||||||
|
self.custom_attributes[name] = value
|
||||||
|
OrderedType.__setattr__(self, name, value)
|
||||||
|
|
||||||
def Field(self): # noqa: N802
|
def Field(self): # noqa: N802
|
||||||
'''
|
'''
|
||||||
Mount the UnmountedType as Field
|
Mount the UnmountedType as Field
|
||||||
'''
|
'''
|
||||||
from .field import Field
|
from .field import Field
|
||||||
return Field(
|
f = Field(
|
||||||
self.get_type(),
|
self.get_type(),
|
||||||
*self.args,
|
*self.args,
|
||||||
_creation_counter=self.creation_counter,
|
_creation_counter=self.creation_counter,
|
||||||
**self.kwargs
|
**self.kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if hasattr(self, 'custom_attributes'):
|
||||||
|
for name, value in self.custom_attributes.items():
|
||||||
|
setattr(f, name, value)
|
||||||
|
|
||||||
|
return f
|
||||||
|
|
||||||
def InputField(self): # noqa: N802
|
def InputField(self): # noqa: N802
|
||||||
'''
|
'''
|
||||||
Mount the UnmountedType as InputField
|
Mount the UnmountedType as InputField
|
||||||
'''
|
'''
|
||||||
from .inputfield import InputField
|
from .inputfield import InputField
|
||||||
return InputField(
|
f = InputField(
|
||||||
self.get_type(),
|
self.get_type(),
|
||||||
*self.args,
|
*self.args,
|
||||||
_creation_counter=self.creation_counter,
|
_creation_counter=self.creation_counter,
|
||||||
**self.kwargs
|
**self.kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if hasattr(self, 'custom_attributes'):
|
||||||
|
for name, value in self.custom_attributes.items():
|
||||||
|
setattr(f, name, value)
|
||||||
|
|
||||||
|
return f
|
||||||
|
|
||||||
def Argument(self): # noqa: N802
|
def Argument(self): # noqa: N802
|
||||||
'''
|
'''
|
||||||
Mount the UnmountedType as Argument
|
Mount the UnmountedType as Argument
|
||||||
|
|
Loading…
Reference in New Issue
Block a user