From c97647e8bd9e6b5ecd0414152643307a26948f14 Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Sun, 12 Jul 2020 14:00:25 +0200 Subject: [PATCH 1/2] 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 --- gql/client.py | 30 +++++++++++++++++++-------- tests/starwars/test_validation.py | 2 +- tests/test_async_client_validation.py | 2 ++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/gql/client.py b/gql/client.py index 2431ae72..c395fcfd 100644 --- a/gql/client.py +++ b/gql/client.py @@ -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, @@ -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, @@ -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 diff --git a/tests/starwars/test_validation.py b/tests/starwars/test_validation.py index 05feb1ea..00384c99 100644 --- a/tests/starwars/test_validation.py +++ b/tests/starwars/test_validation.py @@ -13,7 +13,7 @@ def local_schema(): @pytest.fixture def typedef_schema(): return Client( - type_def=""" + schema=""" schema { query: Query } diff --git a/tests/test_async_client_validation.py b/tests/test_async_client_validation.py index ec651866..55239f9e 100644 --- a/tests/test_async_client_validation.py +++ b/tests/test_async_client_validation.py @@ -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( @@ -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( From aabce6079a6e30ee08fcd90e00ce883ae0d9dd35 Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Sun, 12 Jul 2020 17:23:27 +0200 Subject: [PATCH 2/2] Fixes after review --- gql/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gql/client.py b/gql/client.py index c395fcfd..fc571a88 100644 --- a/gql/client.py +++ b/gql/client.py @@ -1,6 +1,6 @@ import asyncio import warnings -from typing import Any, AsyncGenerator, Dict, Generator, Optional, Union, cast +from typing import Any, AsyncGenerator, Dict, Generator, Optional, Union from graphql import ( DocumentNode, @@ -31,7 +31,7 @@ def __init__( ): assert not ( type_def and introspection - ), "Cannot provide introspection type definition at the same time." + ), "Cannot provide introspection and type definition at the same time." if type_def: assert ( @@ -51,7 +51,7 @@ def __init__( if isinstance(schema, str): type_def_ast = parse(schema) - schema = cast(GraphQLSchema, build_ast_schema(type_def_ast)) + schema = build_ast_schema(type_def_ast) if transport and fetch_schema_from_transport: assert (