Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
KingDarBoja authored Oct 17, 2020
2 parents 734b3f0 + e39398a commit d383367
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Lint

on: [pull_request]
on: [push, pull_request]

jobs:
build:
Expand Down
56 changes: 41 additions & 15 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
name: Tests

on: [pull_request]
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
max-parallel: 4
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9-dev"]
os: [ubuntu-latest, windows-latest]
exclude:
- os: windows-latest
python-version: "3.6"
- os: windows-latest
python-version: "3.7"
- os: windows-latest
python-version: "3.9-dev"

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
- name: Test with tox
run: tox
env:
TOXENV: ${{ matrix.toxenv }}

coverage:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
- name: Test with tox
run: tox
env:
TOXENV: ${{ matrix.toxenv }}
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install test dependencies
run: |
python -m pip install --upgrade pip
pip install .[test]
- name: Test with coverage
run: pytest --cov=graphql_server --cov-report=xml tests
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
7 changes: 5 additions & 2 deletions graphql_server/aiohttp/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List

from aiohttp import web
from graphql import GraphQLError
from graphql import ExecutionResult, GraphQLError
from graphql.type.schema import GraphQLSchema

from graphql_server import (
Expand Down Expand Up @@ -152,7 +152,10 @@ async def __call__(self, request):
)

exec_res = (
[await ex for ex in execution_results]
[
ex if ex is None or isinstance(ex, ExecutionResult) else await ex
for ex in execution_results
]
if self.enable_async
else execution_results
)
Expand Down
9 changes: 7 additions & 2 deletions graphql_server/sanic/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import partial
from typing import List

from graphql import GraphQLError
from graphql import ExecutionResult, GraphQLError
from graphql.type.schema import GraphQLSchema
from sanic.response import HTTPResponse, html
from sanic.views import HTTPMethodView
Expand Down Expand Up @@ -105,7 +105,12 @@ async def dispatch_request(self, request, *args, **kwargs):
middleware=self.get_middleware(),
)
exec_res = (
[await ex for ex in execution_results]
[
ex
if ex is None or isinstance(ex, ExecutionResult)
else await ex
for ex in execution_results
]
if self.enable_async
else execution_results
)
Expand Down
18 changes: 18 additions & 0 deletions tests/aiohttp/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,22 @@ def resolver_field_sync(_obj, info):
)


def resolver_field_sync_1(_obj, info):
return "synced_one"


def resolver_field_sync_2(_obj, info):
return "synced_two"


SyncQueryType = GraphQLObjectType(
"SyncQueryType",
{
"a": GraphQLField(GraphQLString, resolve=resolver_field_sync_1),
"b": GraphQLField(GraphQLString, resolve=resolver_field_sync_2),
},
)


AsyncSchema = GraphQLSchema(AsyncQueryType)
SyncSchema = GraphQLSchema(SyncQueryType)
48 changes: 44 additions & 4 deletions tests/aiohttp/test_graphiqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from jinja2 import Environment

from tests.aiohttp.app import create_app, url_string
from tests.aiohttp.schema import AsyncSchema, Schema
from tests.aiohttp.schema import AsyncSchema, Schema, SyncSchema


@pytest.fixture
Expand Down Expand Up @@ -102,11 +102,51 @@ async def test_graphiql_get_subscriptions(app, client):


@pytest.mark.asyncio
@pytest.mark.parametrize("app", [create_app(schema=AsyncSchema, enable_async=True)])
async def test_graphiql_async_schema(app, client):
@pytest.mark.parametrize(
"app", [create_app(schema=AsyncSchema, enable_async=True, graphiql=True)]
)
async def test_graphiql_enabled_async_schema(app, client):
response = await client.get(
url_string(query="{a,b,c}"), headers={"Accept": "text/html"},
)

expected_response = (
(
"{\n"
' "data": {\n'
' "a": "hey",\n'
' "b": "hey2",\n'
' "c": "hey3"\n'
" }\n"
"}"
)
.replace('"', '\\"')
.replace("\n", "\\n")
)
assert response.status == 200
assert expected_response in await response.text()


@pytest.mark.asyncio
@pytest.mark.parametrize(
"app", [create_app(schema=SyncSchema, enable_async=True, graphiql=True)]
)
async def test_graphiql_enabled_sync_schema(app, client):
response = await client.get(
url_string(query="{a,b}"), headers={"Accept": "text/html"},
)

expected_response = (
(
"{\n"
' "data": {\n'
' "a": "synced_one",\n'
' "b": "synced_two"\n'
" }\n"
"}"
)
.replace('"', '\\"')
.replace("\n", "\\n")
)
assert response.status == 200
assert await response.json() == {"data": {"a": "hey", "b": "hey2", "c": "hey3"}}
assert expected_response in await response.text()
18 changes: 18 additions & 0 deletions tests/sanic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,22 @@ def resolver_field_sync(_obj, info):
},
)


def resolver_field_sync_1(_obj, info):
return "synced_one"


def resolver_field_sync_2(_obj, info):
return "synced_two"


SyncQueryType = GraphQLObjectType(
"SyncQueryType",
{
"a": GraphQLField(GraphQLString, resolve=resolver_field_sync_1),
"b": GraphQLField(GraphQLString, resolve=resolver_field_sync_2),
},
)

AsyncSchema = GraphQLSchema(AsyncQueryType)
SyncSchema = GraphQLSchema(SyncQueryType)
31 changes: 28 additions & 3 deletions tests/sanic/test_graphiqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from jinja2 import Environment

from .app import create_app, url_string
from .schema import AsyncSchema
from .schema import AsyncSchema, SyncSchema


@pytest.fixture
Expand Down Expand Up @@ -62,9 +62,9 @@ def test_graphiql_html_is_not_accepted(app):


@pytest.mark.parametrize(
"app", [create_app(graphiql=True, schema=AsyncSchema, enable_async=True)]
"app", [create_app(schema=AsyncSchema, enable_async=True, graphiql=True)]
)
def test_graphiql_asyncio_schema(app):
def test_graphiql_enabled_async_schema(app):
query = "{a,b,c}"
_, response = app.client.get(
uri=url_string(query=query), headers={"Accept": "text/html"}
Expand All @@ -86,3 +86,28 @@ def test_graphiql_asyncio_schema(app):

assert response.status == 200
assert expected_response in response.body.decode("utf-8")


@pytest.mark.parametrize(
"app", [create_app(schema=SyncSchema, enable_async=True, graphiql=True)]
)
def test_graphiql_enabled_sync_schema(app):
query = "{a,b}"
_, response = app.client.get(
uri=url_string(query=query), headers={"Accept": "text/html"}
)

expected_response = (
(
"{\n"
' "data": {\n'
' "a": "synced_one",\n'
' "b": "synced_two"\n'
" }\n"
"}"
)
.replace('"', '\\"')
.replace("\n", "\\n")
)
assert response.status == 200
assert expected_response in response.body.decode("utf-8")

0 comments on commit d383367

Please sign in to comment.