|
9 | 9 |
|
10 | 10 | from graphene import ( |
11 | 11 | Boolean, |
| 12 | + DefaultGlobalIDType, |
12 | 13 | Dynamic, |
13 | 14 | Field, |
14 | 15 | Float, |
|
42 | 43 | from .models import ( |
43 | 44 | Article, |
44 | 45 | CompositeFullName, |
| 46 | + CompositePrimaryKeyTestModel, |
45 | 47 | Employee, |
46 | 48 | NonAbstractPerson, |
47 | 49 | Person, |
@@ -513,6 +515,55 @@ async def resolve_reporter(self, _info): |
513 | 515 | # Test Custom SQLAlchemyObjectType Implementation |
514 | 516 |
|
515 | 517 |
|
| 518 | +@pytest.mark.asyncio |
| 519 | +async def test_composite_id_resolver(session): |
| 520 | + """Test that the correct resolver functions are called""" |
| 521 | + |
| 522 | + composite_reporter = CompositePrimaryKeyTestModel( |
| 523 | + first_name="graphql", last_name="foundation" |
| 524 | + ) |
| 525 | + |
| 526 | + session.add(composite_reporter) |
| 527 | + await eventually_await_session(session, "commit") |
| 528 | + |
| 529 | + class CompositePrimaryKeyTestModelType(SQLAlchemyObjectType): |
| 530 | + class Meta: |
| 531 | + model = CompositePrimaryKeyTestModel |
| 532 | + interfaces = (Node,) |
| 533 | + |
| 534 | + class Query(ObjectType): |
| 535 | + composite_reporter = Field(CompositePrimaryKeyTestModelType) |
| 536 | + |
| 537 | + async def resolve_composite_reporter(self, _info): |
| 538 | + session = utils.get_session(_info.context) |
| 539 | + if SQL_VERSION_HIGHER_EQUAL_THAN_1_4 and isinstance(session, AsyncSession): |
| 540 | + return ( |
| 541 | + (await session.scalars(select(CompositePrimaryKeyTestModel))) |
| 542 | + .unique() |
| 543 | + .first() |
| 544 | + ) |
| 545 | + return session.query(CompositePrimaryKeyTestModel).first() |
| 546 | + |
| 547 | + schema = Schema(query=Query) |
| 548 | + result = await schema.execute_async( |
| 549 | + """ |
| 550 | + query { |
| 551 | + compositeReporter { |
| 552 | + id |
| 553 | + firstName |
| 554 | + lastName |
| 555 | + } |
| 556 | + } |
| 557 | + """, |
| 558 | + context_value={"session": session}, |
| 559 | + ) |
| 560 | + |
| 561 | + assert not result.errors |
| 562 | + assert result.data["compositeReporter"]["id"] == DefaultGlobalIDType.to_global_id( |
| 563 | + CompositePrimaryKeyTestModelType, str(("graphql", "foundation")) |
| 564 | + ) |
| 565 | + |
| 566 | + |
516 | 567 | def test_custom_objecttype_registered(): |
517 | 568 | class CustomSQLAlchemyObjectType(SQLAlchemyObjectType): |
518 | 569 | class Meta: |
|
0 commit comments