File: test_execution.py

package info (click to toggle)
strawberry-graphql 0.306.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,176 kB
  • sloc: javascript: 178,052; python: 65,643; sh: 33; makefile: 25
file content (171 lines) | stat: -rw-r--r-- 4,884 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import strawberry
from strawberry.extensions import SchemaExtension


@strawberry.type
class Query:
    @strawberry.field
    def ping(self) -> str:
        return "pong"


def test_execution_context_operation_name_and_type():
    operation_name = None
    operation_type = None

    class MyExtension(SchemaExtension):
        def on_operation(self):
            yield
            nonlocal operation_name
            nonlocal operation_type

            execution_context = self.execution_context

            operation_name = execution_context.operation_name
            operation_type = execution_context.operation_type.value

    schema = strawberry.Schema(Query, extensions=[MyExtension])

    result = schema.execute_sync("{ ping }")
    assert not result.errors

    assert operation_name is None
    assert operation_type == "query"

    # Try again with an operation_name
    result = schema.execute_sync("query MyOperation { ping }")
    assert not result.errors

    assert operation_name == "MyOperation"
    assert operation_type == "query"

    # Try again with an operation_name override
    result = schema.execute_sync(
        """
        query MyOperation { ping }
        query MyOperation2 { ping }
        """,
        operation_name="MyOperation2",
    )
    assert not result.errors

    assert operation_name == "MyOperation2"
    assert operation_type == "query"


def test_execution_context_operation_type_mutation():
    operation_name = None
    operation_type = None

    class MyExtension(SchemaExtension):
        def on_operation(self):
            yield
            nonlocal operation_name
            nonlocal operation_type

            execution_context = self.execution_context

            operation_name = execution_context.operation_name
            operation_type = execution_context.operation_type.value

    @strawberry.type
    class Mutation:
        @strawberry.mutation
        def my_mutation(self) -> str:
            return "hi"

    schema = strawberry.Schema(Query, mutation=Mutation, extensions=[MyExtension])

    result = schema.execute_sync("mutation { myMutation }")
    assert not result.errors

    assert operation_name is None
    assert operation_type == "mutation"

    # Try again with an operation_name
    result = schema.execute_sync("mutation MyMutation { myMutation }")
    assert not result.errors

    assert operation_name == "MyMutation"
    assert operation_type == "mutation"

    # Try again with an operation_name override
    result = schema.execute_sync(
        """
        mutation MyMutation { myMutation }
        mutation MyMutation2 { myMutation }
        """,
        operation_name="MyMutation2",
    )
    assert not result.errors

    assert operation_name == "MyMutation2"
    assert operation_type == "mutation"


def test_execution_context_operation_name_and_type_with_fragments():
    operation_name = None
    operation_type = None

    class MyExtension(SchemaExtension):
        def on_operation(self):
            yield
            nonlocal operation_name
            nonlocal operation_type

            execution_context = self.execution_context

            operation_name = execution_context.operation_name
            operation_type = execution_context.operation_type.value

    schema = strawberry.Schema(Query, extensions=[MyExtension])

    result = schema.execute_sync(
        """
        fragment MyFragment on Query {
            ping
        }

        query MyOperation {
            ping
            ...MyFragment
        }
        """
    )
    assert not result.errors

    assert operation_name == "MyOperation"
    assert operation_type == "query"


def test_error_when_accessing_operation_type_before_parsing():
    class MyExtension(SchemaExtension):
        def on_operation(self):
            execution_context = self.execution_context

            # This should raise a RuntimeError
            execution_context.operation_type

    schema = strawberry.Schema(Query, extensions=[MyExtension])

    result = schema.execute_sync("mutation { myMutation }")
    assert len(result.errors) == 1
    assert isinstance(result.errors[0].original_error, RuntimeError)
    assert result.errors[0].message == "No GraphQL document available"


def test_error_when_accessing_operation_type_with_invalid_operation_name():
    class MyExtension(SchemaExtension):
        def on_parse(self):
            yield
            execution_context = self.execution_context

            # This should raise a RuntimeError
            execution_context.operation_type

    schema = strawberry.Schema(Query, extensions=[MyExtension])

    result = schema.execute_sync("query { ping }", operation_name="MyQuery")
    assert len(result.errors) == 1
    assert isinstance(result.errors[0].original_error, RuntimeError)
    assert result.errors[0].message == "Can't get GraphQL operation type"