|
| 1 | +import pytest |
| 2 | +from graphql.execution.executors.asyncio import AsyncioExecutor |
| 3 | + |
| 4 | +from graphene.types import ID, Field, ObjectType, Schema |
| 5 | +from graphene.types.scalars import String |
| 6 | +from graphene.relay.mutation import ClientIDMutation |
| 7 | + |
| 8 | + |
| 9 | +class SharedFields(object): |
| 10 | + shared = String() |
| 11 | + |
| 12 | + |
| 13 | +class MyNode(ObjectType): |
| 14 | + # class Meta: |
| 15 | + # interfaces = (Node, ) |
| 16 | + id = ID() |
| 17 | + name = String() |
| 18 | + |
| 19 | + |
| 20 | +class SaySomethingAsync(ClientIDMutation): |
| 21 | + class Input: |
| 22 | + what = String() |
| 23 | + |
| 24 | + phrase = String() |
| 25 | + |
| 26 | + @staticmethod |
| 27 | + async def mutate_and_get_payload(self, info, what, client_mutation_id=None): |
| 28 | + return SaySomethingAsync(phrase=str(what)) |
| 29 | + |
| 30 | + |
| 31 | +# MyEdge = MyNode.Connection.Edge |
| 32 | +class MyEdge(ObjectType): |
| 33 | + node = Field(MyNode) |
| 34 | + cursor = String() |
| 35 | + |
| 36 | + |
| 37 | +class OtherMutation(ClientIDMutation): |
| 38 | + class Input(SharedFields): |
| 39 | + additional_field = String() |
| 40 | + |
| 41 | + name = String() |
| 42 | + my_node_edge = Field(MyEdge) |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def mutate_and_get_payload( |
| 46 | + self, info, shared="", additional_field="", client_mutation_id=None |
| 47 | + ): |
| 48 | + edge_type = MyEdge |
| 49 | + return OtherMutation( |
| 50 | + name=shared + additional_field, |
| 51 | + my_node_edge=edge_type(cursor="1", node=MyNode(name="name")), |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +class RootQuery(ObjectType): |
| 56 | + something = String() |
| 57 | + |
| 58 | + |
| 59 | +class Mutation(ObjectType): |
| 60 | + say_promise = SaySomethingAsync.Field() |
| 61 | + other = OtherMutation.Field() |
| 62 | + |
| 63 | + |
| 64 | +schema = Schema(query=RootQuery, mutation=Mutation) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.asyncio |
| 68 | +async def test_node_query_promise(): |
| 69 | + executed = await schema.execute( |
| 70 | + 'mutation a { sayPromise(input: {what:"hello", clientMutationId:"1"}) { phrase } }', |
| 71 | + executor=AsyncioExecutor(), |
| 72 | + return_promise=True, |
| 73 | + ) |
| 74 | + assert not executed.errors |
| 75 | + assert executed.data == {"sayPromise": {"phrase": "hello"}} |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.asyncio |
| 79 | +async def test_edge_query(): |
| 80 | + executed = await schema.execute( |
| 81 | + 'mutation a { other(input: {clientMutationId:"1"}) { clientMutationId, myNodeEdge { cursor node { name }} } }', |
| 82 | + executor=AsyncioExecutor(), |
| 83 | + return_promise=True, |
| 84 | + ) |
| 85 | + assert not executed.errors |
| 86 | + assert dict(executed.data) == { |
| 87 | + "other": { |
| 88 | + "clientMutationId": "1", |
| 89 | + "myNodeEdge": {"cursor": "1", "node": {"name": "name"}}, |
| 90 | + } |
| 91 | + } |
0 commit comments