Skip to content

Commit

Permalink
basic graphql server
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Sep 22, 2024
1 parent 2137cd2 commit 9f8fd9c
Show file tree
Hide file tree
Showing 7 changed files with 514 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ dev-env/
/data/
tmp/
.task/
node_modules/
package-lock.json
14 changes: 14 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ tasks:
- a-file-not-exists-so-it-always-rerun
cmd: python start_grpc_server.py

graphql:
dotenv:
- .env
sources:
- '*.py'
- 'chii/**/*.py'
- 'rpc/**/*.py'
- 'gql/**/*.py'
- 'gql/**/*.graphql'
generates:
- a-file-not-exists-so-it-always-rerun
cmds:
- uvicorn gql.app:app

mypy: mypy --show-column-numbers chii rpc

lint:
Expand Down
42 changes: 42 additions & 0 deletions gql/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from pathlib import Path

from ariadne import ObjectType, QueryType, gql, make_executable_schema
from ariadne.asgi import GraphQL
from starlette.applications import Starlette
from starlette.routing import Mount

from chii.const import CollectionType
from gql.model import CollectTimeline

# Define types using Schema Definition Language (https://graphql.org/learn/schema/)
# Wrapping string in gql function provides validation and better error traceback
type_defs = gql(
Path(__file__, "..", "schema.graphql").resolve().read_text(encoding="utf8")
)

# Map resolver functions to Query fields using QueryType
query = QueryType()


# Resolvers are simple python functions
@query.field("timeline_collection")
async def timeline_collection(*_):
return [
CollectTimeline(
action=CollectionType.wish, subject_name="test", subject_name_cn="test2"
),
]


# Map resolver functions to custom type fields using ObjectType
person = ObjectType("CollectTimeline")

# Create executable GraphQL schema
schema = make_executable_schema(type_defs, query, person)

app = Starlette(
debug=True,
routes=[
Mount("/graphql", GraphQL(schema, debug=True)),
],
)
8 changes: 8 additions & 0 deletions gql/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass


@dataclass(kw_only=True)
class CollectTimeline:
action: int
subject_name: str
subject_name_cn: str
9 changes: 9 additions & 0 deletions gql/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type Query {
timeline_collection:[CollectTimeline!]!
}

type CollectTimeline {
action: Int!
subject_name: String!
subject_name_cn: String!
}
433 changes: 432 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pydantic-settings = "2.5.2"
typing-extensions = '4.12.2'
sslog = "0.0.0a45"
protobuf = "5.27.2"
ariadne = "^0.23.0"
uvicorn = { version = "^0.30.6", extras = ['standard'] }

[tool.poetry.group.dev.dependencies]
sqlacodegen = "3.0.0rc5"
Expand Down Expand Up @@ -111,6 +113,7 @@ ignore = [
'N806',
'N802',
'N803',
'N999',
'E501',
'BLE001',
'RUF002',
Expand All @@ -135,7 +138,11 @@ ignore = [
'PLR0912',
'PLR0915',
'PLR2004',
'SIM102',
'TRY002',
'ERA001',
'PGH003',
'INP001',
]

[tool.ruff.lint.flake8-tidy-imports.banned-api]
Expand Down

0 comments on commit 9f8fd9c

Please sign in to comment.