Skip to content

Check for errors during client.fetch_schema() #328

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/advanced/error_handling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ Here are the possible Transport Errors:
The message of the exception contains the first error returned by the backend.
All the errors messages are available in the exception :code:`errors` attribute.

If the error message begins with :code:`Error while fetching schema:`, it means
that gql was not able to get the schema from the backend.
If you don't need the schema, you can try to create the client with
:code:`fetch_schema_from_transport=False`

- :class:`TransportClosed <gql.transport.exceptions.TransportClosed>`:
This exception is generated when the client is trying to use the transport
while the transport was previously closed.
Expand Down
31 changes: 24 additions & 7 deletions gql/client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import asyncio
import sys
import warnings
from typing import Any, AsyncGenerator, Dict, Generator, Optional, Union, overload
from typing import Any, AsyncGenerator, Dict, Generator, Optional, Union, cast, overload

from graphql import (
DocumentNode,
ExecutionResult,
GraphQLSchema,
IntrospectionQuery,
build_ast_schema,
get_introspection_query,
parse,
Expand Down Expand Up @@ -55,7 +56,7 @@ class Client:
def __init__(
self,
schema: Optional[Union[str, GraphQLSchema]] = None,
introspection=None,
introspection: Optional[IntrospectionQuery] = None,
transport: Optional[Union[Transport, AsyncTransport]] = None,
fetch_schema_from_transport: bool = False,
execute_timeout: Optional[Union[int, float]] = 10,
Expand Down Expand Up @@ -106,7 +107,7 @@ def __init__(
self.schema: Optional[GraphQLSchema] = schema

# Answer of the introspection query
self.introspection = introspection
self.introspection: Optional[IntrospectionQuery] = introspection

# GraphQL transport chosen
self.transport: Optional[Union[Transport, AsyncTransport]] = transport
Expand All @@ -131,6 +132,22 @@ def validate(self, document: DocumentNode):
if validation_errors:
raise validation_errors[0]

def _build_schema_from_introspection(self, execution_result: ExecutionResult):
if execution_result.errors:
raise TransportQueryError(
(
f"Error while fetching schema: {execution_result.errors[0]!s}\n"
"If you don't need the schema, you can try with: "
'"fetch_schema_from_transport=False"'
),
errors=execution_result.errors,
data=execution_result.data,
extensions=execution_result.extensions,
)

self.introspection = cast(IntrospectionQuery, execution_result.data)
self.schema = build_client_schema(self.introspection)

@overload
def execute_sync(
self,
Expand Down Expand Up @@ -802,8 +819,8 @@ def fetch_schema(self) -> None:
Don't use this function and instead set the fetch_schema_from_transport
attribute to True"""
execution_result = self.transport.execute(parse(get_introspection_query()))
self.client.introspection = execution_result.data
self.client.schema = build_client_schema(self.client.introspection)

self.client._build_schema_from_introspection(execution_result)

@property
def transport(self):
Expand Down Expand Up @@ -1175,8 +1192,8 @@ async def fetch_schema(self) -> None:
execution_result = await self.transport.execute(
parse(get_introspection_query())
)
self.client.introspection = execution_result.data
self.client.schema = build_client_schema(self.client.introspection)

self.client._build_schema_from_introspection(execution_result)

@property
def transport(self):
Expand Down
43 changes: 43 additions & 0 deletions tests/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,3 +1190,46 @@ async def handler(request):
africa = continents[0]

assert africa["code"] == "AF"


@pytest.mark.asyncio
async def test_aiohttp_error_fetching_schema(event_loop, aiohttp_server):
from aiohttp import web
from gql.transport.aiohttp import AIOHTTPTransport

error_answer = """
{
"errors": [
{
"errorType": "UnauthorizedException",
"message": "Permission denied"
}
]
}
"""

async def handler(request):
return web.Response(
text=error_answer,
content_type="application/json",
)

app = web.Application()
app.router.add_route("POST", "/", handler)
server = await aiohttp_server(app)

url = server.make_url("/")

transport = AIOHTTPTransport(url=url, timeout=10)

with pytest.raises(TransportQueryError) as exc_info:
async with Client(transport=transport, fetch_schema_from_transport=True):
pass

expected_error = (
"Error while fetching schema: "
"{'errorType': 'UnauthorizedException', 'message': 'Permission denied'}"
)

assert expected_error in str(exc_info.value)
assert transport.session is None
49 changes: 49 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,3 +755,52 @@ def test_code():
f2.close()

await run_sync_test(event_loop, server, test_code)


@pytest.mark.aiohttp
@pytest.mark.asyncio
async def test_requests_error_fetching_schema(
event_loop, aiohttp_server, run_sync_test
):
from aiohttp import web
from gql.transport.requests import RequestsHTTPTransport

error_answer = """
{
"errors": [
{
"errorType": "UnauthorizedException",
"message": "Permission denied"
}
]
}
"""

async def handler(request):
return web.Response(
text=error_answer,
content_type="application/json",
)

app = web.Application()
app.router.add_route("POST", "/", handler)
server = await aiohttp_server(app)

url = server.make_url("/")

def test_code():
transport = RequestsHTTPTransport(url=url)

with pytest.raises(TransportQueryError) as exc_info:
with Client(transport=transport, fetch_schema_from_transport=True):
pass

expected_error = (
"Error while fetching schema: "
"{'errorType': 'UnauthorizedException', 'message': 'Permission denied'}"
)

assert expected_error in str(exc_info.value)
assert transport.session is None

await run_sync_test(event_loop, server, test_code)