2022-06-24 17:12:32 +00:00
|
|
|
"""GraphQL API for SelfPrivacy."""
|
|
|
|
# pylint: disable=too-few-public-methods
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
import asyncio
|
|
|
|
from typing import AsyncGenerator
|
2022-06-24 17:12:32 +00:00
|
|
|
import strawberry
|
2023-04-12 13:13:30 +00:00
|
|
|
from strawberry.types import Info
|
|
|
|
|
2023-04-12 13:59:23 +00:00
|
|
|
from selfprivacy_api.graphql import IsAuthenticated, LocaleExtension
|
2022-06-29 17:39:46 +00:00
|
|
|
from selfprivacy_api.graphql.mutations.api_mutations import ApiMutations
|
2022-08-25 17:03:56 +00:00
|
|
|
from selfprivacy_api.graphql.mutations.job_mutations import JobMutations
|
|
|
|
from selfprivacy_api.graphql.mutations.mutation_interface import GenericMutationReturn
|
|
|
|
from selfprivacy_api.graphql.mutations.services_mutations import ServicesMutations
|
2022-08-01 10:40:40 +00:00
|
|
|
from selfprivacy_api.graphql.mutations.ssh_mutations import SshMutations
|
2022-08-25 17:03:56 +00:00
|
|
|
from selfprivacy_api.graphql.mutations.storage_mutations import StorageMutations
|
2022-07-12 13:24:29 +00:00
|
|
|
from selfprivacy_api.graphql.mutations.system_mutations import SystemMutations
|
2022-06-24 17:12:32 +00:00
|
|
|
|
2022-06-29 17:39:46 +00:00
|
|
|
from selfprivacy_api.graphql.queries.api_queries import Api
|
2022-08-25 17:03:56 +00:00
|
|
|
from selfprivacy_api.graphql.queries.jobs import Job
|
|
|
|
from selfprivacy_api.graphql.queries.services import Services
|
2022-07-25 14:08:31 +00:00
|
|
|
from selfprivacy_api.graphql.queries.storage import Storage
|
2022-06-24 17:12:32 +00:00
|
|
|
from selfprivacy_api.graphql.queries.system import System
|
|
|
|
|
2022-08-01 10:40:40 +00:00
|
|
|
from selfprivacy_api.graphql.mutations.users_mutations import UserMutations
|
|
|
|
from selfprivacy_api.graphql.queries.users import Users
|
2022-08-25 17:03:56 +00:00
|
|
|
from selfprivacy_api.jobs.test import test_job
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2022-06-24 17:12:32 +00:00
|
|
|
|
|
|
|
@strawberry.type
|
|
|
|
class Query:
|
|
|
|
"""Root schema for queries"""
|
2022-06-24 18:14:20 +00:00
|
|
|
|
2022-06-29 17:39:46 +00:00
|
|
|
@strawberry.field(permission_classes=[IsAuthenticated])
|
2022-06-24 18:18:21 +00:00
|
|
|
def system(self) -> System:
|
|
|
|
"""System queries"""
|
|
|
|
return System()
|
2022-06-24 18:14:20 +00:00
|
|
|
|
2022-06-24 17:12:32 +00:00
|
|
|
@strawberry.field
|
|
|
|
def api(self) -> Api:
|
|
|
|
"""API access status"""
|
|
|
|
return Api()
|
|
|
|
|
2022-08-01 10:40:40 +00:00
|
|
|
@strawberry.field(permission_classes=[IsAuthenticated])
|
|
|
|
def users(self) -> Users:
|
|
|
|
"""Users queries"""
|
|
|
|
return Users()
|
|
|
|
|
2022-07-25 14:08:31 +00:00
|
|
|
@strawberry.field(permission_classes=[IsAuthenticated])
|
|
|
|
def storage(self) -> Storage:
|
|
|
|
"""Storage queries"""
|
|
|
|
return Storage()
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@strawberry.field(permission_classes=[IsAuthenticated])
|
|
|
|
def jobs(self) -> Job:
|
|
|
|
"""Jobs queries"""
|
|
|
|
return Job()
|
|
|
|
|
|
|
|
@strawberry.field(permission_classes=[IsAuthenticated])
|
|
|
|
def services(self) -> Services:
|
|
|
|
"""Services queries"""
|
|
|
|
return Services()
|
|
|
|
|
2023-04-12 13:13:30 +00:00
|
|
|
@strawberry.field()
|
|
|
|
def test(self, info: Info) -> str:
|
|
|
|
"""Test query"""
|
2023-04-12 13:59:23 +00:00
|
|
|
return info.context["locale"]
|
2023-04-12 13:13:30 +00:00
|
|
|
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-06-29 17:39:46 +00:00
|
|
|
@strawberry.type
|
2022-08-01 10:40:40 +00:00
|
|
|
class Mutation(
|
|
|
|
ApiMutations,
|
|
|
|
SystemMutations,
|
|
|
|
UserMutations,
|
|
|
|
SshMutations,
|
|
|
|
StorageMutations,
|
2022-08-25 17:03:56 +00:00
|
|
|
ServicesMutations,
|
|
|
|
JobMutations,
|
2022-08-01 10:40:40 +00:00
|
|
|
):
|
2022-06-29 17:39:46 +00:00
|
|
|
"""Root schema for mutations"""
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def test_mutation(self) -> GenericMutationReturn:
|
|
|
|
"""Test mutation"""
|
|
|
|
test_job()
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="Test mutation",
|
|
|
|
code=200,
|
|
|
|
)
|
|
|
|
|
2022-06-29 17:39:46 +00:00
|
|
|
pass
|
2022-06-24 18:14:20 +00:00
|
|
|
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@strawberry.type
|
|
|
|
class Subscription:
|
|
|
|
"""Root schema for subscriptions"""
|
|
|
|
|
|
|
|
@strawberry.subscription(permission_classes=[IsAuthenticated])
|
|
|
|
async def count(self, target: int = 100) -> AsyncGenerator[int, None]:
|
|
|
|
for i in range(target):
|
|
|
|
yield i
|
|
|
|
await asyncio.sleep(0.5)
|
|
|
|
|
|
|
|
|
2023-04-12 13:59:23 +00:00
|
|
|
schema = strawberry.Schema(
|
|
|
|
query=Query,
|
|
|
|
mutation=Mutation,
|
|
|
|
subscription=Subscription,
|
|
|
|
extensions=[LocaleExtension],
|
|
|
|
)
|