Skip to content

Commit 4a1d76b

Browse files
Initialised and setup for the Strawberry (#1509)
* Initialised and setup for the Strawberry * Updated Code * Updated Tests * Updated Code * Update code * updated tests according to suggestion * Update code --------- Co-authored-by: Arkadii Yakovets <2201626+arkid15r@users.noreply.github.com> Co-authored-by: Arkadii Yakovets <arkadii.yakovets@owasp.org>
1 parent 8b4cb61 commit 4a1d76b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1155
-1248
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ repos:
4242
hooks:
4343
- id: mypy
4444
additional_dependencies:
45+
- strawberry-graphql-django
4546
- types-jsonschema
4647
- types-lxml
4748
- types-python-dateutil

backend/apps/common/graphql/__init__.py

Whitespace-only changes.

backend/apps/common/graphql/nodes.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

backend/apps/common/graphql/queries.py

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
"""GitHub issue GraphQL node."""
22

3-
import graphene
3+
import strawberry
4+
import strawberry_django
45

5-
from apps.common.graphql.nodes import BaseNode
6+
from apps.github.graphql.nodes.user import UserNode
67
from apps.github.models.issue import Issue
78

89

9-
class IssueNode(BaseNode):
10+
@strawberry_django.type(
11+
Issue,
12+
fields=[
13+
"created_at",
14+
"state",
15+
"title",
16+
"url",
17+
],
18+
)
19+
class IssueNode:
1020
"""GitHub issue node."""
1121

12-
organization_name = graphene.String()
13-
repository_name = graphene.String()
14-
15-
class Meta:
16-
model = Issue
17-
fields = (
18-
"author",
19-
"created_at",
20-
"state",
21-
"title",
22-
"url",
22+
@strawberry.field
23+
def author(self) -> UserNode | None:
24+
"""Resolve author."""
25+
return self.author
26+
27+
@strawberry.field
28+
def organization_name(self) -> str | None:
29+
"""Resolve organization name."""
30+
return (
31+
self.repository.organization.login
32+
if self.repository and self.repository.organization
33+
else None
2334
)
2435

25-
def resolve_organization_name(self, info) -> str | None:
26-
"""Return organization name."""
27-
return self.repository.organization.login if self.repository.organization else None
28-
29-
def resolve_repository_name(self, info):
36+
@strawberry.field
37+
def repository_name(self) -> str | None:
3038
"""Resolve the repository name."""
31-
return self.repository.name
39+
return self.repository.name if self.repository else None
Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,49 @@
11
"""Github Milestone Node."""
22

3-
import graphene
3+
import strawberry
4+
import strawberry_django
45

5-
from apps.common.graphql.nodes import BaseNode
6+
from apps.github.graphql.nodes.user import UserNode
67
from apps.github.models.milestone import Milestone
78

89

9-
class MilestoneNode(BaseNode):
10+
@strawberry_django.type(
11+
Milestone,
12+
fields=[
13+
"closed_issues_count",
14+
"created_at",
15+
"body",
16+
"open_issues_count",
17+
"title",
18+
"url",
19+
],
20+
)
21+
class MilestoneNode:
1022
"""Github Milestone Node."""
1123

12-
organization_name = graphene.String()
13-
progress = graphene.Float()
14-
repository_name = graphene.String()
15-
16-
class Meta:
17-
model = Milestone
18-
19-
fields = (
20-
"author",
21-
"body",
22-
"created_at",
23-
"title",
24-
"open_issues_count",
25-
"closed_issues_count",
26-
"url",
24+
@strawberry.field
25+
def author(self) -> UserNode | None:
26+
"""Resolve author."""
27+
return self.author
28+
29+
@strawberry.field
30+
def organization_name(self) -> str | None:
31+
"""Resolve organization name."""
32+
return (
33+
self.repository.organization.login
34+
if self.repository and self.repository.organization
35+
else None
2736
)
2837

29-
def resolve_organization_name(self, info):
30-
"""Return organization name."""
31-
return self.repository.organization.login if self.repository.organization else None
32-
33-
def resolve_progress(self, info):
34-
"""Return milestone progress."""
38+
@strawberry.field
39+
def progress(self) -> float:
40+
"""Resolve milestone progress."""
3541
total_issues_count = self.closed_issues_count + self.open_issues_count
3642
if not total_issues_count:
3743
return 0.0
3844
return round((self.closed_issues_count / total_issues_count) * 100, 2)
3945

40-
def resolve_repository_name(self, info):
46+
@strawberry.field
47+
def repository_name(self) -> str | None:
4148
"""Resolve repository name."""
42-
return self.repository.name
49+
return self.repository.name if self.repository else None
Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,46 @@
11
"""GitHub organization GraphQL node."""
22

3-
import graphene
3+
import strawberry
4+
import strawberry_django
45
from django.db import models
56

6-
from apps.common.graphql.nodes import BaseNode
77
from apps.github.models.organization import Organization
88
from apps.github.models.repository import Repository
99
from apps.github.models.repository_contributor import RepositoryContributor
1010

1111

12-
class OrganizationStatsNode(graphene.ObjectType):
12+
@strawberry.type
13+
class OrganizationStatsNode:
1314
"""Organization stats node."""
1415

15-
total_repositories = graphene.Int()
16-
total_contributors = graphene.Int()
17-
total_stars = graphene.Int()
18-
total_forks = graphene.Int()
19-
total_issues = graphene.Int()
16+
total_contributors: int
17+
total_forks: int
18+
total_issues: int
19+
total_repositories: int
20+
total_stars: int
2021

2122

22-
class OrganizationNode(BaseNode):
23+
@strawberry_django.type(
24+
Organization,
25+
fields=[
26+
"avatar_url",
27+
"collaborators_count",
28+
"company",
29+
"created_at",
30+
"description",
31+
"email",
32+
"followers_count",
33+
"location",
34+
"login",
35+
"name",
36+
"updated_at",
37+
],
38+
)
39+
class OrganizationNode:
2340
"""GitHub organization node."""
2441

25-
stats = graphene.Field(OrganizationStatsNode)
26-
url = graphene.String()
27-
28-
class Meta:
29-
model = Organization
30-
fields = (
31-
"avatar_url",
32-
"collaborators_count",
33-
"company",
34-
"created_at",
35-
"description",
36-
"email",
37-
"followers_count",
38-
"location",
39-
"login",
40-
"name",
41-
"updated_at",
42-
)
43-
44-
def resolve_stats(self, info):
42+
@strawberry.field
43+
def stats(self) -> OrganizationStatsNode:
4544
"""Resolve organization stats."""
4645
repositories = Repository.objects.filter(organization=self)
4746

@@ -51,9 +50,10 @@ def resolve_stats(self, info):
5150
total_forks=models.Sum("forks_count"),
5251
total_issues=models.Sum("open_issues_count"),
5352
)
54-
total_stars = aggregated_stats["total_stars"] or 0
55-
total_forks = aggregated_stats["total_forks"] or 0
56-
total_issues = aggregated_stats["total_issues"] or 0
53+
54+
total_stars = aggregated_stats.get("total_stars") or 0
55+
total_forks = aggregated_stats.get("total_forks") or 0
56+
total_issues = aggregated_stats.get("total_issues") or 0
5757

5858
unique_contributors = (
5959
RepositoryContributor.objects.filter(repository__in=repositories)
@@ -70,6 +70,7 @@ def resolve_stats(self, info):
7070
total_issues=total_issues,
7171
)
7272

73-
def resolve_url(self, info):
73+
@strawberry.field
74+
def url(self) -> str:
7475
"""Resolve organization URL."""
7576
return self.url
Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,42 @@
11
"""GitHub Pull Request Node."""
22

3-
import graphene
3+
import strawberry
4+
import strawberry_django
45

5-
from apps.common.graphql.nodes import BaseNode
6+
from apps.github.graphql.nodes.user import UserNode
67
from apps.github.models.pull_request import PullRequest
78

89

9-
class PullRequestNode(BaseNode):
10+
@strawberry_django.type(
11+
PullRequest,
12+
fields=[
13+
"created_at",
14+
"title",
15+
],
16+
)
17+
class PullRequestNode:
1018
"""GitHub pull request node."""
1119

12-
organization_name = graphene.String()
13-
repository_name = graphene.String()
14-
url = graphene.String()
15-
16-
class Meta:
17-
model = PullRequest
18-
fields = (
19-
"author",
20-
"created_at",
21-
"title",
20+
@strawberry.field
21+
def author(self) -> UserNode | None:
22+
"""Resolve author."""
23+
return self.author
24+
25+
@strawberry.field
26+
def organization_name(self) -> str | None:
27+
"""Resolve organization name."""
28+
return (
29+
self.repository.organization.login
30+
if self.repository and self.repository.organization
31+
else None
2232
)
2333

24-
def resolve_organization_name(self, info):
25-
"""Return organization name."""
26-
return self.repository.organization.login if self.repository.organization else None
27-
28-
def resolve_repository_name(self, info):
34+
@strawberry.field
35+
def repository_name(self) -> str | None:
2936
"""Resolve repository name."""
30-
return self.repository.name
37+
return self.repository.name if self.repository else None
3138

32-
def resolve_url(self, info):
39+
@strawberry.field
40+
def url(self) -> str:
3341
"""Resolve URL."""
34-
return self.url
42+
return str(self.url) if self.url else ""

0 commit comments

Comments
 (0)