diff --git a/graphene/contrib/sqlalchemy/options.py b/graphene/contrib/sqlalchemy/options.py index 3040dc3c..44886287 100644 --- a/graphene/contrib/sqlalchemy/options.py +++ b/graphene/contrib/sqlalchemy/options.py @@ -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 = [] diff --git a/graphene/contrib/sqlalchemy/types.py b/graphene/contrib/sqlalchemy/types.py index 8af80325..f466a1af 100644 --- a/graphene/contrib/sqlalchemy/types.py +++ b/graphene/contrib/sqlalchemy/types.py @@ -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)