mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-26 19:43:56 +03:00
add meta option identifier to SQLAlchemy nodes
This commit is contained in:
parent
efa67fa1db
commit
1f9252f26a
|
@ -2,7 +2,7 @@ from ...core.classtypes.objecttype import ObjectTypeOptions
|
|||
from ...relay.types import Node
|
||||
from ...relay.utils import is_node
|
||||
|
||||
VALID_ATTRS = ('model', 'only_fields', 'exclude_fields')
|
||||
VALID_ATTRS = ('model', 'only_fields', 'exclude_fields', 'identifier')
|
||||
|
||||
|
||||
class SQLAlchemyOptions(ObjectTypeOptions):
|
||||
|
@ -10,7 +10,7 @@ class SQLAlchemyOptions(ObjectTypeOptions):
|
|||
def __init__(self, *args, **kwargs):
|
||||
super(SQLAlchemyOptions, self).__init__(*args, **kwargs)
|
||||
self.model = None
|
||||
self.identifier = None
|
||||
self.identifier = "id"
|
||||
self.valid_attrs += VALID_ATTRS
|
||||
self.only_fields = None
|
||||
self.exclude_fields = []
|
||||
|
|
|
@ -109,11 +109,15 @@ class SQLAlchemyNode(six.with_metaclass(
|
|||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def to_global_id(self):
|
||||
id_ = getattr(self.instance, self._meta.identifier)
|
||||
return self.global_id(id_)
|
||||
|
||||
@classmethod
|
||||
def get_node(cls, id, info=None):
|
||||
try:
|
||||
model = cls._meta.model
|
||||
identifier = cls._meta.identifier or "id"
|
||||
identifier = cls._meta.identifier
|
||||
query = get_query(model, info)
|
||||
instance = query.filter(getattr(model, identifier) == id).one()
|
||||
return cls(instance)
|
||||
|
|
Loading…
Reference in New Issue
Block a user