diff --git a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers.py b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers.py index b56c32f65..d195bc210 100644 --- a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers.py +++ b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers.py @@ -35,7 +35,7 @@ from .handlers import ( GraphiqlHandler, - GraphqlHandler + GraphqlHandler, ) from .requests import ( RestRequest, @@ -129,15 +129,15 @@ def _mount_graphql(self, app: web.Application): """ def register_handlers(self, app: web.Application): - routes = [(r'/graphql', GraphqlHandler)] + routes = [(r"/graphql", GraphqlHandler)] # if self.dev: - routes += [(r'/graphiql', GraphiqlHandler)] + routes += [(r"/graphiql", GraphiqlHandler)] app.router.add_routes([web.view(route[0], route[1]) for route in routes]) async def initialize_jinja2(self, app: web.Application): - aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('./templates')) + aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader("./templates")) @staticmethod def get_callback( diff --git a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/__init__.py b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/__init__.py index 1e49dde44..6a3937e81 100644 --- a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/__init__.py +++ b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/__init__.py @@ -1,6 +1,6 @@ +from .graphiql import ( + GraphiqlHandler, +) from .graphql import ( GraphqlHandler, ) -from .graphiql import ( - GraphiqlHandler, -) diff --git a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/abc.py b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/abc.py index 628fbe4a7..8a402d652 100644 --- a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/abc.py +++ b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/abc.py @@ -1,14 +1,17 @@ import asyncio +import json import logging import urllib -import json + import aiohttp -from aiohttp import web -#from graphql.execution.executors.asyncio import AsyncioExecutor +from aiohttp import ( + web, +) +# from graphql.execution.executors.asyncio import AsyncioExecutor -class BaseHandler(web.View): +class BaseHandler(web.View): @property def fetch(self): return self.app.fetch @@ -21,9 +24,9 @@ async def options(self): return web.Response( status=204, headers={ - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH', - 'Access-Control-Allow-Headers': 'x-requested-with,access-control-allow-origin,authorization,content-type', + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH", + "Access-Control-Allow-Headers": "x-requested-with,access-control-allow-origin,authorization,content-type", }, ) @@ -39,17 +42,17 @@ async def handle_graqhql(self): status = 200 result = await self.execute_graphql() logging.debug( - 'GraphQL result data: %s errors: %s', + "GraphQL result data: %s errors: %s", result.data, result.errors, - #result.invalid, + # result.invalid, ) if result and result.errors: status = 500 - #ex = ExecutionError(errors=result.errors) - #logging.debug('GraphQL Error: %s', ex) + # ex = ExecutionError(errors=result.errors) + # logging.debug('GraphQL Error: %s', ex) errors = result.errors @@ -57,44 +60,44 @@ async def handle_graqhql(self): errors = [] return web.json_response( - {'data': result.data, 'errors': [str(err) for err in errors]}, + {"data": result.data, "errors": [str(err) for err in errors]}, status=status, - headers={'Access-Control-Allow-Origin': '*'}, + headers={"Access-Control-Allow-Origin": "*"}, ) async def execute_graphql(self): graphql_req = await self.graphql_request - logging.debug('graphql request: %s', graphql_req) - context_value = graphql_req.get('context', {}) - variables = graphql_req.get('variables', {}) + logging.debug("graphql request: %s", graphql_req) + context_value = graphql_req.get("context", {}) + variables = graphql_req.get("variables", {}) - context_value['application'] = self.app + context_value["application"] = self.app - #executor = AsyncioExecutor(loop=asyncio.get_event_loop()) + # executor = AsyncioExecutor(loop=asyncio.get_event_loop()) result = self.schema.execute( - graphql_req['query'], - #executor=executor, - #return_promise=True, + graphql_req["query"], + # executor=executor, + # return_promise=True, context_value=context_value, variable_values=variables, ) - ref = self.request.headers.get('referer') - url_path = '' + ref = self.request.headers.get("referer") + url_path = "" if ref: url = urllib.parse.urlparse(ref) url_path = url.path if result.errors: - if '/graphiql' not in url_path: + if "/graphiql" not in url_path: aiohttp.log.server_logger.error(f'Graphql query error: for query "{graphql_req}"') return result @property async def graphql_request(self): - if self.request.method == 'GET': - return json.loads(self.request.query['q']) + if self.request.method == "GET": + return json.loads(self.request.query["q"]) return await self.request.json() diff --git a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/graphiql.py b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/graphiql.py index 4e82cd7c3..3cf938e43 100644 --- a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/graphiql.py +++ b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/graphiql.py @@ -1,8 +1,11 @@ import aiohttp_jinja2 -from .abc import BaseHandler + +from .abc import ( + BaseHandler, +) class GraphiqlHandler(BaseHandler): - @aiohttp_jinja2.template('graphiql.html') + @aiohttp_jinja2.template("graphiql.html") async def get(self): - return {'base_url': f'/graphql'} + return {"base_url": f"/graphql"} diff --git a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/graphql.py b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/graphql.py index 9746e4f15..43fcc556a 100644 --- a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/graphql.py +++ b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/handlers/graphql.py @@ -1,11 +1,15 @@ import graphene -from .abc import GQLBaseHandler -from minos.plugins.graphql_aiohttp.schema import AllQuery +from minos.plugins.graphql_aiohttp.schema import ( + AllQuery, +) +from .abc import ( + GQLBaseHandler, +) -class GraphqlHandler(GQLBaseHandler): +class GraphqlHandler(GQLBaseHandler): @property def schema(self): return graphene.Schema(query=AllQuery) diff --git a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/schema.py b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/schema.py index 9d1ee5a0e..d823cdfe3 100644 --- a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/schema.py +++ b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/schema.py @@ -1,4 +1,6 @@ -from schemas.schema_hello import Query +from schemas.schema_hello import ( + Query, +) class AllQuery( @@ -7,4 +9,4 @@ class AllQuery( # Schema2, # Schema3 ): - '''AllQuery''' + """AllQuery""" diff --git a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/schemas/schema_hello.py b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/schemas/schema_hello.py index b29703d77..951c3210f 100644 --- a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/schemas/schema_hello.py +++ b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/schemas/schema_hello.py @@ -1,4 +1,8 @@ -from graphene import ObjectType, String, Schema +from graphene import ( + ObjectType, + Schema, + String, +) class Query(ObjectType): @@ -7,10 +11,10 @@ class Query(ObjectType): goodbye = String() def resolve_hello(root, info, name): - return f'Hello {name}!' + return f"Hello {name}!" def resolve_goodbye(root, info): - return 'See ya!' + return "See ya!" schema = Schema(query=Query) diff --git a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/services.py b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/services.py index ce26b9858..e8bca0f69 100644 --- a/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/services.py +++ b/packages/plugins/minos-graphql-aiohttp/minos/plugins/graphql_aiohttp/services.py @@ -6,9 +6,10 @@ from aiomisc.service.aiohttp import ( AIOHTTPService, ) + from .handlers import ( GraphiqlHandler, - GraphqlHandler + GraphqlHandler, ) @@ -24,21 +25,22 @@ def __init__(self, **kwargs): # self.handler = RestHandler.from_config(**kwargs) # super().__init__(**(kwargs | {"address": self.handler.host, "port": self.handler.port})) super().__init__(**(kwargs | {"address": "localhost", "port": 7030})) + """ def _mount_graphql(self, app: web.Application): GraphQLView.attach(app, schema=schema, graphiql=True) """ def register_handlers(self, app: web.Application): - routes = [(r'/graphql', GraphqlHandler)] + routes = [(r"/graphql", GraphqlHandler)] # if self.dev: - routes += [(r'/graphiql', GraphiqlHandler)] + routes += [(r"/graphiql", GraphiqlHandler)] app.router.add_routes([web.view(route[0], route[1]) for route in routes]) async def initialize_jinja2(self, app: web.Application): - aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('./templates')) + aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader("./templates")) async def create_application(self) -> web.Application: """Create the web application. @@ -49,6 +51,6 @@ async def create_application(self) -> web.Application: self.register_handlers(app) await self.initialize_jinja2(app) # self._mount_routes(app) - #self._mount_graphql(app) + # self._mount_graphql(app) return app # return self.handler.get_app() # pragma: no cover