From 8dbcd9abdf1aa020b3675f56a66d600be119a148 Mon Sep 17 00:00:00 2001 From: Bilal Date: Tue, 9 May 2023 11:56:38 +0100 Subject: [PATCH 1/3] upgrade Ariadne (Moving away from GraphQL Playground to GraphiQL) --- graphql_service/server.py | 5 ++++- requirements.txt | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/graphql_service/server.py b/graphql_service/server.py index 77c3a89..6a7ddf4 100644 --- a/graphql_service/server.py +++ b/graphql_service/server.py @@ -16,6 +16,7 @@ from typing import Optional from ariadne.asgi import GraphQL +from ariadne.asgi.handlers import GraphQLHTTPHandler from ariadne.contrib.tracing.apollotracing import ApolloTracingExtension from ariadne.types import ExtensionList from pymongo import monitoring @@ -78,6 +79,8 @@ EXECUTABLE_SCHEMA, debug=DEBUG_MODE, context_value=CONTEXT_PROVIDER, - extensions=EXTENSIONS, + http_handler=GraphQLHTTPHandler( + extensions=EXTENSIONS, + ), ), ) diff --git a/requirements.txt b/requirements.txt index 3aee78f..f0a4b57 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,6 @@ mongomock==4.0.0 pymongo==4.1.1 requests==2.28.0 aiodataloader==0.2.1 -ariadne==0.15.1 +ariadne==0.19.1 python-dotenv==0.20.0 uvicorn==0.18.1 From 20e3f15f0b00ecad9d1131d01c3b133460dda3c9 Mon Sep 17 00:00:00 2001 From: Bilal Date: Thu, 11 May 2023 17:01:54 +0100 Subject: [PATCH 2/3] customize graphiql2 ui and activate plugins (this may need some clean up) --- graphql_service/server.py | 84 ++++++++++ .../templates/custom_graphiql.html | 154 ++++++++++++++++++ 2 files changed, 238 insertions(+) create mode 100644 graphql_service/templates/custom_graphiql.html diff --git a/graphql_service/server.py b/graphql_service/server.py index 6a7ddf4..0343d49 100644 --- a/graphql_service/server.py +++ b/graphql_service/server.py @@ -18,6 +18,8 @@ from ariadne.asgi import GraphQL from ariadne.asgi.handlers import GraphQLHTTPHandler from ariadne.contrib.tracing.apollotracing import ApolloTracingExtension +from ariadne.explorer import ExplorerGraphiQL, render_template, escape_default_query +from ariadne.explorer.template import read_template from ariadne.types import ExtensionList from pymongo import monitoring from starlette.applications import Starlette @@ -72,6 +74,87 @@ Middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["GET", "POST"]) ] +# The original HTML file can be found under +# [venv]/ariadne/explorer/templates/graphiql.html +CUSTOM_GRAPHIQL_HTML = read_template( + os.path.dirname(os.path.realpath(__file__)) + "/templates/custom_graphiql.html" +) + +DEFAULT_QUERY = """ +# +# GraphiQL is an in -browser tool for writing, validating, and +# testing GraphQL queries. +# +# Type queries into this side of the screen, and you will see intelligent +# typeaheads aware of the current GraphQL type schema and live syntax and +# validation errors highlighted within the text. +# +# GraphQL queries typically start with a "{" character. Lines that start +# with a # are ignored. +# +# An example GraphQL query might look like: +# +# { +# field(arg: "value") { +# subField +# +# } +# +# } +# +# Keyboard shortcuts: +# +# Prettify query: Shift - Ctrl - P(or press the prettify button) +# +# Merge fragments: Shift - Ctrl - M(or press the merge button) +# +# Run Query: Ctrl - Enter(or press the play button) +# +# Auto Complete: Ctrl - Space(or just start typing) +# +query ENSG00000139618 { + gene( + by_id: {genome_id: "a7335667-93e7-11ec-a39d-005056b38ce3", stable_id: "ENSG00000139618"} + ) { + alternative_symbols + name + so_term + stable_id + transcripts { + stable_id + symbol + } + } +} +""" + + +class CustomExplorerGraphiQL(ExplorerGraphiQL): + """ + We can customize the GraphiQL interface in Ariadne by overriding the ExplorerGraphiQL class + which is responsible for rendering the default GraphiQL UI + """ + + def __init__( + self, + title: str = "Ensembl Core API", + explorer_plugin: bool = True, + default_query: str = DEFAULT_QUERY, + ): + super(CustomExplorerGraphiQL, self).__init__() + self.parsed_html = render_template( + CUSTOM_GRAPHIQL_HTML, + { + "title": title, + "enable_explorer_plugin": explorer_plugin, + "default_query": escape_default_query(default_query), + }, + ) + + def html(self, _): + return self.parsed_html + + APP = Starlette(debug=DEBUG_MODE, middleware=starlette_middleware) APP.mount( "/", @@ -82,5 +165,6 @@ http_handler=GraphQLHTTPHandler( extensions=EXTENSIONS, ), + explorer=CustomExplorerGraphiQL(), ), ) diff --git a/graphql_service/templates/custom_graphiql.html b/graphql_service/templates/custom_graphiql.html new file mode 100644 index 0000000..3df3934 --- /dev/null +++ b/graphql_service/templates/custom_graphiql.html @@ -0,0 +1,154 @@ + + + + + + + {{ title }} + + + {% if enable_explorer_plugin %} + + {% endif %} + + + + +
+ + + + Core API (Beta) + + © EMBL-EBI + + + + + + + + +
+ +
+
Loading {{ title }}...
+
+ + + + + + + {% if enable_explorer_plugin %} + + {% endif %} + + + From 292c6525b02542fe6c5c9248935249abaf3eb0e0 Mon Sep 17 00:00:00 2001 From: Bilal Date: Thu, 11 May 2023 20:01:41 +0100 Subject: [PATCH 3/3] switch to develop in CI/CD --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d1ab35c..e1d38f7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -59,7 +59,7 @@ test: build: extends: .build only: - - main + - develop # deploy application to k8s cluster for k8s-deploy branch deploy: @@ -67,4 +67,4 @@ deploy: environment: name: thoas-graphql only: - - main + - develop