This repository has been archived by the owner on Oct 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
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 #24 from preply/provides-support
Provides support
- Loading branch information
Showing
7 changed files
with
116 additions
and
4 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .main import build_schema | ||
from .entity import key | ||
from .extend import extend, external, requires | ||
from .provides import provides |
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,19 @@ | ||
from graphene import Field | ||
|
||
provides_parent_types = set() | ||
|
||
|
||
def provides(field, fields: str = None): | ||
""" | ||
:param field: base type (when used as decorator) or field of base type | ||
:param fields: | ||
:return: | ||
""" | ||
if fields is None: # used as decorator on base type | ||
if isinstance(field, Field): | ||
raise RuntimeError("Please specify fields") | ||
provides_parent_types.add(field) | ||
else: # used as wrapper over field | ||
field._provides = fields | ||
return field |
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
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 |
---|---|---|
|
@@ -145,3 +145,47 @@ def test_requires(): | |
|
||
assert articles == [ | ||
{'id': 1, 'text': 'some text', 'author': {'uppercaseEmail': '[email protected]'}}] | ||
|
||
|
||
def test_provides(): | ||
""" | ||
articles -> w/o provide (get age value from service b) | ||
articlesWithAuthorAgeProvide -> w/ provide (get age value from service c) | ||
:return: | ||
""" | ||
query = { | ||
'query': """ | ||
query { | ||
articles { | ||
id | ||
text | ||
author { | ||
age | ||
} | ||
} | ||
articlesWithAuthorAgeProvide { | ||
id | ||
text | ||
author { | ||
age | ||
} | ||
} | ||
} | ||
""", | ||
'variables': {} | ||
} | ||
response = requests.post( | ||
'http://federation:3000/graphql/', | ||
json=query, | ||
) | ||
assert response.status_code == 200 | ||
data = json.loads(response.content)['data'] | ||
articles = data['articles'] | ||
articles_with_age_provide = data['articlesWithAuthorAgeProvide'] | ||
|
||
assert articles == [ | ||
{'id': 1, 'text': 'some text', 'author': {'age': 17}}] | ||
|
||
assert articles_with_age_provide == [ | ||
{'id': 1, 'text': 'some text', 'author': {'age': 18}}] |