File: test_relay.py

package info (click to toggle)
strawberry-graphql-django 0.62.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,968 kB
  • sloc: python: 27,530; sh: 17; makefile: 16
file content (84 lines) | stat: -rw-r--r-- 1,886 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
75
76
77
78
79
80
81
82
83
84
import strawberry
from strawberry import auto, relay
from typing_extensions import Self

import strawberry_django

from .models import User

# textdedent not possible because of comments
expected_schema = '''
"""An object with a Globally Unique ID"""
interface Node {
  """The Globally Unique ID of this object"""
  id: ID!
}

type Query {
  user: UserType!
}

type UserType implements Node {
  """The Globally Unique ID of this object"""
  id: ID!
  name: String!
}
'''


def test_relay_with_nodeid():
    @strawberry_django.type(User)
    class UserType(relay.Node):
        id: relay.NodeID[int]
        name: auto

    @strawberry.type
    class Query:
        user: UserType

    schema = strawberry.Schema(query=Query)
    # test print_schema

    assert str(schema) == expected_schema.strip()
    # check that resolve_id_attr resolves correctly
    assert UserType.resolve_id_attr() == "id"


def test_relay_with_resolve_id_attr():
    @strawberry_django.type(User)
    class UserType(relay.Node):
        name: auto

        @classmethod
        def resolve_id_attr(cls):
            return "foobar"

    @strawberry.type
    class Query:
        user: UserType

    # Crash because of early check
    schema = strawberry.Schema(query=Query)
    # test print_schema
    assert str(schema) == expected_schema.strip()


def test_relay_with_resolve_id_and_node_id():
    @strawberry_django.type(User)
    class UserType(relay.Node):
        id: relay.NodeID[int]
        name: auto

        @classmethod
        def resolve_id(cls, root: Self, *, info):  # type: ignore
            return str(root.id)

    @strawberry.type
    class Query:
        user: UserType

    schema = strawberry.Schema(query=Query)
    # test print_schema
    assert str(schema) == expected_schema.strip()
    # check that resolve_id_attr resolves correctly
    assert UserType.resolve_id_attr() == "id"