Skip to content

Commit b93c73a

Browse files
committed
Make base views more resilient
1 parent f70388c commit b93c73a

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

src/graphql_server/http/async_base_view.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,10 @@ async def execute_operation(
205205
request_data: GraphQLRequestData,
206206
context: Context,
207207
root_value: Optional[RootValue],
208+
allowed_operation_types: set[OperationType],
208209
) -> ExecutionResult:
209210
request_adapter = self.request_adapter_class(request)
210211

211-
allowed_operation_types = operation_type_from_http(request_adapter.method)
212-
213-
if not self.allow_queries_via_get and request_adapter.method == "GET":
214-
allowed_operation_types = allowed_operation_types - {OperationType.QUERY}
215-
216212
assert self.schema
217213

218214
if request_data.protocol == "multipart-subscription":
@@ -331,12 +327,6 @@ async def run(
331327
request = cast("Request", request)
332328

333329
request_adapter = self.request_adapter_class(request)
334-
sub_response = await self.get_sub_response(request)
335-
context = (
336-
await self.get_context(request, response=sub_response)
337-
if context is UNSET
338-
else context
339-
)
340330

341331
if not self.is_request_allowed(request_adapter):
342332
raise HTTPException(405, "GraphQL only supports GET and POST requests.")
@@ -349,17 +339,38 @@ async def run(
349339
except KeyError as e:
350340
raise HTTPException(400, "File(s) missing in form data") from e
351341

352-
if self.should_render_graphql_ide(request_adapter):
342+
allowed_operation_types = operation_type_from_http(request_adapter.method)
343+
344+
if not self.allow_queries_via_get and request_adapter.method == "GET":
345+
allowed_operation_types = allowed_operation_types - {OperationType.QUERY}
346+
347+
if request_adapter.method == "GET":
348+
if not self.allow_queries_via_get:
349+
allowed_operation_types = allowed_operation_types - {
350+
OperationType.QUERY
351+
}
352+
353+
should_render_graphql_ide = self.should_render_graphql_ide(request_adapter)
353354
if self.graphql_ide:
354-
return await self.render_graphql_ide(request, request_data)
355-
raise HTTPException(404, "Not Found")
355+
if should_render_graphql_ide:
356+
return await self.render_graphql_ide(request, request_data)
357+
elif should_render_graphql_ide:
358+
raise HTTPException(404, "Not Found") # pragma: no cover
359+
360+
sub_response = await self.get_sub_response(request)
361+
context = (
362+
await self.get_context(request, response=sub_response)
363+
if context is UNSET
364+
else context
365+
)
356366

357367
try:
358368
result = await self.execute_operation(
359369
request=request,
360370
request_data=request_data,
361371
context=context,
362372
root_value=root_value,
373+
allowed_operation_types=allowed_operation_types,
363374
)
364375
except GraphQLValidationError as e:
365376
result = ExecutionResult(data=None, errors=e.errors)

src/graphql_server/http/sync_base_view.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,10 @@ def execute_operation(
104104
request_data: GraphQLRequestData,
105105
context: Context,
106106
root_value: Optional[RootValue],
107+
allowed_operation_types: set[OperationType],
107108
) -> ExecutionResult:
108109
request_adapter = self.request_adapter_class(request)
109110

110-
allowed_operation_types = operation_type_from_http(request_adapter.method)
111-
112-
if not self.allow_queries_via_get and request_adapter.method == "GET":
113-
allowed_operation_types = allowed_operation_types - {OperationType.QUERY}
114-
115111
assert self.schema
116112

117113
return execute_sync(
@@ -189,10 +185,20 @@ def run(
189185
except KeyError as e:
190186
raise HTTPException(400, "File(s) missing in form data") from e
191187

192-
if self.should_render_graphql_ide(request_adapter):
188+
allowed_operation_types = operation_type_from_http(request_adapter.method)
189+
190+
if request_adapter.method == "GET":
191+
if not self.allow_queries_via_get:
192+
allowed_operation_types = allowed_operation_types - {
193+
OperationType.QUERY
194+
}
195+
196+
should_render_graphql_ide = self.should_render_graphql_ide(request_adapter)
193197
if self.graphql_ide:
194-
return self.render_graphql_ide(request, request_data)
195-
raise HTTPException(404, "Not Found")
198+
if should_render_graphql_ide:
199+
return self.render_graphql_ide(request, request_data)
200+
elif should_render_graphql_ide:
201+
raise HTTPException(404, "Not Found") # pragma: no cover
196202

197203
sub_response = self.get_sub_response(request)
198204
context = (
@@ -208,6 +214,7 @@ def run(
208214
request_data=request_data,
209215
context=context,
210216
root_value=root_value,
217+
allowed_operation_types=allowed_operation_types,
211218
)
212219
except HTTPException:
213220
raise

0 commit comments

Comments
 (0)