Skip to content

Commit 6220bff

Browse files
authored
Merge branch 'master' into async_sync_graceful_shutdown
2 parents 26c577f + a21ff73 commit 6220bff

File tree

10 files changed

+107
-21
lines changed

10 files changed

+107
-21
lines changed

CODEOWNERS

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/ @syrusakbary @ekampf @cito
1+
/ @syrusakbary @ekampf @cito @leszekhanusz @KingDarBoja

CONTRIBUTING.md

+49-4
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,44 @@ And you ready to start development!
5757

5858
<!-- TODO: Provide environment.yml file for conda env -->
5959

60+
## Coding guidelines
61+
62+
Several tools are used to ensure a coherent coding style.
63+
You need to make sure that your code satisfy those requirements
64+
or the automated tests will fail.
65+
66+
- [black code formatter](https://github.com/psf/black)
67+
- [flake8 style enforcement](https://flake8.pycqa.org/en/latest/index.html)
68+
- [mypy static type checker](http://mypy-lang.org/)
69+
- [isort to sort imports alphabetically](https://isort.readthedocs.io/en/stable/)
70+
71+
On Linux or MacOS, you can fix and check your code style by running
72+
the Makefile command `make check` (this is also checked by running
73+
the automated tests with tox but it is much faster with make)
74+
75+
In addition to the above checks, it is asked that:
76+
77+
- [type hints are used](https://docs.python.org/3/library/typing.html)
78+
- tests are added to ensure complete code coverage
79+
6080
## Running tests
6181

6282
After developing, the full test suite can be evaluated by running:
6383

6484
```sh
65-
pytest tests --cov=gql -vv
85+
pytest tests --cov=gql --cov-report=term-missing -vv
6686
```
6787

68-
If you are using Linux or MacOS, you can make use of Makefile command
69-
`make tests`, which is a shortcut for the above python command.
88+
Please note that some tests which require external online resources are not
89+
done in the automated tests. You can run those tests by running:
90+
91+
```sh
92+
pytest tests --cov=gql --cov-report=term-missing --run-online -vv
93+
```
94+
95+
If you are using Linux or MacOS, you can make use of Makefile commands
96+
`make tests` and `make all_tests`, which are shortcuts for the above
97+
python commands.
7098

7199
You can also test on several python environments by using tox.
72100

@@ -91,7 +119,24 @@ conda install -c conda-forge tox-conda
91119

92120
This install tox underneath so no need to install it before.
93121

94-
Then uncomment the `requires = tox-conda` line on `tox.ini` file.
122+
Then add the line `requires = tox-conda` in the `tox.ini` file under `[tox]`.
95123

96124
Run `tox` and you will see all the environments being created
97125
and all passing tests. :rocket:
126+
127+
## How to create a good Pull Request
128+
129+
1. Make a fork of the master branch on github
130+
2. Clone your forked repo on your computer
131+
3. Create a feature branch `git checkout -b feature_my_awesome_feature`
132+
4. Modify the code
133+
5. Verify that the [Coding guidelines](#coding-guidelines) are respected
134+
6. Verify that the [automated tests](#running-tests) are passing
135+
7. Make a commit and push it to your fork
136+
8. From github, create the pull request. Automated tests from travis
137+
and coveralls will then automatically run the tests and check the code coverage
138+
9. If other modifications are needed, you are free to create more commits and
139+
push them on your branch. They'll get added to the PR automatically.
140+
141+
Once the Pull Request is accepted and merged, you can safely
142+
delete the branch (and the forked repo if no more development is needed).

gql/client.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ def __init__(
7171
session.fetch_schema()
7272

7373
def validate(self, document):
74-
if not self.schema:
75-
raise Exception(
76-
"Cannot validate the document locally, you need to pass a schema."
77-
)
74+
assert (
75+
self.schema
76+
), "Cannot validate the document locally, you need to pass a schema."
77+
7878
validation_errors = validate(self.schema, document)
7979
if validation_errors:
8080
raise validation_errors[0]

gql/dsl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def selection_field(field):
124124
if isinstance(field, DSLField):
125125
return field
126126

127-
raise Exception(f'Received incompatible query field: "{field}".')
127+
raise TypeError(f'Received incompatible query field: "{field}".')
128128

129129

130130
def query(*fields, **kwargs):

gql/gql.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from graphql import Source, parse
1+
from graphql import DocumentNode, Source, parse
22

33

4-
def gql(request_string):
5-
if not isinstance(request_string, str):
6-
raise Exception(f'Received incompatible request "{request_string}".')
4+
def gql(request_string: str) -> DocumentNode:
75
source = Source(request_string, "GraphQL request")
86
return parse(source)

gql/transport/aiohttp.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,15 @@ async def execute(
103103
"""
104104

105105
query_str = print_ast(document)
106-
payload = {
106+
payload: Dict[str, Any] = {
107107
"query": query_str,
108-
"variables": variable_values or {},
109-
"operationName": operation_name or "",
110108
}
111109

110+
if variable_values:
111+
payload["variables"] = variable_values
112+
if operation_name:
113+
payload["operationName"] = operation_name
114+
112115
post_args = {
113116
"json": payload,
114117
}

scripts/gql-cli

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async def main():
2727
elif scheme in ["http", "https"]:
2828
transport = AIOHTTPTransport(url=args.server)
2929
else:
30-
raise Exception("URL protocol should be one of: http, https, ws, wss")
30+
raise ValueError("URL protocol should be one of: http, https, ws, wss")
3131

3232
async with Client(transport=transport) as session:
3333

tests/starwars/test_validation.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ def validation_errors(client, query):
7575

7676

7777
def test_incompatible_request_gql(client):
78-
with pytest.raises(Exception) as exc_info:
78+
with pytest.raises(TypeError) as exc_info:
7979
gql(123)
80-
assert "Received incompatible request" in str(exc_info.value)
80+
assert "body must be a string" in str(exc_info.value)
8181

8282

8383
def test_nested_query_with_fragment(client):

tests/test_aiohttp.py

+40
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,43 @@ async def handler(request):
222222
africa = continents[0]
223223

224224
assert africa["code"] == "AF"
225+
226+
227+
query2_str = """
228+
query getEurope ($code: ID!) {
229+
continent (code: $code) {
230+
name
231+
}
232+
}
233+
"""
234+
235+
query2_server_answer = '{"data": {"continent": {"name": "Europe"}}}'
236+
237+
238+
@pytest.mark.asyncio
239+
async def test_aiohttp_query_variable_values(event_loop, aiohttp_server):
240+
async def handler(request):
241+
return web.Response(text=query2_server_answer, content_type="application/json")
242+
243+
app = web.Application()
244+
app.router.add_route("POST", "/", handler)
245+
server = await aiohttp_server(app)
246+
247+
url = server.make_url("/")
248+
249+
sample_transport = AIOHTTPTransport(url=url, timeout=10)
250+
251+
async with Client(transport=sample_transport,) as session:
252+
253+
params = {"code": "EU"}
254+
255+
query = gql(query2_str)
256+
257+
# Execute query asynchronously
258+
result = await session.execute(
259+
query, variable_values=params, operation_name="getEurope"
260+
)
261+
262+
continent = result["continent"]
263+
264+
assert continent["name"] == "Europe"

tests/test_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_retries_on_transport(execute_mock):
7070

7171

7272
def test_no_schema_exception():
73-
with pytest.raises(Exception) as exc_info:
73+
with pytest.raises(AssertionError) as exc_info:
7474
client = Client()
7575
client.validate("")
7676
assert "Cannot validate the document locally, you need to pass a schema." in str(

0 commit comments

Comments
 (0)