-
-
Save nodezi2099/3bcde8eb1c5e47fc6c5fdc1a2e2fda79 to your computer and use it in GitHub Desktop.
basic flask-graphql app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask | |
| from flask_sqlalchemy import SQLAlchemy | |
| import os | |
| import graphene | |
| from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField | |
| from flask_graphql import GraphQLView | |
| ################################# | |
| app = Flask(__name__) | |
| app.debug = True | |
| basedir = os.path.abspath(os.path.dirname(__file__)) | |
| # 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 | |
| # Modules | |
| db = SQLAlchemy(app) | |
| # Models | |
| 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 | |
| # 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, mutation=Mutation) | |
| #### views | |
| @app.route('/') | |
| def index(): | |
| return '<p> Hello World!</p>' | |
| app.add_url_rule( | |
| '/graphql', | |
| view_func=GraphQLView.as_view( | |
| 'graphql', | |
| schema=schema, | |
| graphiql=True # for having the GraphiQL interface | |
| ) | |
| ) | |
| ################################## | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment