add meta option identifier to SQLAlchemy nodes

This commit is contained in:
evalette 2016-04-13 14:06:31 +02:00
parent efa67fa1db
commit 1f9252f26a
2 changed files with 7 additions and 3 deletions

View File

@ -2,7 +2,7 @@ from ...core.classtypes.objecttype import ObjectTypeOptions
from ...relay.types import Node from ...relay.types import Node
from ...relay.utils import is_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): class SQLAlchemyOptions(ObjectTypeOptions):
@ -10,7 +10,7 @@ class SQLAlchemyOptions(ObjectTypeOptions):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(SQLAlchemyOptions, self).__init__(*args, **kwargs) super(SQLAlchemyOptions, self).__init__(*args, **kwargs)
self.model = None self.model = None
self.identifier = None self.identifier = "id"
self.valid_attrs += VALID_ATTRS self.valid_attrs += VALID_ATTRS
self.only_fields = None self.only_fields = None
self.exclude_fields = [] self.exclude_fields = []

View File

@ -109,11 +109,15 @@ class SQLAlchemyNode(six.with_metaclass(
class Meta: class Meta:
abstract = True abstract = True
def to_global_id(self):
id_ = getattr(self.instance, self._meta.identifier)
return self.global_id(id_)
@classmethod @classmethod
def get_node(cls, id, info=None): def get_node(cls, id, info=None):
try: try:
model = cls._meta.model model = cls._meta.model
identifier = cls._meta.identifier or "id" identifier = cls._meta.identifier
query = get_query(model, info) query = get_query(model, info)
instance = query.filter(getattr(model, identifier) == id).one() instance = query.filter(getattr(model, identifier) == id).one()
return cls(instance) return cls(instance)