Skip to content

Commit

Permalink
type_def argument of Client is now deprecated.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
leszekhanusz committed Jul 12, 2020
1 parent faadac4 commit c97647e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
30 changes: 21 additions & 9 deletions gql/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
from typing import Any, AsyncGenerator, Dict, Generator, Optional, Union
import warnings
from typing import Any, AsyncGenerator, Dict, Generator, Optional, Union, cast

from graphql import (
DocumentNode,
Expand All @@ -21,7 +22,7 @@
class Client:
def __init__(
self,
schema: Optional[GraphQLSchema] = None,
schema: Optional[Union[str, GraphQLSchema]] = None,
introspection=None,
type_def: Optional[str] = None,
transport: Optional[Union[Transport, AsyncTransport]] = None,
Expand All @@ -31,22 +32,33 @@ def __init__(
assert not (
type_def and introspection
), "Cannot provide introspection type definition at the same time."
if transport and fetch_schema_from_transport:

if type_def:
assert (
not schema
), "Cannot fetch the schema from transport if is already provided."
), "Cannot provide type definition and schema at the same time."
warnings.warn(
"type_def is deprecated; use schema instead",
category=DeprecationWarning,
)
schema = type_def

if introspection:
assert (
not schema
), "Cannot provide introspection and schema at the same time."
schema = build_client_schema(introspection)
elif type_def:

if isinstance(schema, str):
type_def_ast = parse(schema)
schema = cast(GraphQLSchema, build_ast_schema(type_def_ast))

if transport and fetch_schema_from_transport:
assert (
not schema
), "Cannot provide type definition and schema at the same time."
type_def_ast = parse(type_def)
schema = build_ast_schema(type_def_ast)
elif schema and not transport:
), "Cannot fetch the schema from transport if is already provided."

if schema and not transport:
transport = LocalSchemaTransport(schema)

# GraphQL schema
Expand Down
2 changes: 1 addition & 1 deletion tests/starwars/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def local_schema():
@pytest.fixture
def typedef_schema():
return Client(
type_def="""
schema="""
schema {
query: Query
}
Expand Down
2 changes: 2 additions & 0 deletions tests/test_async_client_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ async def server_starwars(ws, path):
{"schema": StarWarsSchema},
{"introspection": StarWarsIntrospection},
{"type_def": StarWarsTypeDef},
{"schema": StarWarsTypeDef},
],
)
async def test_async_client_validation(
Expand Down Expand Up @@ -124,6 +125,7 @@ async def test_async_client_validation(
{"schema": StarWarsSchema},
{"introspection": StarWarsIntrospection},
{"type_def": StarWarsTypeDef},
{"schema": StarWarsTypeDef},
],
)
async def test_async_client_validation_invalid_query(
Expand Down

0 comments on commit c97647e

Please sign in to comment.