Ai
1 Star 0 Fork 0

pashanhu/flask_graphql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.py 3.35 KB
一键复制 编辑 原始数据 按行查看 历史
Your Name 提交于 2019-04-14 22:44 +08:00 . 0
#https://medium.com/@marvinkome/creating-a-graphql-server-with-flask-ae767c7e2525
#https://github.com/Getmrahul/Flask-Graphene-SQLAlchemy/blob/master/schema.py
# Imports
"""
mutation {
createPost(username:"johndoe", title:"Hello 2", body:"Hello body 2"){
post{
title
body
author{
username
}
}
}
}
{
allPosts{
edges{
node{
title
body
author{
username
}
}
}
}
}
"""
from flask import Flask
import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField
from flask_sqlalchemy import SQLAlchemy
from flask_graphql import GraphQLView
import os
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.debug = True
# Configs
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
# Models
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = 'users'
uuid = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(256), index=True, unique=True)
posts = db.relationship('Post', backref='author')
def __repr__(self):
return '<User %r>' % self.username
class Post(db.Model):
__tablename__ = 'posts'
uuid = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(256), index=True)
body = db.Column(db.Text)
author_id = db.Column(db.Integer, db.ForeignKey('users.uuid'))
def __repr__(self):
return '<Post %r>' % self.title
def init():
db.create_all()
john = User(username='johndoe')
post = Post()
post.title = "Hello World"
post.body = "This is the first post"
post.author = john
db.session.add(post)
db.session.add(john)
db.session.commit()
print (User.query.all(), Post.query.all())
# Schema Objects
class PostObject(SQLAlchemyObjectType):
class Meta:
model = Post
interfaces = (graphene.relay.Node, )
class UserObject(SQLAlchemyObjectType):
class Meta:
model = User
interfaces = (graphene.relay.Node, )
class Query(graphene.ObjectType):
node = graphene.relay.Node.Field()
all_posts = SQLAlchemyConnectionField(PostObject)
all_users = SQLAlchemyConnectionField(UserObject)
class CreatePost(graphene.Mutation):
class Arguments:
title = graphene.String(required=True)
body = graphene.String(required=True)
username = graphene.String(required=True)
post = graphene.Field(lambda: PostObject)
def mutate(self, info, title, body, username):
user = User.query.filter_by(username=username).first()
post = Post(title=title, body=body)
if user is not None:
post.author = user
db.session.add(post)
db.session.commit()
return CreatePost(post=post)
class Mutation(graphene.ObjectType):
create_post = CreatePost.Field()
#schema = graphene.Schema(query=Query,)
schema = graphene.Schema(query=Query, mutation=Mutation)
# TO-DO
# Routes
app.add_url_rule(
'/graphql',
view_func=GraphQLView.as_view(
'graphql',
schema=schema,
graphiql=True # for having the GraphiQL interface
)
)
# TO-DO
@app.route('/')
def index():
return '<p> Hello World</p>'
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5555)
#init()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/jpython/flask_graphql.git
git@gitee.com:jpython/flask_graphql.git
jpython
flask_graphql
flask_graphql
master

搜索帮助