Skip to content

Commit 64ac830

Browse files
committed
Making code more DRY
1 parent 304fb72 commit 64ac830

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

gql/client.py

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import asyncio
22
import sys
33
import warnings
4-
from typing import Any, AsyncGenerator, Dict, Generator, Optional, Union, overload
4+
from typing import Any, AsyncGenerator, Dict, Generator, Optional, Union, cast, overload
55

66
from graphql import (
77
DocumentNode,
88
ExecutionResult,
99
GraphQLSchema,
10+
IntrospectionQuery,
1011
build_ast_schema,
1112
get_introspection_query,
1213
parse,
@@ -55,7 +56,7 @@ class Client:
5556
def __init__(
5657
self,
5758
schema: Optional[Union[str, GraphQLSchema]] = None,
58-
introspection=None,
59+
introspection: Optional[IntrospectionQuery] = None,
5960
transport: Optional[Union[Transport, AsyncTransport]] = None,
6061
fetch_schema_from_transport: bool = False,
6162
execute_timeout: Optional[Union[int, float]] = 10,
@@ -106,7 +107,7 @@ def __init__(
106107
self.schema: Optional[GraphQLSchema] = schema
107108

108109
# Answer of the introspection query
109-
self.introspection = introspection
110+
self.introspection: Optional[IntrospectionQuery] = introspection
110111

111112
# GraphQL transport chosen
112113
self.transport: Optional[Union[Transport, AsyncTransport]] = transport
@@ -131,6 +132,22 @@ def validate(self, document: DocumentNode):
131132
if validation_errors:
132133
raise validation_errors[0]
133134

135+
def _build_schema_from_introspection(self, execution_result: ExecutionResult):
136+
if execution_result.errors:
137+
raise TransportQueryError(
138+
(
139+
f"Error while fetching schema: {execution_result.errors[0]!s}\n"
140+
"If you don't need the schema, you can try with: "
141+
'"fetch_schema_from_transport=False"'
142+
),
143+
errors=execution_result.errors,
144+
data=execution_result.data,
145+
extensions=execution_result.extensions,
146+
)
147+
148+
self.introspection = cast(IntrospectionQuery, execution_result.data)
149+
self.schema = build_client_schema(self.introspection)
150+
134151
@overload
135152
def execute_sync(
136153
self,
@@ -803,20 +820,7 @@ def fetch_schema(self) -> None:
803820
attribute to True"""
804821
execution_result = self.transport.execute(parse(get_introspection_query()))
805822

806-
if execution_result.errors:
807-
raise TransportQueryError(
808-
(
809-
f"Error while fetching schema: {execution_result.errors[0]!s}\n"
810-
"If you don't need the schema, you can try with: "
811-
'"fetch_schema_from_transport=False"'
812-
),
813-
errors=execution_result.errors,
814-
data=execution_result.data,
815-
extensions=execution_result.extensions,
816-
)
817-
818-
self.client.introspection = execution_result.data
819-
self.client.schema = build_client_schema(self.client.introspection)
823+
self.client._build_schema_from_introspection(execution_result)
820824

821825
@property
822826
def transport(self):
@@ -1189,20 +1193,7 @@ async def fetch_schema(self) -> None:
11891193
parse(get_introspection_query())
11901194
)
11911195

1192-
if execution_result.errors:
1193-
raise TransportQueryError(
1194-
(
1195-
f"Error while fetching schema: {execution_result.errors[0]!s}\n"
1196-
"If you don't need the schema, you can try with: "
1197-
'"fetch_schema_from_transport=False"'
1198-
),
1199-
errors=execution_result.errors,
1200-
data=execution_result.data,
1201-
extensions=execution_result.extensions,
1202-
)
1203-
1204-
self.client.introspection = execution_result.data
1205-
self.client.schema = build_client_schema(self.client.introspection)
1196+
self.client._build_schema_from_introspection(execution_result)
12061197

12071198
@property
12081199
def transport(self):

0 commit comments

Comments
 (0)