Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

from .handlers import (
GraphiqlHandler,
GraphqlHandler
GraphqlHandler,
)
from .requests import (
RestRequest,
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .graphiql import (
GraphiqlHandler,
)
from .graphql import (
GraphqlHandler,
)
from .graphiql import (
GraphiqlHandler,
)
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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",
},
)

Expand All @@ -39,62 +42,62 @@ 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

if errors is None:
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()
Original file line number Diff line number Diff line change
@@ -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"}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from schemas.schema_hello import Query
from schemas.schema_hello import (
Query,
)


class AllQuery(
Expand All @@ -7,4 +9,4 @@ class AllQuery(
# Schema2,
# Schema3
):
'''AllQuery'''
"""AllQuery"""
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from graphene import ObjectType, String, Schema
from graphene import (
ObjectType,
Schema,
String,
)


class Query(ObjectType):
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
from aiomisc.service.aiohttp import (
AIOHTTPService,
)

from .handlers import (
GraphiqlHandler,
GraphqlHandler
GraphqlHandler,
)


Expand All @@ -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.
Expand All @@ -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