Skip to content

Run additional parse step only when necessary (#43) #51

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

Merged
merged 1 commit into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pip-delete-this-directory.txt
htmlcov/
.tox/
.nox/
.venv/
.coverage
.coverage.*
.cache
Expand Down
20 changes: 10 additions & 10 deletions graphql_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,23 +236,23 @@ def get_response(
if not params.query:
raise HttpQueryError(400, "Must provide query string.")

# Parse document to trigger a new HttpQueryError if allow_only_query is True
try:
document = parse(params.query)
except GraphQLError as e:
return ExecutionResult(data=None, errors=[e])
except Exception as e:
e = GraphQLError(str(e), original_error=e)
return ExecutionResult(data=None, errors=[e])

if allow_only_query:
# Parse document to check that only query operations are used
try:
document = parse(params.query)
except GraphQLError as e:
return ExecutionResult(data=None, errors=[e])
except Exception as e:
e = GraphQLError(str(e), original_error=e)
return ExecutionResult(data=None, errors=[e])
operation_ast = get_operation_ast(document, params.operation_name)
if operation_ast:
operation = operation_ast.operation.value
if operation != OperationType.QUERY.value:
raise HttpQueryError(
405,
f"Can only perform a {operation} operation from a POST request.", # noqa
f"Can only perform a {operation} operation"
" from a POST request.",
headers={"Allow": "POST"},
)

Expand Down