mirror of
https://github.com/graphql-python/graphene.git
synced 2025-09-21 19:32:33 +03:00
Allow initialization of ObjectType with dynamic private state
This commit is contained in:
parent
c5b2281e22
commit
5a537872e4
80
graphene/tests/issues/test_870.py
Normal file
80
graphene/tests/issues/test_870.py
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
import graphene
|
||||||
|
|
||||||
|
|
||||||
|
class SecondChild(graphene.ObjectType):
|
||||||
|
hello = graphene.String()
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_second_child(self, info):
|
||||||
|
return SecondChild(hello=self.second_child._test)
|
||||||
|
|
||||||
|
|
||||||
|
second_child_f = graphene.Field(SecondChild, resolver=resolve_second_child)
|
||||||
|
|
||||||
|
|
||||||
|
class FirstChild(graphene.ObjectType):
|
||||||
|
hello = graphene.String()
|
||||||
|
second_child = second_child_f
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_first_child(self, info):
|
||||||
|
message = self.first_child._test + " stranger"
|
||||||
|
|
||||||
|
test_case = {"_test": message}
|
||||||
|
|
||||||
|
return FirstChild(
|
||||||
|
hello=self.first_child._test, second_child=SecondChild(**test_case)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
first_child_f = graphene.Field(FirstChild, resolver=resolve_first_child)
|
||||||
|
|
||||||
|
|
||||||
|
class ParentQuery(graphene.ObjectType):
|
||||||
|
hello = graphene.String()
|
||||||
|
first_child = first_child_f
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_parent(self, info, **args):
|
||||||
|
|
||||||
|
message = args.get("greeting") + " there"
|
||||||
|
|
||||||
|
test_case = {"_test": message}
|
||||||
|
|
||||||
|
return ParentQuery(hello=args.get("greeting"), first_child=FirstChild(**test_case))
|
||||||
|
|
||||||
|
|
||||||
|
parent = graphene.Field(
|
||||||
|
ParentQuery, resolver=resolve_parent, greeting=graphene.Argument(graphene.String)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Query(graphene.ObjectType):
|
||||||
|
final = parent
|
||||||
|
|
||||||
|
|
||||||
|
def test_issue():
|
||||||
|
query_string = """
|
||||||
|
query {
|
||||||
|
final (greeting: "hi") {
|
||||||
|
hello
|
||||||
|
firstChild {
|
||||||
|
hello
|
||||||
|
secondChild {
|
||||||
|
hello
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
schema = graphene.Schema(query=Query)
|
||||||
|
result = schema.execute(query_string)
|
||||||
|
|
||||||
|
assert not result.errors
|
||||||
|
assert result.data["final"]["hello"] == "hi"
|
||||||
|
assert result.data["final"]["firstChild"]["hello"] == "hi there"
|
||||||
|
assert (
|
||||||
|
result.data["final"]["firstChild"]["secondChild"]["hello"]
|
||||||
|
== "hi there stranger"
|
||||||
|
)
|
|
@ -95,7 +95,7 @@ class ObjectType(BaseType):
|
||||||
for prop in list(kwargs):
|
for prop in list(kwargs):
|
||||||
try:
|
try:
|
||||||
if isinstance(
|
if isinstance(
|
||||||
getattr(self.__class__, prop), property
|
getattr(self.__class__, prop, None), property
|
||||||
) or prop.startswith("_"):
|
) or prop.startswith("_"):
|
||||||
setattr(self, prop, kwargs.pop(prop))
|
setattr(self, prop, kwargs.pop(prop))
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
|
|
@ -91,8 +91,8 @@ def test_generate_objecttype_with_private_attributes():
|
||||||
m = MyObjectType(_private_state="custom")
|
m = MyObjectType(_private_state="custom")
|
||||||
assert m._private_state == "custom"
|
assert m._private_state == "custom"
|
||||||
|
|
||||||
with pytest.raises(TypeError):
|
m = MyObjectType(_other_private_state="custom")
|
||||||
MyObjectType(_other_private_state="Wrong")
|
assert getattr(m, "_other_private_state", None) is "custom"
|
||||||
|
|
||||||
|
|
||||||
def test_ordered_fields_in_objecttype():
|
def test_ordered_fields_in_objecttype():
|
||||||
|
|
Loading…
Reference in New Issue
Block a user