-
-
Notifications
You must be signed in to change notification settings - Fork 630
Optimize GitHub app IssueNode #4609
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
Open
ahmedxgouda
wants to merge
10
commits into
OWASP:feature/graphql-dataloaders
Choose a base branch
from
ahmedxgouda:dataloaders/github-issue
base: feature/graphql-dataloaders
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c8212ba
Add a custom context and view, a dataloader, and utils
ahmedxgouda 9c8e4ef
Update IssueNode test
ahmedxgouda fee2d8d
Merge branch 'main' into dataloaders/github-issue
ahmedxgouda 4afef11
Apply rabbit suggestion
ahmedxgouda 215e7df
Fix SynchronousOnlyOperation exception
ahmedxgouda e2c05fa
Update tests
ahmedxgouda 2bf92b7
Add dataloaders tests
ahmedxgouda ffd9f8e
Migrate to pytest-asyncio
ahmedxgouda a98ad72
Apply cubic suggestion
ahmedxgouda 3cb194e
Merge branch 'feature/graphql-dataloaders' into dataloaders/github-issue
ahmedxgouda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| """Shared utilities for GraphQL dataloaders.""" | ||
|
|
||
| from collections import defaultdict | ||
|
|
||
| from django.db.models import QuerySet | ||
|
|
||
|
|
||
| async def results_by_keys[K, V]( | ||
| queryset: QuerySet, keys: list[K], key_field: str, value_field: str | ||
| ) -> list[list[V]]: | ||
| """Map a grouped-results dict back to an ordered list matching ``keys``. | ||
|
|
||
| Args: | ||
| queryset: The queryset to iterate over. | ||
| keys: A list of keys to map the results to, in the desired order. | ||
| key_field: The name of the attribute on each item that contains the key. | ||
| value_field: The name of the attribute on each item that contains the value. | ||
|
|
||
| Returns: | ||
| A list of result-lists, one per key, in the same order as ``keys``. | ||
|
|
||
| """ | ||
| mapping: dict[K, list[V]] = defaultdict(list) | ||
| async for item in queryset: | ||
| key = getattr(item, key_field) | ||
| value = getattr(item, value_field) | ||
| mapping[key].append(value) | ||
|
|
||
| return [mapping.get(key, []) for key in keys] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from strawberry.dataloader import DataLoader | ||
|
|
||
| from apps.github.api.internal.dataloaders.interested_users import make_interested_users_loader | ||
|
|
||
|
|
||
| def make_github_dataloaders() -> dict[str, DataLoader]: | ||
| """Return a dict of dataloader instances for GitHub API resolvers.""" | ||
| return { | ||
| "interested_users_loader": make_interested_users_loader(), | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
backend/apps/github/api/internal/dataloaders/interested_users.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| """DataLoader for interested users per issue.""" | ||
|
|
||
| from strawberry.dataloader import DataLoader | ||
|
|
||
| from apps.common.api.internal.dataloaders.utils import results_by_keys | ||
| from apps.github.models.user import User | ||
| from apps.mentorship.models.issue_user_interest import IssueUserInterest | ||
|
|
||
|
|
||
| async def load_interested_users(issue_ids: list[int]) -> list[list[User]]: | ||
| """Batch-load interested users for the given issue IDs in a single query.""" | ||
| interests = ( | ||
| IssueUserInterest.objects.select_related("user__owasp_profile") | ||
| .filter(issue_id__in=issue_ids) | ||
| .order_by("user__login") | ||
| ) | ||
| return await results_by_keys(interests, issue_ids, key_field="issue_id", value_field="user") | ||
|
|
||
|
|
||
| def make_interested_users_loader() -> DataLoader: | ||
| """Return a per-request DataLoader instance.""" | ||
| return DataLoader(load_fn=load_interested_users) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| """Custom GraphQL context for OWASP Nest.""" | ||
|
|
||
| from strawberry.django.context import StrawberryDjangoContext | ||
|
|
||
| from apps.github.api.internal.dataloaders import make_github_dataloaders | ||
|
|
||
|
|
||
| class NestGraphQLContext(StrawberryDjangoContext): | ||
| """Nest GraphQL context.""" | ||
|
|
||
| def __init__(self, *args, **kwargs) -> None: | ||
| """Initialize the context with fresh dataloader instances.""" | ||
| super().__init__(*args, **kwargs) | ||
| self.github_dataloaders = make_github_dataloaders() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.