Skip to content

Feature/allow errors to be raised or returned #49

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

Closed
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
15 changes: 13 additions & 2 deletions gql/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

log = logging.getLogger(__name__)


class RetryError(Exception):
"""Custom exception thrown when retry logic fails"""
def __init__(self, retries_count, last_exception):
Expand All @@ -15,9 +16,15 @@ def __init__(self, retries_count, last_exception):
self.last_exception = last_exception


class ResultError(Exception):
"""Custom exception thrown when results contain an errors."""
def __init__(self, message):
super(Exception, self).__init__(message)


class Client(object):
def __init__(self, schema=None, introspection=None, type_def=None, transport=None,
fetch_schema_from_transport=False, retries=0):
fetch_schema_from_transport=False, retries=0, raise_error=False):
assert not(type_def and introspection), 'Cant provide introspection type definition at the same time'
if transport and fetch_schema_from_transport:
assert not schema, 'Cant fetch the schema from transport if is already provided'
Expand All @@ -36,6 +43,7 @@ def __init__(self, schema=None, introspection=None, type_def=None, transport=Non
self.introspection = introspection
self.transport = transport
self.retries = retries
self.raise_error = raise_error

def validate(self, document):
if not self.schema:
Expand All @@ -50,7 +58,10 @@ def execute(self, document, *args, **kwargs):

result = self._get_result(document, *args, **kwargs)
if result.errors:
raise Exception(str(result.errors[0]))
if self.raise_error:
return result
else:
raise ResultError(str(result.errors[0]))

return result.data

Expand Down
16 changes: 16 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

from gql import gql


@pytest.fixture()
def gql_query():
return gql('''
{
myFavoriteFilm: film(id:"RmlsbToz") {
id
title
episodeId
}
}
''')
60 changes: 47 additions & 13 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,67 @@
import mock

from gql import Client, gql
from gql.client import ResultError
from gql.transport.requests import RequestsHTTPTransport


@mock.patch('gql.transport.requests.RequestsHTTPTransport.execute')
def test_retries(execute_mock):
def test_retries(execute_mock, gql_query):
expected_retries = 3
execute_mock.side_effect =Exception("fail")
execute_mock.side_effect = Exception("fail")

client = Client(
retries=expected_retries,
transport=RequestsHTTPTransport(url='http://swapi.graphene-python.org/graphql')
)

query = gql('''
{
myFavoriteFilm: film(id:"RmlsbToz") {
id
title
episodeId
}
}
''')

with pytest.raises(Exception):
client.execute(query)
client.execute(gql_query)

assert execute_mock.call_count == expected_retries


@mock.patch('gql.transport.requests.RequestsHTTPTransport.execute')
def test_client_does_not_raise_exception(execute_mock, gql_query):
mock_response = mock.Mock()
mock_response.errors = None
execute_mock.return_value = mock_response

client = Client(
transport=RequestsHTTPTransport(url='http://swapi.graphene-python.org/graphql'),
raise_error=False
)

assert client.execute(gql_query)


@mock.patch('gql.transport.requests.RequestsHTTPTransport.execute')
def test_client_raises_error_exception(execute_mock, gql_query):

mock_response = mock.Mock()
mock_response.errors = [{'message': 'a real error'}]
execute_mock.return_value = mock_response

client = Client(
transport=RequestsHTTPTransport(url='http://swapi.graphene-python.org/graphql'),
raise_error=False
)

with pytest.raises(ResultError):
client.execute(gql_query)


@mock.patch('gql.transport.requests.RequestsHTTPTransport.execute')
def test_client_returns_errors(execute_mock, gql_query):

mock_response = mock.Mock()
mock_response.errors = [{'message': 'a real error'}]
execute_mock.return_value = mock_response

client = Client(
transport=RequestsHTTPTransport(url='http://swapi.graphene-python.org/graphql'),
raise_error=True
)

response = client.execute(gql_query)
assert response.errors == mock_response.errors