-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from mirumee/fix-45-proxy-errors
Proxy errors and extensions in ProxySchema
- Loading branch information
Showing
6 changed files
with
382 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import List, Optional | ||
|
||
from ariadne.types import BaseProxyRootValue, GraphQLResult | ||
|
||
|
||
class ProxyRootValue(BaseProxyRootValue): | ||
__slots__ = ("root_value", "errors", "extensions") | ||
|
||
def __init__( | ||
self, | ||
root_value: Optional[dict] = None, | ||
errors: Optional[List[dict]] = None, | ||
extensions: Optional[dict] = None, | ||
): | ||
super().__init__(root_value) | ||
self.errors = errors | ||
self.extensions = extensions | ||
|
||
def update_result(self, result: GraphQLResult) -> GraphQLResult: | ||
success, data = super().update_result(result) | ||
|
||
if self.errors: | ||
data.setdefault("errors", []) | ||
data["errors"] += self.errors | ||
|
||
if self.extensions: | ||
data.setdefault("extensions", {}) | ||
data["extensions"].update(self.extensions) | ||
|
||
return success, data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
from ariadne_graphql_proxy import ProxyRootValue | ||
|
||
|
||
def test_proxy_root_value_without_errors_or_extensions_skips_result_update(): | ||
result = False, {"data": "ok"} | ||
root_value = ProxyRootValue() | ||
assert root_value.update_result(result) == result | ||
|
||
|
||
def test_proxy_root_value_with_errors_extends_result(): | ||
result = False, {"data": "ok"} | ||
root_value = ProxyRootValue(errors=[{"message": "Test"}]) | ||
assert root_value.update_result(result) == ( | ||
False, | ||
{ | ||
"data": "ok", | ||
"errors": [ | ||
{ | ||
"message": "Test", | ||
}, | ||
], | ||
}, | ||
) | ||
|
||
|
||
def test_proxy_root_value_with_extensions_extends_result(): | ||
result = False, {"data": "ok"} | ||
root_value = ProxyRootValue(extensions={"score": "100"}) | ||
assert root_value.update_result(result) == ( | ||
False, | ||
{ | ||
"data": "ok", | ||
"extensions": { | ||
"score": "100", | ||
}, | ||
}, | ||
) | ||
|
||
|
||
def test_proxy_root_value_with_errors_and_extensions_extends_result(): | ||
result = False, {"data": "ok"} | ||
root_value = ProxyRootValue( | ||
errors=[{"message": "Test"}], | ||
extensions={"score": "100"}, | ||
) | ||
assert root_value.update_result(result) == ( | ||
False, | ||
{ | ||
"data": "ok", | ||
"errors": [ | ||
{ | ||
"message": "Test", | ||
}, | ||
], | ||
"extensions": { | ||
"score": "100", | ||
}, | ||
}, | ||
) | ||
|
||
|
||
def test_proxy_root_value_with_errors_updates_result(): | ||
result = False, {"data": "ok", "errors": [{"message": "Org"}]} | ||
|
||
root_value = ProxyRootValue(errors=[{"message": "Test"}]) | ||
assert root_value.update_result(result) == ( | ||
False, | ||
{ | ||
"data": "ok", | ||
"errors": [ | ||
{ | ||
"message": "Org", | ||
}, | ||
{ | ||
"message": "Test", | ||
}, | ||
], | ||
}, | ||
) | ||
|
||
|
||
def test_proxy_root_value_with_extensions_updates_result(): | ||
result = False, {"data": "ok", "extensions": {"core": True}} | ||
root_value = ProxyRootValue(extensions={"score": "100"}) | ||
assert root_value.update_result(result) == ( | ||
False, | ||
{ | ||
"data": "ok", | ||
"extensions": { | ||
"core": True, | ||
"score": "100", | ||
}, | ||
}, | ||
) | ||
|
||
|
||
def test_proxy_root_value_with_errors_and_extensions_updates_result(): | ||
result = ( | ||
False, | ||
{ | ||
"data": "ok", | ||
"errors": [{"message": "Org"}], | ||
"extensions": {"core": True}, | ||
}, | ||
) | ||
|
||
root_value = ProxyRootValue( | ||
errors=[{"message": "Test"}], | ||
extensions={"score": "100"}, | ||
) | ||
assert root_value.update_result(result) == ( | ||
False, | ||
{ | ||
"data": "ok", | ||
"errors": [ | ||
{ | ||
"message": "Org", | ||
}, | ||
{ | ||
"message": "Test", | ||
}, | ||
], | ||
"extensions": { | ||
"core": True, | ||
"score": "100", | ||
}, | ||
}, | ||
) |
Oops, something went wrong.