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 3 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
18 changes: 18 additions & 0 deletions gql/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,15 @@ 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()))

if execution_result.errors:
raise TransportQueryError(
f"Error while fetching schema: {execution_result.errors[0]!s}",
errors=execution_result.errors,
data=execution_result.data,
extensions=execution_result.extensions,
)

self.client.introspection = execution_result.data
self.client.schema = build_client_schema(self.client.introspection)

Expand Down Expand Up @@ -1175,6 +1184,15 @@ async def fetch_schema(self) -> None:
execution_result = await self.transport.execute(
parse(get_introspection_query())
)

if execution_result.errors:
raise TransportQueryError(
f"Error while fetching schema: {execution_result.errors[0]!s}",
errors=execution_result.errors,
data=execution_result.data,
extensions=execution_result.extensions,
)

self.client.introspection = execution_result.data
self.client.schema = build_client_schema(self.client.introspection)

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)