Skip to content

Commit f71a277

Browse files
committed
add simple_app.py
1 parent 83c8f42 commit f71a277

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
SHELL=/bin/bash
2+
API_FOLDER=app
3+
VENV_NAME := $(shell [ -d venv ] && echo venv || echo .venv)
4+
VENV_DIR=${VENV_NAME}
5+
PYTHON=$(shell if [ -d $(VENV_DIR) ]; then echo $(VENV_DIR)/bin/python; else echo python; fi)
6+
7+
ifneq (,$(findstring xterm,${TERM}))
8+
BOLD := $(shell tput -Txterm bold)
9+
RED := $(shell tput -Txterm setaf 1)
10+
GREEN := $(shell tput -Txterm setaf 2)
11+
YELLOW := $(shell tput -Txterm setaf 3)
12+
NORMAL := $(shell tput -Txterm sgr0)
13+
endif
14+
15+
install:
16+
$(PYTHON) -m pip install -U pip
17+
$(PYTHON) -m pip install -U -r requirements.txt
18+
19+
run_simple_app:
20+
uvicorn simple_app:app --reload
21+
22+
run:
23+
uvicorn main:app --reload

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# FastAPI with GraphQL
2+
3+
- <https://fastapi.tiangolo.com/how-to/graphql/>
4+
- <https://strawberry.rocks/docs/integrations/fastapi>
5+
- <https://www.geeksforgeeks.org/fastapi-using-graphql/>
6+
7+
## Test simple_app.py
8+
9+
1. run: `make run_simple_app`
10+
1. open requests file: `test_simple_app.http`

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fastapi[uvicorn]
2+
strawberry-graphql

simple_app.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import strawberry
2+
from fastapi import FastAPI
3+
from strawberry.asgi import GraphQL
4+
from strawberry.fastapi import GraphQLRouter
5+
6+
7+
@strawberry.type
8+
class User:
9+
name: str
10+
age: int
11+
12+
13+
@strawberry.type
14+
class Query:
15+
@strawberry.field
16+
def user(self) -> User:
17+
return User(name="Patrick", age=100)
18+
19+
20+
schema = strawberry.Schema(query=Query)
21+
22+
app = FastAPI()
23+
24+
"""
25+
old version from fastapi:
26+
https://fastapi.tiangolo.com/how-to/graphql/
27+
graphql_app = GraphQL(schema)
28+
app.add_route("/graphql", graphql_app)
29+
app.add_websocket_route("/graphql", graphql_app)
30+
"""
31+
32+
# latest version from strawberry:
33+
# https://strawberry.rocks/docs/integrations/fastapi
34+
graphql_app = GraphQLRouter(schema)
35+
app.include_router(graphql_app, prefix="/graphql")

test_simple_app.http

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### only age field
2+
curl -X POST http://127.0.0.1:8000/graphql -H "Content-Type: application/json" -d '{
3+
"query": "{ user { age } }"
4+
}'
5+
6+
### both age and name field
7+
8+
curl -X POST http://127.0.0.1:8000/graphql -H "Content-Type: application/json" -d '{
9+
"query": "{ user { age name} }"
10+
}'
11+
12+
13+
### graphql syntax
14+
# https://notes.dt.in.th/20220907T163410Z4258
15+
POST http://127.0.0.1:8000/graphql
16+
Content-Type: application/json
17+
X-REQUEST-TYPE: GraphQL
18+
19+
query {
20+
user { age name}
21+
}

0 commit comments

Comments
 (0)