GraphQLについて、下記で簡単に取り上げましたが、今回はもう少し実用的に試してみました。
環境) Python 3.9.6 / Mac(arm64)
インストール)
% pip install flask
% pip install flask_graphql
% pip install flask_cors
% pip install “graphql-core<3”
% pip install “graphene<3”
% pip freeze
aniso8601==7.0.0
blinker==1.7.0
click==8.1.7
Flask==3.0.0
Flask-Cors==4.0.0
Flask-GraphQL==2.0.1
graphene==2.1.9
graphql-core==2.3.2
graphql-relay==2.0.1
graphql-server-core==1.2.0
importlib-metadata==7.0.1
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
promise==2.3
Rx==1.6.3
six==1.16.0
Werkzeug==3.0.1
zipp==3.17.0
※バージョンを下げないと依存性が解決できなかった点がちょっと気になる・・
参考)
https://www.magata.net/memo/index.php?GraphQL%C6%FE%CC%E7#ze68b00b
schema01.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import graphene sample_users = { "1": {"id": 1, "name": "name01", "age": 10}, "2": {"id": 2, "name": "name02", "age": 20}, "3": {"id": 3, "name": "name03", "age": 30} } class User(graphene.ObjectType): id = graphene.ID() name = graphene.String() age = graphene.Int() class Query(graphene.ObjectType): user = graphene.Field(User, id=graphene.ID()) users = graphene.Field(graphene.List(User)) def resolve_user(self, info, id): user_info = None if id: # if info.variable_values["id"] でも可 user_info = sample_users.get(id) # sample_users.get(info.variable_values["id"]) if user_info: return User(**user_info) else: return None def resolve_users(self, info, **kwargs): return [ User(**sample_users[id]) for id in sample_users] class CreateUser(graphene.Mutation): class Arguments: name = graphene.String() age = graphene.Int() created = graphene.Boolean() user = graphene.Field(lambda: User) def mutate(root, info, name, age): id = len(sample_users) + 1 sample_users[str(id)] = {"id": id, "name": name, "age": age} user = User(id=id, name=name, age=age) return CreateUser(user=user, created=True) class MyMutations(graphene.ObjectType): create_user = CreateUser.Field() schema = graphene.Schema(query=Query, mutation=MyMutations) if __name__ == "__main__": query = """ mutation createMutation { createUser(name:"name06", age:60) { user { id name age } created } } """ result = schema.execute(query) print(result.data) query = """ query ListQuery { users { id, name, age } } """ result = schema.execute(query) print(result.data) query = """ query SampleQuery($id: ID) { user(id: $id) { id, name, age } } """ result = schema.execute(query, variable_values={"id": 3}) print(result.data) |
※こちらのmainは動かさず、参考まで。
flask_app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from schema01 import schema from flask import Flask from flask_graphql import GraphQLView from flask_cors import CORS app = Flask(__name__) app.debug = True CORS(app) app.add_url_rule( '/', view_func=GraphQLView.as_view( 'graphql', schema=schema, graphiql=True ) ) if __name__ == '__main__': app.run() |
※graphiql=True でGUIツールを表示
起動)
% pyton flask_app.py
curlでも確認
% curl http://127.0.0.1:5000/ –data query=’mutation { createUser(name:”name04″, age:40) { user {id, name, age}, created }}’
{“data”:{“createUser”:{“user”:{“id”:”4″,”name”:”name04″,”age”:40},”created”:true}}}%
% curl http://127.0.0.1:5000/ –data query=’query { user(id: “4”) { id, name, age}}’
“data”:{“user”:{“id”:”4″,”name”:”name04″,”age”:40}}}%
データの追加も確認できました。永続性は別途考慮する必要があリます。
他の実装もまた調査してみたいです。
参考)
https://github.com/graphql-python/graphene/blob/master/examples/context_example.py
https://docs.graphene-python.org/en/latest/execution/execute/