-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
integrate ariadne, modularity and injection
- Loading branch information
Showing
6 changed files
with
117 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,46 @@ | ||
from quart import request, jsonify | ||
from graphql import GraphQLSchema | ||
from ariadne import ObjectType, make_executable_schema, graphql | ||
from ariadne.constants import PLAYGROUND_HTML | ||
from .schemas import init_graph as init_graph_schemas | ||
from .resources import init_graph as init_graph_resources | ||
|
||
from .repositories import PANTRY_STORES | ||
|
||
PANTRY = PANTRY_STORES['list'] | ||
|
||
type_defs = """ | ||
TYPE_DEFS = """ | ||
type Query { | ||
substances(nature: String!): [Substance] | ||
} | ||
type Substance { | ||
nature: String!, | ||
state: [String]! | ||
} | ||
""" | ||
|
||
query = ObjectType('Query') | ||
substance = ObjectType('Substance') | ||
|
||
@query.field('substances') | ||
def resolve_substances(obj, *_, nature='Unknown'): | ||
return PANTRY.find_substances_by_nature(nature) | ||
|
||
|
||
@substance.field('nature') | ||
def resolve_nature(obj, *_): | ||
return obj.nature | ||
|
||
@substance.field('state') | ||
def resolve_state(obj, *_): | ||
return obj.state | ||
|
||
schema = make_executable_schema(type_defs, query, substance) | ||
|
||
async def graphql_server(): | ||
data = await request.get_json() | ||
success, result = await graphql( | ||
schema, | ||
data, | ||
context_value=request | ||
) | ||
|
||
return jsonify(result), (200 if success else 400) | ||
class InjectorObjectType(ObjectType): | ||
def __init__(self, *args, app=None, **kwargs): | ||
super(InjectorObjectType, self).__init__(*args, **kwargs) | ||
self._app = app | ||
|
||
def get_injector(self): | ||
return self._app.extensions['injector'] | ||
|
||
def field(self, name: str): | ||
g = super(InjectorObjectType, self).field(name) | ||
def injected_resolver(f): | ||
def _inject_resolver(*args, **kwargs): | ||
self._app.logger.error(name) | ||
inj = self.get_injector() | ||
return inj.call_with_injection( | ||
f, args=args, kwargs=kwargs | ||
) | ||
return g(_inject_resolver) | ||
return injected_resolver | ||
|
||
def init_app(app): | ||
app.route('/graphql', methods=['GET'], endpoint='graphql_playground')( | ||
lambda: (PLAYGROUND_HTML, 200) | ||
) | ||
query = InjectorObjectType('Query', app=app) | ||
|
||
type_defs = [TYPE_DEFS] + init_graph_schemas(query) | ||
resolvers = [query] + init_graph_resources(query) | ||
|
||
app.route('/graphql', methods=['POST'], endpoint='graphql_server')( | ||
graphql_server | ||
) | ||
schema = make_executable_schema(type_defs, resolvers) | ||
|
||
return [] | ||
return [ | ||
lambda binder: binder.bind( | ||
GraphQLSchema, | ||
to=schema | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
from . import substance | ||
from . import alembic_instruction | ||
from . import graph | ||
|
||
def init_app(app): | ||
substance.init_app(app) | ||
alembic_instruction.init_app(app) | ||
graph.init_app(app) | ||
return [] | ||
|
||
def init_graph(graph): | ||
resolvers = [] | ||
resolvers += substance.init_graph(graph) | ||
return resolvers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from quart import request, jsonify | ||
from quart_openapi import Resource | ||
from injector import inject | ||
from typing import Dict | ||
from ariadne import graphql | ||
from ariadne.constants import PLAYGROUND_HTML | ||
from graphql.type import GraphQLSchema | ||
from ..injection import ViewInjectorMeta | ||
from ..repositories.pantry import Pantry | ||
from ..models import db | ||
|
||
class GraphResource(Resource): | ||
@inject | ||
def __init__(self, schema: GraphQLSchema): | ||
super(GraphResource, self).__init__() | ||
|
||
self._schema = schema | ||
|
||
async def get(self): | ||
return (PLAYGROUND_HTML, 200) | ||
|
||
async def post(self): | ||
data = await request.get_json() | ||
success, result = await graphql( | ||
self._schema, | ||
data, | ||
context_value=request | ||
) | ||
|
||
return jsonify(result), (200 if success else 400) | ||
|
||
|
||
def init_app(app): | ||
class AppGraphResource(GraphResource, metaclass=ViewInjectorMeta): | ||
get_app = lambda: app | ||
|
||
app.route('/graphql', endpoint='graphql')(AppGraphResource) | ||
|
||
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters