mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 12:44:15 +03:00
Added app_label
This commit is contained in:
parent
cd216447c2
commit
1ec2f5a4c3
|
@ -1,17 +1,18 @@
|
||||||
from graphene.utils import cached_property
|
from graphene.utils import cached_property
|
||||||
|
|
||||||
DEFAULT_NAMES = ('description', 'name', 'interface',
|
DEFAULT_NAMES = ('app_label', 'description', 'name', 'interface',
|
||||||
'type_name', 'interfaces', 'proxy')
|
'type_name', 'interfaces', 'proxy')
|
||||||
|
|
||||||
|
|
||||||
class Options(object):
|
class Options(object):
|
||||||
def __init__(self, meta=None):
|
def __init__(self, meta=None, app_label=None):
|
||||||
self.meta = meta
|
self.meta = meta
|
||||||
self.local_fields = []
|
self.local_fields = []
|
||||||
self.interface = False
|
self.interface = False
|
||||||
self.proxy = False
|
self.proxy = False
|
||||||
self.interfaces = []
|
self.interfaces = []
|
||||||
self.parents = []
|
self.parents = []
|
||||||
|
self.app_label = app_label
|
||||||
|
|
||||||
def contribute_to_class(self, cls, name):
|
def contribute_to_class(self, cls, name):
|
||||||
cls._meta = self
|
cls._meta = self
|
||||||
|
|
|
@ -33,7 +33,7 @@ class ObjectTypeMeta(type):
|
||||||
meta = attr_meta
|
meta = attr_meta
|
||||||
base_meta = getattr(new_class, '_meta', None)
|
base_meta = getattr(new_class, '_meta', None)
|
||||||
|
|
||||||
new_class.add_to_class('_meta', Options(meta))
|
new_class.add_to_class('_meta', Options(meta, module))
|
||||||
if base_meta and base_meta.proxy:
|
if base_meta and base_meta.proxy:
|
||||||
new_class._meta.interface = base_meta.interface
|
new_class._meta.interface = base_meta.interface
|
||||||
# Add all attributes to the class.
|
# Add all attributes to the class.
|
||||||
|
|
|
@ -14,16 +14,34 @@ def get_object_type(field_type, object_type=None):
|
||||||
if field_type == 'self':
|
if field_type == 'self':
|
||||||
field_type = object_type._meta.type
|
field_type = object_type._meta.type
|
||||||
else:
|
else:
|
||||||
object_type = get_registered_object_type(field_type)
|
object_type = get_registered_object_type(field_type, object_type)
|
||||||
field_type = object_type._meta.type
|
field_type = object_type._meta.type
|
||||||
return field_type
|
return field_type
|
||||||
|
|
||||||
|
|
||||||
def get_registered_object_type(name):
|
def get_registered_object_type(name, object_type=None):
|
||||||
for object_type in registered_object_types:
|
app_label = None
|
||||||
if object_type._meta.type_name == name:
|
object_type_name = name
|
||||||
return object_type
|
|
||||||
return None
|
if '.' in name:
|
||||||
|
app_label, object_type_name = name.split('.', 1)
|
||||||
|
elif object_type:
|
||||||
|
app_label = object_type._meta.app_label
|
||||||
|
|
||||||
|
# Filter all registered object types which have the same name
|
||||||
|
ots = [ot for ot in registered_object_types if ot._meta.type_name == name]
|
||||||
|
# If the list have more than one object type with the name, filter by
|
||||||
|
# the app_label
|
||||||
|
if len(ots)>1 and app_label:
|
||||||
|
ots = [ot for ot in ots if ot._meta.app_label == app_label]
|
||||||
|
|
||||||
|
if len(ots)>1:
|
||||||
|
raise Exception('Multiple ObjectTypes returned with the name %s' % name)
|
||||||
|
if not ots:
|
||||||
|
raise Exception('No ObjectType found with name %s' % name)
|
||||||
|
|
||||||
|
return ots[0]
|
||||||
|
|
||||||
|
|
||||||
@signals.class_prepared.connect
|
@signals.class_prepared.connect
|
||||||
def object_type_created(sender):
|
def object_type_created(sender):
|
||||||
|
|
|
@ -21,7 +21,7 @@ def wrap_character(character):
|
||||||
|
|
||||||
class Character(graphene.Interface):
|
class Character(graphene.Interface):
|
||||||
name = graphene.StringField()
|
name = graphene.StringField()
|
||||||
friends = relay.Connection('self')
|
friends = relay.Connection('Character')
|
||||||
appearsIn = graphene.ListField(Episode)
|
appearsIn = graphene.ListField(Episode)
|
||||||
|
|
||||||
def resolve_friends(self, args, *_):
|
def resolve_friends(self, args, *_):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user