From c79097879d1bf43ddc021b03563fcb25d7e73ff5 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sun, 27 Sep 2015 20:37:47 -0700 Subject: [PATCH] Improved ObjectType instances --- README.md | 10 ++-------- graphene/core/schema.py | 3 ++- graphene/core/types.py | 8 ++++++++ tests/starwars_relay/schema.py | 8 ++------ 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 0b3e0019..9e6b9e28 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,6 @@ class Query(graphene.ObjectType): @resolve_only_args def resolve_human(self, id): return wrap_character(getHuman(id)) - if human: - return Human(human) @resolve_only_args def resolve_droid(self, id): @@ -91,9 +89,7 @@ class Ship(relay.Node): @classmethod def get_node(cls, id): - ship = getShip(id) - if ship: - return Ship(ship) + return Ship(getShip(id)) class Faction(relay.Node): @@ -107,9 +103,7 @@ class Faction(relay.Node): @classmethod def get_node(cls, id): - faction = getFaction(id) - if faction: - return Faction(faction) + return Faction(getFaction(id) class Query(graphene.ObjectType): diff --git a/graphene/core/schema.py b/graphene/core/schema.py index 132a4760..75f11eae 100644 --- a/graphene/core/schema.py +++ b/graphene/core/schema.py @@ -43,10 +43,11 @@ class Schema(object): return self._types[type_name] def execute(self, request='', root=None, vars=None, operation_name=None): + root = root or object() return graphql( self._schema, request=request, - root=root or self.query(), + root=self.query(root), vars=vars, operation_name=operation_name ) diff --git a/graphene/core/types.py b/graphene/core/types.py index 978fd41e..6c098a57 100644 --- a/graphene/core/types.py +++ b/graphene/core/types.py @@ -83,6 +83,13 @@ class ObjectTypeMeta(type): class ObjectType(six.with_metaclass(ObjectTypeMeta)): + def __new__(cls, instance=None, *args, **kwargs): + if cls._meta.interface: + raise Exception("An interface cannot be initialized") + if instance == None: + return None + return super(ObjectType, cls).__new__(cls, instance, *args, **kwargs) + def __init__(self, instance=None): signals.pre_init.send(self.__class__, instance=instance) self.instance = instance @@ -136,6 +143,7 @@ class Interface(ObjectType): interface = True proxy = True + @signals.init_schema.connect def add_types_to_schema(schema): own_schema = schema diff --git a/tests/starwars_relay/schema.py b/tests/starwars_relay/schema.py index fbbfc49c..8731f712 100644 --- a/tests/starwars_relay/schema.py +++ b/tests/starwars_relay/schema.py @@ -14,9 +14,7 @@ class Ship(relay.Node): @classmethod def get_node(cls, id): - ship = getShip(id) - if ship: - return Ship(ship) + return Ship(getShip(id)) class Faction(relay.Node): @@ -30,9 +28,7 @@ class Faction(relay.Node): @classmethod def get_node(cls, id): - faction = getFaction(id) - if faction: - return Faction(faction) + return Faction(getFaction(id)) class Query(graphene.ObjectType):