Skip to content

Commit

Permalink
make context sharing configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaniyaps committed Jan 21, 2025
1 parent eb6c9cb commit a885b67
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
36 changes: 25 additions & 11 deletions strawberry/http/async_base_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,31 @@ async def execute_operation(

if isinstance(request_data, list):
# batch GraphQL requests
tasks = [
self.execute_single(
request=request,
request_adapter=request_adapter,
sub_response=sub_response,
context=context,
root_value=root_value,
request_data=data,
)
for data in request_data
]
if not self.schema.config.batching_config["share_context"]:
tasks = [
self.execute_single(
request=request,
request_adapter=request_adapter,
sub_response=sub_response,
# create a new context for each request data
context=await self.get_context(request, response=sub_response),
root_value=root_value,
request_data=data,
)
for data in request_data
]
else:
tasks = [
self.execute_single(
request=request,
request_adapter=request_adapter,
sub_response=sub_response,
context=context,
root_value=root_value,
request_data=data,
)
for data in request_data
]

return await asyncio.gather(*tasks)

Expand Down
13 changes: 13 additions & 0 deletions strawberry/http/sync_base_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ def execute_operation(

if isinstance(request_data, list):
# batch GraphQL requests
if not self.schema.config.batching_config["share_context"]:
return [
self.execute_single(
request=request,
request_adapter=request_adapter,
sub_response=sub_response,
# create a new context for each request data
context=self.get_context(request, response=sub_response),
root_value=root_value,
request_data=data,
)
for data in request_data
]
return [
self.execute_single(
request=request,
Expand Down
1 change: 1 addition & 0 deletions strawberry/schema/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class BatchingConfig(TypedDict, total=False):
enabled: Required[bool]
max_operations: int
share_context: Required[bool]


@dataclass
Expand Down

0 comments on commit a885b67

Please sign in to comment.