Skip to content

Commit c97647e

Browse files
committed
type_def argument of Client is now deprecated.
You should use 'schema=' now instead of 'type_def=' The schema argument will accept either: - a string (previously passed to the type_def argument) - a GraphQLSchema
1 parent faadac4 commit c97647e

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,5 +1,6 @@
11
import asyncio
2-
from typing import Any, AsyncGenerator, Dict, Generator, Optional, Union
2+
import warnings
3+
from typing import Any, AsyncGenerator, Dict, Generator, Optional, Union, cast
34

45
from graphql import (
56
DocumentNode,
@@ -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,
@@ -31,22 +32,33 @@ def __init__(
3132
assert not (
3233
type_def and introspection
3334
), "Cannot provide introspection type definition at the same time."
34-
if transport and fetch_schema_from_transport:
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 = cast(GraphQLSchema, 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)