mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-22 17:46:57 +03:00
Move resolver to interface too
This commit is contained in:
parent
da3769b902
commit
0db5b1cfbc
|
@ -16,24 +16,20 @@ class Character(graphene.Interface):
|
|||
friends = graphene.List(lambda: Character)
|
||||
appears_in = graphene.List(Episode)
|
||||
|
||||
def resolve_friends(self, args, *_):
|
||||
# The character friends is a list of strings
|
||||
return [get_character(f) for f in self.friends]
|
||||
|
||||
|
||||
@graphene.implements(Character)
|
||||
class Human(graphene.ObjectType):
|
||||
home_planet = graphene.String()
|
||||
|
||||
def resolve_friends(self, args, *_):
|
||||
# The character friends is a list of strings
|
||||
return [get_character(f) for f in self.friends]
|
||||
|
||||
|
||||
@graphene.implements(Character)
|
||||
class Droid(graphene.ObjectType):
|
||||
primary_function = graphene.String()
|
||||
|
||||
def resolve_friends(self, args, *_):
|
||||
# The character friends is a list of strings
|
||||
return [get_character(f) for f in self.friends]
|
||||
|
||||
|
||||
class Query(graphene.ObjectType):
|
||||
hero = graphene.Field(Character,
|
||||
|
|
|
@ -74,10 +74,29 @@ class Field(AbstractField, GraphQLField, OrderedType):
|
|||
|
||||
@property
|
||||
def resolver(self):
|
||||
from .objecttype import ObjectType
|
||||
from .interface import GrapheneInterfaceType
|
||||
def default_resolver(root, args, context, info):
|
||||
return getattr(root, self.source or self.attname, None)
|
||||
|
||||
resolver = getattr(self.parent, 'resolve_{}'.format(self.attname), default_resolver)
|
||||
resolver = getattr(self.parent, 'resolve_{}'.format(self.attname), None)
|
||||
|
||||
# We try to get the resolver from the interfaces
|
||||
if not resolver and issubclass(self.parent, ObjectType):
|
||||
graphql_type = self.parent._meta.graphql_type
|
||||
for interface in graphql_type._provided_interfaces:
|
||||
if not isinstance(interface, GrapheneInterfaceType):
|
||||
continue
|
||||
fields = interface.get_fields()
|
||||
if self.attname in fields:
|
||||
resolver = getattr(interface.graphene_type, 'resolve_{}'.format(self.attname), None)
|
||||
if resolver:
|
||||
# We remove the bounding to the method
|
||||
resolver = resolver.__func__
|
||||
break
|
||||
|
||||
if not resolver:
|
||||
resolver = default_resolver
|
||||
|
||||
def resolver_wrapper(root, *args, **kwargs):
|
||||
if not isinstance(root, self.parent):
|
||||
|
|
Loading…
Reference in New Issue
Block a user