|
7 | 7 | from graphene.contrib.sqlalchemy import (SQLAlchemyConnectionField,
|
8 | 8 | SQLAlchemyNode, SQLAlchemyObjectType)
|
9 | 9 |
|
10 |
| -from .models import Article, Base, Reporter |
| 10 | +from .models import Article, Base, Reporter, Editor |
11 | 11 |
|
12 | 12 | db = create_engine('sqlite:///test_sqlalchemy.sqlite3')
|
13 | 13 |
|
@@ -37,6 +37,8 @@ def setup_fixtures(session):
|
37 | 37 | session.add(reporter2)
|
38 | 38 | article = Article(headline='Hi!')
|
39 | 39 | session.add(article)
|
| 40 | + editor = Editor(name="John") |
| 41 | + session.add(editor) |
40 | 42 | session.commit()
|
41 | 43 |
|
42 | 44 |
|
@@ -187,3 +189,51 @@ def resolve_article(self, *args, **kwargs):
|
187 | 189 | result = schema.execute(query)
|
188 | 190 | assert not result.errors
|
189 | 191 | assert result.data == expected
|
| 192 | + |
| 193 | + |
| 194 | +def test_should_custom_identifier(session): |
| 195 | + setup_fixtures(session) |
| 196 | + |
| 197 | + class EditorNode(SQLAlchemyNode): |
| 198 | + |
| 199 | + class Meta: |
| 200 | + model = Editor |
| 201 | + identifier = "editor_id" |
| 202 | + |
| 203 | + class Query(graphene.ObjectType): |
| 204 | + node = relay.NodeField(EditorNode) |
| 205 | + all_editors = SQLAlchemyConnectionField(EditorNode) |
| 206 | + |
| 207 | + query = ''' |
| 208 | + query EditorQuery { |
| 209 | + allEditors { |
| 210 | + edges { |
| 211 | + node { |
| 212 | + id, |
| 213 | + name |
| 214 | + } |
| 215 | + } |
| 216 | + }, |
| 217 | + node(id: "RWRpdG9yTm9kZTox") { |
| 218 | + name |
| 219 | + } |
| 220 | + } |
| 221 | + ''' |
| 222 | + expected = { |
| 223 | + 'allEditors': { |
| 224 | + 'edges': [{ |
| 225 | + 'node': { |
| 226 | + 'id': 'RWRpdG9yTm9kZTox', |
| 227 | + 'name': 'John' |
| 228 | + } |
| 229 | + }] |
| 230 | + }, |
| 231 | + 'node': { |
| 232 | + 'name': 'John' |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + schema = graphene.Schema(query=Query, session=session) |
| 237 | + result = schema.execute(query) |
| 238 | + assert not result.errors |
| 239 | + assert result.data == expected |
0 commit comments