Skip to content
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

Fix graphql error import bug #876

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions starlette/graphql.py
Expand Up @@ -10,13 +10,22 @@

try:
import graphene
from graphql.execution.executors.asyncio import AsyncioExecutor
from graphql.error import format_error as format_graphql_error
from graphql.error import GraphQLError
except ImportError: # pragma: nocover
graphene = None

try:
from graphql.execution.executors.asyncio import AsyncioExecutor
except ImportError: # pragma: nocover
AsyncioExecutor = None # type: ignore

try:
from graphql.error import format_error as format_graphql_error
except ImportError: # pragma: nocover
format_graphql_error = None # type: ignore

try:
from graphql.error import GraphQLError
except ImportError: # pragma: nocover
GraphQLError = None # type: ignore


Expand Down
12 changes: 12 additions & 0 deletions tests/test_graphql.py
@@ -1,5 +1,6 @@
import graphene
from graphql.execution.executors.asyncio import AsyncioExecutor
from graphql.error import GraphQLError

from starlette.applications import Starlette
from starlette.datastructures import Headers
Expand All @@ -20,6 +21,7 @@ async def __call__(self, scope, receive, send):
class Query(graphene.ObjectType):
hello = graphene.String(name=graphene.String(default_value="stranger"))
whoami = graphene.String()
graphql_error = graphene.String()

def resolve_hello(self, info, name):
return "Hello " + name
Expand All @@ -31,6 +33,9 @@ def resolve_whoami(self, info):
else info.context["request"]["user"]
)

def resolve_graphql_error(self, info):
raise GraphQLError("GraphQL Error")


schema = graphene.Schema(query=Query)
app = GraphQLApp(schema=schema, graphiql=True)
Expand Down Expand Up @@ -101,6 +106,13 @@ def test_graphiql_get():
assert "<!DOCTYPE html>" in response.text


def test_graphql_error():
response = client.get("/?query={ graphqlError }")
assert response.status_code == 400
assert response.json()["data"] == {"graphqlError": None}
assert response.json()["errors"][0]["message"] == "GraphQL Error"


def test_graphiql_not_found():
app = GraphQLApp(schema=schema, graphiql=False)
client = TestClient(app)
Expand Down