This is an automated email from the ASF dual-hosted git repository.

hugh pushed a commit to branch graphql
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 43614439975502a7a0cda0f1d76c21dd92ea2cad
Author: hughhhh <[email protected]>
AuthorDate: Tue Aug 31 22:08:51 2021 -0700

    working database query
---
 setup.cfg                     |  2 +-
 superset/views/__init__.py    |  1 +
 superset/views/graphql.py     | 50 +++++++++++++++++++++++++++++++++++++++++++
 superset/views/schema.graphql | 22 +++++++++++++++++++
 4 files changed, 74 insertions(+), 1 deletion(-)

diff --git a/setup.cfg b/setup.cfg
index 9a108f7..a1b2b27 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -30,7 +30,7 @@ combine_as_imports = true
 include_trailing_comma = true
 line_length = 88
 known_first_party = superset
-known_third_party 
=alembic,apispec,backoff,bleach,cachelib,celery,click,colorama,cron_descriptor,croniter,cryptography,dateutil,deprecation,flask,flask_appbuilder,flask_babel,flask_caching,flask_compress,flask_jwt_extended,flask_login,flask_migrate,flask_sqlalchemy,flask_talisman,flask_testing,flask_wtf,freezegun,geohash,geopy,graphlib,holidays,humanize,isodate,jinja2,jwt,markdown,markupsafe,marshmallow,marshmallow_enum,msgpack,numpy,pandas,parameterized,parsedatetime,pgsanity,pkg_resour
 [...]
+known_third_party 
=alembic,apispec,ariadne,backoff,bleach,cachelib,celery,click,colorama,cron_descriptor,croniter,cryptography,dateutil,deprecation,flask,flask_appbuilder,flask_babel,flask_caching,flask_compress,flask_jwt_extended,flask_login,flask_migrate,flask_sqlalchemy,flask_talisman,flask_testing,flask_wtf,freezegun,geohash,geopy,graphlib,holidays,humanize,isodate,jinja2,jwt,markdown,markupsafe,marshmallow,marshmallow_enum,msgpack,numpy,pandas,parameterized,parsedatetime,pgsanity,pk
 [...]
 multi_line_output = 3
 order_by_type = false
 
diff --git a/superset/views/__init__.py b/superset/views/__init__.py
index c3a349c..68cd598 100644
--- a/superset/views/__init__.py
+++ b/superset/views/__init__.py
@@ -25,6 +25,7 @@ from . import (
     dashboard,
     datasource,
     dynamic_plugins,
+    graphql,
     health,
     redirects,
     schedules,
diff --git a/superset/views/graphql.py b/superset/views/graphql.py
new file mode 100644
index 0000000..a670776
--- /dev/null
+++ b/superset/views/graphql.py
@@ -0,0 +1,50 @@
+from ariadne import (
+    convert_kwargs_to_snake_case,
+    graphql_sync,
+    load_schema_from_path,
+    make_executable_schema,
+    ObjectType,
+    snake_case_fallback_resolvers,
+)
+from ariadne.constants import PLAYGROUND_HTML
+from flask import jsonify, request
+
+from superset import app, db
+from superset.models.core import Database
+from superset.typing import FlaskResponse
+
+
+@convert_kwargs_to_snake_case
+def resolve_database(obj, info, database_id):
+    database = db.session.query(Database).filter_by(id=database_id).one()
+    try:
+        payload = {
+            "success": True,
+            "database": database.data,
+        }
+    except Exception as error:
+        payload = {"success": False, "errors": [str(error)]}
+    return payload
+
+
+query = ObjectType("Query")
+
+query.set_field("database", resolve_database)
+
+type_defs = load_schema_from_path("superset/views/schema.graphql")
+schema = make_executable_schema(type_defs, query, 
snake_case_fallback_resolvers)
+
+
[email protected]("/graphql", methods=["GET"])
+def graphql_playground():
+    return PLAYGROUND_HTML, 200
+
+
[email protected]("/graphql", methods=["POST"])
+def graphql_server():
+    data = request.get_json()
+
+    success, result = graphql_sync(schema, data, context_value=request, 
debug=app.debug)
+
+    status_code = 200 if success else 400
+    return jsonify(result), status_code
diff --git a/superset/views/schema.graphql b/superset/views/schema.graphql
new file mode 100644
index 0000000..bedf0e4
--- /dev/null
+++ b/superset/views/schema.graphql
@@ -0,0 +1,22 @@
+schema {
+    query: Query
+}
+
+type Database {
+    id: ID!
+    name: String
+    backend: String
+    configuration_method: String
+
+}
+
+type DatabaseResult {
+    success: Boolean!
+    errors: [String]
+    database: Database
+}
+
+type Query {
+    databases: DatabaseResult!
+    database(databaseId: ID!): DatabaseResult!
+}

Reply via email to