mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-13 17:52:19 +03:00
merge with neomodel branch (edges updated)
This commit is contained in:
commit
ab52178a94
|
@ -11,7 +11,7 @@ from graphene.relay import ConnectionField, PageInfo
|
|||
from graphql_relay.connection.arrayconnection import connection_from_list_slice
|
||||
|
||||
from .settings import graphene_settings
|
||||
from .utils import maybe_queryset, is_parent_set
|
||||
from .utils import maybe_queryset, is_parent_set, set_parent
|
||||
|
||||
|
||||
class DjangoListField(Field):
|
||||
|
@ -183,11 +183,7 @@ class DjangoConnectionField(ConnectionField):
|
|||
if qs is None:
|
||||
qs = default_manager.filter()
|
||||
if _parent and root is not None:
|
||||
instances = []
|
||||
for instance in qs:
|
||||
setattr(instance, '_parent', root)
|
||||
instances.append(instance)
|
||||
return instances
|
||||
return list(map(lambda instance: set_parent(instance, root), qs))
|
||||
return qs
|
||||
|
||||
return cls.connection_resolver_original(
|
||||
|
|
|
@ -6,7 +6,7 @@ from ..fields import DjangoConnectionField
|
|||
from .utils import get_filterset_class, get_filtering_args_from_filterset, \
|
||||
make_qs
|
||||
|
||||
from ..utils import is_parent_set
|
||||
from ..utils import is_parent_set, set_parent
|
||||
|
||||
|
||||
class DjangoFilterConnectionField(DjangoConnectionField):
|
||||
|
@ -116,6 +116,7 @@ class DjangoFilterConnectionField(DjangoConnectionField):
|
|||
order = args.get('order', None)
|
||||
|
||||
_parent = is_parent_set(info)
|
||||
|
||||
if not _parent:
|
||||
_parent = args.get('know_parent', False)
|
||||
|
||||
|
@ -157,11 +158,7 @@ class DjangoFilterConnectionField(DjangoConnectionField):
|
|||
# set parent to child fields
|
||||
# in ''_parent'' attribute
|
||||
if _parent and root is not None:
|
||||
instances=[]
|
||||
for instance in qs:
|
||||
setattr(instance, '_parent', root)
|
||||
instances.append(instance)
|
||||
return instances
|
||||
return list(map(lambda instance: set_parent(instance, root), qs))
|
||||
return qs
|
||||
|
||||
if custom_resolver:
|
||||
|
|
|
@ -178,16 +178,16 @@ def list_resolver(node_type, edge_node) -> Callable:
|
|||
rel_data = []
|
||||
relation_field = getattr(root, edge_node.target_field)
|
||||
|
||||
# know_parent == None or False, return all relationships
|
||||
if not kwargs.get('know_parent'):
|
||||
for rel_node in relation_field.filter():
|
||||
rel_data.append(relation_field.relationship(rel_node))
|
||||
return relation_field.all_relationships()
|
||||
else:
|
||||
if not hasattr(root, '_parent'):
|
||||
raise EdgeNodeClass.parent_type_exception
|
||||
else:
|
||||
rel_data = relation_field.filter_relationships(root._parent)
|
||||
rel_data = relation_field.all_relationships(root._parent)
|
||||
if kwargs.get('id'):
|
||||
rel_data = relation_field.filter_relationships(
|
||||
rel_data = relation_field.all_relationships(
|
||||
edge_node.target_model.nodes.get(uid=kwargs['id']))
|
||||
return rel_data
|
||||
|
||||
|
@ -203,16 +203,17 @@ def field_resolver(node_type, edge_node) -> Callable:
|
|||
relation_field = getattr(root, edge_node.target_field)
|
||||
|
||||
if not kwargs.get('know_parent'):
|
||||
data = relation_field.filter().first_or_none()
|
||||
rels = relation_field.all_relationships()
|
||||
if rels:
|
||||
return rels[0]
|
||||
return None
|
||||
else:
|
||||
if not hasattr(root, '_parent'):
|
||||
raise EdgeNodeClass.parent_type_exception
|
||||
else:
|
||||
data = relation_field.relationship(root._parent)
|
||||
|
||||
if kwargs.get('id'):
|
||||
data = relation_field.relationship(
|
||||
edge_node.target_model.nodes.get(uid=kwargs['id']))
|
||||
return data
|
||||
|
||||
return default_resolver
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import inspect
|
||||
import inspect, re
|
||||
|
||||
from django.db import models
|
||||
from graphene.types.scalars import Int
|
||||
|
@ -11,15 +11,25 @@ from neomodel import (
|
|||
pagination_params = dict(first=Int(default_value=100), last=Int())
|
||||
# from graphene.utils import LazyList
|
||||
|
||||
def convert(name):
|
||||
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
|
||||
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
|
||||
|
||||
def is_parent_set(info):
|
||||
if hasattr(info.parent_type.graphene_type._meta, 'know_parent_fields'):
|
||||
options = info.parent_type.graphene_type._meta.know_parent_fields
|
||||
field_name = convert(info.field_name)
|
||||
assert isinstance(options, (list, tuple)), \
|
||||
"know_parent_fields should be list or tuple"
|
||||
return info.field_name in options
|
||||
return field_name in options
|
||||
return False
|
||||
|
||||
|
||||
def set_parent(item, root):
|
||||
setattr(item, '_parent', root)
|
||||
return item
|
||||
|
||||
|
||||
class LazyList(object):
|
||||
pass
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user