Skip to content

Commit 1ec139d

Browse files
authored
Deprecation of type_def argument of Client (#113)
The schema argument will now accept either: - a string (previously passed to the type_def argument) - a GraphQLSchema
1 parent c0bec62 commit 1ec139d

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

gql/client.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import warnings
23
from typing import Any, AsyncGenerator, Dict, Generator, Optional, Union
34

45
from graphql import (
@@ -21,7 +22,7 @@
2122
class Client:
2223
def __init__(
2324
self,
24-
schema: Optional[GraphQLSchema] = None,
25+
schema: Optional[Union[str, GraphQLSchema]] = None,
2526
introspection=None,
2627
type_def: Optional[str] = None,
2728
transport: Optional[Union[Transport, AsyncTransport]] = None,
@@ -30,23 +31,34 @@ def __init__(
3031
):
3132
assert not (
3233
type_def and introspection
33-
), "Cannot provide introspection type definition at the same time."
34-
if transport and fetch_schema_from_transport:
34+
), "Cannot provide introspection and type definition at the same time."
35+
36+
if type_def:
3537
assert (
3638
not schema
37-
), "Cannot fetch the schema from transport if is already provided."
39+
), "Cannot provide type definition and schema at the same time."
40+
warnings.warn(
41+
"type_def is deprecated; use schema instead",
42+
category=DeprecationWarning,
43+
)
44+
schema = type_def
45+
3846
if introspection:
3947
assert (
4048
not schema
4149
), "Cannot provide introspection and schema at the same time."
4250
schema = build_client_schema(introspection)
43-
elif type_def:
51+
52+
if isinstance(schema, str):
53+
type_def_ast = parse(schema)
54+
schema = build_ast_schema(type_def_ast)
55+
56+
if transport and fetch_schema_from_transport:
4457
assert (
4558
not schema
46-
), "Cannot provide type definition and schema at the same time."
47-
type_def_ast = parse(type_def)
48-
schema = build_ast_schema(type_def_ast)
49-
elif schema and not transport:
59+
), "Cannot fetch the schema from transport if is already provided."
60+
61+
if schema and not transport:
5062
transport = LocalSchemaTransport(schema)
5163

5264
# GraphQL schema

tests/starwars/test_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def local_schema():
1313
@pytest.fixture
1414
def typedef_schema():
1515
return Client(
16-
type_def="""
16+
schema="""
1717
schema {
1818
query: Query
1919
}

tests/test_async_client_validation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ async def server_starwars(ws, path):
8282
{"schema": StarWarsSchema},
8383
{"introspection": StarWarsIntrospection},
8484
{"type_def": StarWarsTypeDef},
85+
{"schema": StarWarsTypeDef},
8586
],
8687
)
8788
async def test_async_client_validation(
@@ -124,6 +125,7 @@ async def test_async_client_validation(
124125
{"schema": StarWarsSchema},
125126
{"introspection": StarWarsIntrospection},
126127
{"type_def": StarWarsTypeDef},
128+
{"schema": StarWarsTypeDef},
127129
],
128130
)
129131
async def test_async_client_validation_invalid_query(

0 commit comments

Comments
 (0)