Skip to content

Commit bdadcef

Browse files
committed
🎉 Initial commit with ORM, Graphene and dev tools
0 parents  commit bdadcef

23 files changed

+1640
-0
lines changed

.env.sample

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# PostgreSQL
2+
POSTGRES_DRIVER=postgres
3+
POSTGRES_SERVER=localhost
4+
POSTGRES_USER=fastapi
5+
POSTGRES_PASSWORD=fastapi
6+
POSTGRES_DB=fastapi_graphql
7+
POSTGRES_PORT=5432

.flake8

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[flake8]
2+
max-line-length = 99
3+
max-complexity = 18
4+
ignore = E203, E266, E501, W503, E231, C901
5+
select = B,C,E,F,W,T4,B9
6+
exclude =
7+
# ignore migrations
8+
*/migrations/*
9+
# ignore thrid-party packages
10+
*/site-packages/*

.gitignore

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
98+
__pypackages__/
99+
100+
# Celery stuff
101+
celerybeat-schedule
102+
celerybeat.pid
103+
104+
# SageMath parsed files
105+
*.sage.py
106+
107+
# Environments
108+
.env
109+
.venv
110+
env/
111+
venv/
112+
ENV/
113+
env.bak/
114+
venv.bak/
115+
116+
# Spyder project settings
117+
.spyderproject
118+
.spyproject
119+
120+
# Rope project settings
121+
.ropeproject
122+
123+
# mkdocs documentation
124+
/site
125+
126+
# mypy
127+
.mypy_cache/
128+
.dmypy.json
129+
dmypy.json
130+
131+
# Pyre type checker
132+
.pyre/
133+
134+
# pytype static type analyzer
135+
.pytype/
136+
137+
# Cython debug symbols
138+
cython_debug/

.pre-commit-config.yaml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
repos:
2+
3+
- repo: https://github.com/pre-commit/pre-commit-hooks.git
4+
rev: v2.4.0
5+
hooks:
6+
- id: check-added-large-files
7+
- id: trailing-whitespace
8+
- id: check-builtin-literals
9+
- id: check-yaml
10+
args: [--unsafe]
11+
- id: check-json
12+
- id: check-docstring-first
13+
- id: check-byte-order-marker
14+
- id: check-case-conflict
15+
- id: requirements-txt-fixer
16+
- id: debug-statements
17+
- id: check-toml
18+
- id: pretty-format-json
19+
args: [--autofix]
20+
language_version: python3.9
21+
- id: end-of-file-fixer
22+
- id: flake8
23+
language_version: python3.9
24+
- id: forbid-new-submodules
25+
- id: mixed-line-ending
26+
27+
- repo: https://github.com/pre-commit/mirrors-mypy
28+
rev: v0.790
29+
hooks:
30+
- id: mypy
31+
name: Mypy typing checks
32+
33+
- repo: https://github.com/psf/black
34+
rev: 19.10b0
35+
hooks:
36+
- id: black
37+
language_version: python3.9
38+
39+
- repo: https://github.com/timothycrosley/isort
40+
rev: "4.3.21"
41+
hooks: [{id: isort}]
42+
43+
- repo: local
44+
hooks:
45+
- id: tests
46+
name: run tests
47+
entry: pytest
48+
pass_filenames: false
49+
language_version: python3.9
50+
language: system

README.md

Whitespace-only changes.

app/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "0.1.0"

app/config.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from pydantic import BaseSettings
2+
3+
4+
class Settings(BaseSettings):
5+
POSTGRES_DRIVER: str = "postgres"
6+
POSTGRES_SERVER: str = "localhost"
7+
POSTGRES_USER: str
8+
POSTGRES_PASSWORD: str
9+
POSTGRES_DB: str
10+
POSTGRES_PORT: int = 5432
11+
12+
class Config:
13+
case_sensitive = True
14+
env_file = ".env"
15+
16+
17+
settings = Settings()

app/db.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from orator import DatabaseManager, Schema, Model
2+
3+
from .config import settings
4+
5+
DATABASES = {
6+
"postgres": {
7+
"driver": settings.POSTGRES_DRIVER,
8+
"host": settings.POSTGRES_SERVER,
9+
"database": settings.POSTGRES_DB,
10+
"user": settings.POSTGRES_USER,
11+
"password": settings.POSTGRES_PASSWORD,
12+
"prefix": "",
13+
"port": settings.POSTGRES_PORT,
14+
}
15+
}
16+
17+
db = DatabaseManager(DATABASES)
18+
schema = Schema(db)
19+
Model.set_connection_resolver(db)

app/main.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import graphene
2+
from fastapi import FastAPI
3+
from starlette.graphql import GraphQLApp
4+
5+
from .schema import Query
6+
7+
8+
app = FastAPI()
9+
10+
app.add_route("/graphql", GraphQLApp(schema=graphene.Schema(query=Query)))
11+
12+
13+
@app.get("/")
14+
async def ping():
15+
return {"ping": "pong!"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from orator.migrations import Migration
2+
3+
4+
class CreateUsersTable(Migration):
5+
def up(self):
6+
"""
7+
Run the migrations.
8+
"""
9+
with self.schema.create("users") as table:
10+
table.increments("id")
11+
table.string("name")
12+
table.text("address")
13+
table.string("phone_number", 11)
14+
table.enum("sex", ["male", "female"])
15+
table.timestamps()
16+
17+
def down(self):
18+
"""
19+
Revert the migrations.
20+
"""
21+
self.schema.drop("users")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from orator.migrations import Migration
2+
3+
4+
class CreatePostsTable(Migration):
5+
def up(self):
6+
"""
7+
Run the migrations.
8+
"""
9+
with self.schema.create("posts") as table:
10+
table.increments("id")
11+
table.integer("user_id").unsigned()
12+
table.foreign("user_id").references("id").on("users")
13+
table.string("title")
14+
table.text("body")
15+
table.timestamps()
16+
17+
def down(self):
18+
"""
19+
Revert the migrations.
20+
"""
21+
self.schema.drop("posts")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from orator.migrations import Migration
2+
3+
4+
class CreateCommentsTable(Migration):
5+
def up(self):
6+
"""
7+
Run the migrations.
8+
"""
9+
with self.schema.create("comments") as table:
10+
table.increments("id")
11+
table.integer("user_id").unsigned().nullable()
12+
table.foreign("user_id").references("id").on("users")
13+
table.integer("post_id").unsigned().nullable()
14+
table.foreign("post_id").references("id").on("posts")
15+
table.text("body")
16+
table.timestamps()
17+
18+
def down(self):
19+
"""
20+
Revert the migrations.
21+
"""
22+
self.schema.drop("comments")

app/migrations/__init__.py

Whitespace-only changes.

app/models/__init__.py

Whitespace-only changes.

app/models/comment.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from db import Model
2+
3+
4+
class Comment(Model):
5+
pass

app/models/post.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from orator.orm import has_many
2+
3+
from db import Model
4+
5+
6+
class Post(Model):
7+
@has_many
8+
def comments(self):
9+
from .comment import Comment
10+
11+
return Comment

app/models/user.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from orator.orm import has_many
2+
3+
from db import Model
4+
5+
6+
class User(Model):
7+
@has_many
8+
def posts(self):
9+
from .post import Post
10+
11+
return Post
12+
13+
@has_many
14+
def comments(self):
15+
from .comment import Comment
16+
17+
return Comment

app/schema.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import graphene
2+
3+
4+
class Query(graphene.ObjectType):
5+
say_hello = graphene.String(name=graphene.String(default_value="Kaka"))
6+
7+
@staticmethod
8+
def resolve_say_hello(parent, info, name):
9+
return f"Hello {name}"

0 commit comments

Comments
 (0)