Allow initialization of ObjectType with dynamic private state

This commit is contained in:
getglad 2019-03-27 22:26:37 -04:00
parent c5b2281e22
commit 5a537872e4
3 changed files with 83 additions and 3 deletions

View 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"
)

View File

@ -95,7 +95,7 @@ class ObjectType(BaseType):
for prop in list(kwargs):
try:
if isinstance(
getattr(self.__class__, prop), property
getattr(self.__class__, prop, None), property
) or prop.startswith("_"):
setattr(self, prop, kwargs.pop(prop))
except AttributeError:

View File

@ -91,8 +91,8 @@ def test_generate_objecttype_with_private_attributes():
m = MyObjectType(_private_state="custom")
assert m._private_state == "custom"
with pytest.raises(TypeError):
MyObjectType(_other_private_state="Wrong")
m = MyObjectType(_other_private_state="custom")
assert getattr(m, "_other_private_state", None) is "custom"
def test_ordered_fields_in_objecttype():