1
1
import asyncio
2
2
from typing import Any , AsyncGenerator , Dict , Generator , Optional , Union , cast
3
3
4
- from graphql import build_ast_schema , build_client_schema , introspection_query , parse
5
- from graphql .execution import ExecutionResult
6
- from graphql .language .ast import Document
7
- from graphql .type import GraphQLSchema
8
- from graphql .validation import validate
4
+ from graphql import (
5
+ DocumentNode ,
6
+ ExecutionResult ,
7
+ GraphQLSchema ,
8
+ build_ast_schema ,
9
+ build_client_schema ,
10
+ get_introspection_query ,
11
+ parse ,
12
+ validate ,
13
+ )
9
14
10
15
from .transport .async_transport import AsyncTransport
11
16
from .transport .exceptions import TransportQueryError
@@ -32,7 +37,7 @@ def __init__(
32
37
), "Cant fetch the schema from transport if is already provided"
33
38
if isinstance (transport , Transport ):
34
39
# For sync transports, we fetch the schema directly
35
- execution_result = transport .execute (parse (introspection_query ))
40
+ execution_result = transport .execute (parse (get_introspection_query () ))
36
41
execution_result = cast (ExecutionResult , execution_result )
37
42
introspection = execution_result .data
38
43
if introspection :
@@ -72,11 +77,11 @@ def validate(self, document):
72
77
if validation_errors :
73
78
raise validation_errors [0 ]
74
79
75
- async def execute_async (self , document : Document , * args , ** kwargs ) -> Dict :
80
+ async def execute_async (self , document : DocumentNode , * args , ** kwargs ) -> Dict :
76
81
async with self as session :
77
82
return await session .execute (document , * args , ** kwargs )
78
83
79
- def execute (self , document : Document , * args , ** kwargs ) -> Dict :
84
+ def execute (self , document : DocumentNode , * args , ** kwargs ) -> Dict :
80
85
"""Execute the provided document AST against the configured remote server.
81
86
82
87
This function WILL BLOCK until the result is received from the server.
@@ -118,7 +123,7 @@ def execute(self, document: Document, *args, **kwargs) -> Dict:
118
123
return result .data
119
124
120
125
async def subscribe_async (
121
- self , document : Document , * args , ** kwargs
126
+ self , document : DocumentNode , * args , ** kwargs
122
127
) -> AsyncGenerator [Dict , None ]:
123
128
async with self as session :
124
129
@@ -130,7 +135,7 @@ async def subscribe_async(
130
135
yield result
131
136
132
137
def subscribe (
133
- self , document : Document , * args , ** kwargs
138
+ self , document : DocumentNode , * args , ** kwargs
134
139
) -> Generator [Dict , None , None ]:
135
140
"""Execute a GraphQL subscription with a python generator.
136
141
@@ -193,7 +198,7 @@ class ClientSession:
193
198
def __init__ (self , client : Client ):
194
199
self .client = client
195
200
196
- async def validate (self , document : Document ):
201
+ async def validate (self , document : DocumentNode ):
197
202
""" Fetch schema from transport if needed and validate document if schema is present """
198
203
199
204
# Get schema from transport if needed
@@ -205,7 +210,7 @@ async def validate(self, document: Document):
205
210
self .client .validate (document )
206
211
207
212
async def subscribe (
208
- self , document : Document , * args , ** kwargs
213
+ self , document : DocumentNode , * args , ** kwargs
209
214
) -> AsyncGenerator [Dict , None ]:
210
215
211
216
# Fetch schema from transport if needed and validate document if schema is present
@@ -227,7 +232,7 @@ async def subscribe(
227
232
elif result .data is not None :
228
233
yield result .data
229
234
230
- async def execute (self , document : Document , * args , ** kwargs ) -> Dict :
235
+ async def execute (self , document : DocumentNode , * args , ** kwargs ) -> Dict :
231
236
232
237
# Fetch schema from transport if needed and validate document if schema is present
233
238
await self .validate (document )
@@ -245,7 +250,9 @@ async def execute(self, document: Document, *args, **kwargs) -> Dict:
245
250
return result .data
246
251
247
252
async def fetch_schema (self ) -> None :
248
- execution_result = await self .transport .execute (parse (introspection_query ))
253
+ execution_result = await self .transport .execute (
254
+ parse (get_introspection_query ())
255
+ )
249
256
self .client .introspection = execution_result .data
250
257
self .client .schema = build_client_schema (self .client .introspection )
251
258
0 commit comments