mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 12:44:15 +03:00
Added support for resolver tagging. Fixed #6
This commit is contained in:
parent
739856af73
commit
40b88bc87b
|
@ -1,6 +1,6 @@
|
||||||
import inspect
|
import inspect
|
||||||
import six
|
import six
|
||||||
from functools import total_ordering
|
from functools import total_ordering, wraps
|
||||||
from graphql.core.type import (
|
from graphql.core.type import (
|
||||||
GraphQLField,
|
GraphQLField,
|
||||||
GraphQLList,
|
GraphQLList,
|
||||||
|
@ -49,12 +49,26 @@ class Field(object):
|
||||||
cls._meta.add_field(self)
|
cls._meta.add_field(self)
|
||||||
|
|
||||||
def resolve(self, instance, args, info):
|
def resolve(self, instance, args, info):
|
||||||
if self.resolve_fn:
|
resolve_fn = self.get_resolve_fn()
|
||||||
resolve_fn = self.resolve_fn
|
if resolve_fn:
|
||||||
else:
|
|
||||||
resolve_fn = lambda root, args, info: root.resolve(
|
|
||||||
self.field_name, args, info)
|
|
||||||
return resolve_fn(instance, args, info)
|
return resolve_fn(instance, args, info)
|
||||||
|
else:
|
||||||
|
return instance.get_field(self.field_name)
|
||||||
|
|
||||||
|
@memoize
|
||||||
|
def get_resolve_fn(self):
|
||||||
|
if self.resolve_fn:
|
||||||
|
return self.resolve_fn
|
||||||
|
else:
|
||||||
|
custom_resolve_fn_name = 'resolve_%s' % self.field_name
|
||||||
|
if hasattr(self.object_type, custom_resolve_fn_name):
|
||||||
|
resolve_fn = getattr(self.object_type, custom_resolve_fn_name)
|
||||||
|
|
||||||
|
@wraps(resolve_fn)
|
||||||
|
def custom_resolve_fn(instance, args, info):
|
||||||
|
custom_fn = getattr(instance, custom_resolve_fn_name)
|
||||||
|
return custom_fn(args, info)
|
||||||
|
return custom_resolve_fn
|
||||||
|
|
||||||
def get_object_type(self, schema):
|
def get_object_type(self, schema):
|
||||||
field_type = self.field_type
|
field_type = self.field_type
|
||||||
|
@ -110,11 +124,18 @@ class Field(object):
|
||||||
if not internal_type:
|
if not internal_type:
|
||||||
raise Exception("Internal type for field %s is None" % self)
|
raise Exception("Internal type for field %s is None" % self)
|
||||||
|
|
||||||
|
resolve_fn = self.get_resolve_fn()
|
||||||
|
if resolve_fn:
|
||||||
|
@wraps(resolve_fn)
|
||||||
|
def resolver(*args):
|
||||||
|
return self.resolve(*args)
|
||||||
|
else:
|
||||||
|
resolver = self.resolve
|
||||||
return GraphQLField(
|
return GraphQLField(
|
||||||
internal_type,
|
internal_type,
|
||||||
description=self.description,
|
description=self.description,
|
||||||
args=self.args,
|
args=self.args,
|
||||||
resolver=self.resolve,
|
resolver=resolver,
|
||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
@ -144,7 +165,7 @@ class Field(object):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash(self.creation_counter)
|
return hash((self.creation_counter, self.object_type))
|
||||||
|
|
||||||
def __copy__(self):
|
def __copy__(self):
|
||||||
# We need to avoid hitting __reduce__, so define this
|
# We need to avoid hitting __reduce__, so define this
|
||||||
|
|
|
@ -132,13 +132,6 @@ class BaseObjectType(object):
|
||||||
def get_field(self, field):
|
def get_field(self, field):
|
||||||
return getattr(self.instance, field, None)
|
return getattr(self.instance, field, None)
|
||||||
|
|
||||||
def resolve(self, field_name, args, info):
|
|
||||||
custom_resolve_fn = 'resolve_%s' % field_name
|
|
||||||
if hasattr(self, custom_resolve_fn):
|
|
||||||
resolve_fn = getattr(self, custom_resolve_fn)
|
|
||||||
return resolve_fn(args, info)
|
|
||||||
return self.get_field(field_name)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def resolve_type(cls, schema, instance, *_):
|
def resolve_type(cls, schema, instance, *_):
|
||||||
return instance.internal_type(schema)
|
return instance.internal_type(schema)
|
||||||
|
|
|
@ -69,3 +69,15 @@ def test_field_clashes():
|
||||||
class Droid(Character):
|
class Droid(Character):
|
||||||
name = IntField()
|
name = IntField()
|
||||||
assert 'clashes' in str(excinfo.value)
|
assert 'clashes' in str(excinfo.value)
|
||||||
|
|
||||||
|
|
||||||
|
def test_field_mantain_resolver_tags():
|
||||||
|
class Droid(Character):
|
||||||
|
name = StringField()
|
||||||
|
|
||||||
|
def resolve_name(self, *args):
|
||||||
|
return 'My Droid'
|
||||||
|
resolve_name.custom_tag = True
|
||||||
|
|
||||||
|
field = Droid._meta.fields_map['name'].internal_field(schema)
|
||||||
|
assert field.resolver.custom_tag
|
||||||
|
|
Loading…
Reference in New Issue
Block a user