From 880dd9c98cb1c2211480f8ec1f3f03bcab3b4301 Mon Sep 17 00:00:00 2001
From: Mark <mark@openappstack.net>
Date: Fri, 6 Sep 2019 16:25:09 +0200
Subject: [PATCH] Add basic graphql schema

---
 app.py             | 37 ++++++++++++++++++++++++++++++++++++
 database/schema.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++
 requirements.txt   |  2 ++
 3 files changed, 86 insertions(+)
 create mode 100644 app.py
 create mode 100644 database/schema.py

diff --git a/app.py b/app.py
new file mode 100644
index 0000000..868d1fc
--- /dev/null
+++ b/app.py
@@ -0,0 +1,37 @@
+from flask import Flask
+from flask_security import Security, login_required, \
+     SQLAlchemySessionUserDatastore
+from flask_graphql import GraphQLView
+
+from os import environ
+
+from database.database import db_session, init_db
+from database.schema import schema
+from database.models import User, Role
+
+app = Flask(__name__)
+
+app.debug = True if "DEBUG" in environ and environ["DEBUG"] else False
+
+app.add_url_rule(
+    '/graphql',
+    view_func=GraphQLView.as_view(
+        'graphql',
+        schema=schema,
+        graphiql=True # for having the GraphiQL interface
+    )
+)
+
+user_datastore = SQLAlchemySessionUserDatastore(db_session,
+                                                User, Role)
+
+@app.teardown_appcontext
+def shutdown_session(exception=None):
+    db_session.remove()
+
+@app.before_first_request
+def initialize_database():
+    init_db()
+
+if __name__ == '__main__':
+    app.run()
diff --git a/database/schema.py b/database/schema.py
new file mode 100644
index 0000000..851777b
--- /dev/null
+++ b/database/schema.py
@@ -0,0 +1,47 @@
+import graphene
+from graphene import relay
+from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField
+from database.models import User as UserModel, Application as ApplicationModel, Role as RoleModel
+from database.database import scoped_session
+
+
+class User(SQLAlchemyObjectType):
+    class Meta:
+        model = UserModel
+        interfaces = (relay.Node, )
+
+
+class UserConnections(relay.Connection):
+    class Meta:
+        node = User
+
+
+class Application(SQLAlchemyObjectType):
+    class Meta:
+        model = ApplicationModel
+        interfaces = (relay.Node, )
+
+
+class ApplicationConnections(relay.Connection):
+    class Meta:
+        node = Application
+
+
+class Role(SQLAlchemyObjectType):
+    class Meta:
+        model = RoleModel
+        interfaces = (relay.Node, )
+
+
+class RoleConnections(relay.Connection):
+    class Meta:
+        node = Role
+
+
+class Query(graphene.ObjectType):
+    node = relay.Node.Field()
+    all_users = SQLAlchemyConnectionField(UserConnections)
+    all_application = SQLAlchemyConnectionField(ApplicationConnections)
+    all_roles = SQLAlchemyConnectionField(RoleConnections)
+
+schema = graphene.Schema(query=Query)
diff --git a/requirements.txt b/requirements.txt
index fd57dc5..18fe452 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -2,3 +2,5 @@ sqlalchemy
 psycopg2
 flask-sqlalchemy
 flask-security
+graphene_sqlalchemy
+Flask-GraphQL
-- 
GitLab