File tree Expand file tree Collapse file tree 5 files changed +91
-0
lines changed Expand file tree Collapse file tree 5 files changed +91
-0
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 `
Original file line number Diff line number Diff line change
1
+ fastapi [uvicorn ]
2
+ strawberry-graphql
Original file line number Diff line number Diff line change
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" )
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments