Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type_def argument of Client is now deprecated. #113

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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."
leszekhanusz marked this conversation as resolved.
Show resolved Hide resolved
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))
leszekhanusz marked this conversation as resolved.
Show resolved Hide resolved

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