File: test_node_async.py

package info (click to toggle)
graphql-relay 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 416 kB
  • sloc: python: 3,006; sh: 14; makefile: 4
file content (74 lines) | stat: -rw-r--r-- 1,638 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from typing import NamedTuple

from pytest import mark

from graphql import (
    graphql,
    GraphQLField,
    GraphQLID,
    GraphQLNonNull,
    GraphQLObjectType,
    GraphQLSchema,
    GraphQLString,
)

from graphql_relay import node_definitions


class User(NamedTuple):
    id: str
    name: str


user_data = [User(id="1", name="John Doe"), User(id="2", name="Jane Smith")]

user_type: GraphQLObjectType

node_interface, node_field = node_definitions(
    lambda id_, _info: next(filter(lambda obj: obj.id == id_, user_data), None),
    lambda _obj, _info, _type: user_type.name,
)[:2]


user_type = GraphQLObjectType(
    "User",
    lambda: {
        "id": GraphQLField(GraphQLNonNull(GraphQLID)),
        "name": GraphQLField(GraphQLString),
    },
    interfaces=[node_interface],
)

query_type = GraphQLObjectType("Query", lambda: {"node": node_field})

schema = GraphQLSchema(query=query_type, types=[user_type])


def describe_node_interface_and_fields_with_async_object_fetcher():
    @mark.asyncio
    async def gets_the_correct_id_for_users():
        source = """
          {
            node(id: "1") {
              id
            }
          }
        """
        assert await graphql(schema, source) == ({"node": {"id": "1"}}, None)

    @mark.asyncio
    async def gets_the_correct_name_for_users():
        source = """
          {
            node(id: "1") {
              id
              ... on User {
                name
              }
            }
          }
        """
        assert await graphql(schema, source) == (
            {"node": {"id": "1", "name": "John Doe"}},
            None,
        )