Skip to content

Commit 1f9252f

Browse files
author
evalette
committed
add meta option identifier to SQLAlchemy nodes
1 parent efa67fa commit 1f9252f

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

graphene/contrib/sqlalchemy/options.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
from ...relay.types import Node
33
from ...relay.utils import is_node
44

5-
VALID_ATTRS = ('model', 'only_fields', 'exclude_fields')
5+
VALID_ATTRS = ('model', 'only_fields', 'exclude_fields', 'identifier')
66

77

88
class SQLAlchemyOptions(ObjectTypeOptions):
99

1010
def __init__(self, *args, **kwargs):
1111
super(SQLAlchemyOptions, self).__init__(*args, **kwargs)
1212
self.model = None
13-
self.identifier = None
13+
self.identifier = "id"
1414
self.valid_attrs += VALID_ATTRS
1515
self.only_fields = None
1616
self.exclude_fields = []

graphene/contrib/sqlalchemy/types.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,15 @@ class SQLAlchemyNode(six.with_metaclass(
109109
class Meta:
110110
abstract = True
111111

112+
def to_global_id(self):
113+
id_ = getattr(self.instance, self._meta.identifier)
114+
return self.global_id(id_)
115+
112116
@classmethod
113117
def get_node(cls, id, info=None):
114118
try:
115119
model = cls._meta.model
116-
identifier = cls._meta.identifier or "id"
120+
identifier = cls._meta.identifier
117121
query = get_query(model, info)
118122
instance = query.filter(getattr(model, identifier) == id).one()
119123
return cls(instance)

0 commit comments

Comments
 (0)