Skip to content

Commit c749030

Browse files
authored
Run additional parse step only when necessary (#51)
1 parent accfef4 commit c749030

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ pip-delete-this-directory.txt
132132
htmlcov/
133133
.tox/
134134
.nox/
135+
.venv/
135136
.coverage
136137
.coverage.*
137138
.cache

graphql_server/__init__.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,23 +236,23 @@ def get_response(
236236
if not params.query:
237237
raise HttpQueryError(400, "Must provide query string.")
238238

239-
# Parse document to trigger a new HttpQueryError if allow_only_query is True
240-
try:
241-
document = parse(params.query)
242-
except GraphQLError as e:
243-
return ExecutionResult(data=None, errors=[e])
244-
except Exception as e:
245-
e = GraphQLError(str(e), original_error=e)
246-
return ExecutionResult(data=None, errors=[e])
247-
248239
if allow_only_query:
240+
# Parse document to check that only query operations are used
241+
try:
242+
document = parse(params.query)
243+
except GraphQLError as e:
244+
return ExecutionResult(data=None, errors=[e])
245+
except Exception as e:
246+
e = GraphQLError(str(e), original_error=e)
247+
return ExecutionResult(data=None, errors=[e])
249248
operation_ast = get_operation_ast(document, params.operation_name)
250249
if operation_ast:
251250
operation = operation_ast.operation.value
252251
if operation != OperationType.QUERY.value:
253252
raise HttpQueryError(
254253
405,
255-
f"Can only perform a {operation} operation from a POST request.", # noqa
254+
f"Can only perform a {operation} operation"
255+
" from a POST request.",
256256
headers={"Allow": "POST"},
257257
)
258258

0 commit comments

Comments
 (0)