Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/graphql server #2

Merged
merged 4 commits into from
Mar 23, 2017
Merged
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
263 changes: 70 additions & 193 deletions sanic_graphql/graphqlview.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import json
import six
from functools import partial
from cgi import parse_header


from promise import Promise
from sanic.response import HTTPResponse
from sanic.views import HTTPMethodView
from sanic.exceptions import SanicException

from promise import Promise
from graphql import Source, execute, parse, validate
from graphql.error import format_error as format_graphql_error
from graphql.error import GraphQLError
from graphql.execution import ExecutionResult
from graphql.type.schema import GraphQLSchema
from graphql.utils.get_operation_ast import get_operation_ast
from graphql.execution.executors.asyncio import AsyncioExecutor
from graphql_server import run_http_query, HttpQueryError, default_format_error, load_json_body, encode_execution_results, json_encode

from .render_graphiql import render_graphiql


class HttpError(Exception):
def __init__(self, response, message=None, *args, **kwargs):
self.response = response
self.message = message = message or response.args[0]
super(HttpError, self).__init__(message, *args, **kwargs)


class GraphQLView(HTTPMethodView):
schema = None
executor = None
Expand All @@ -44,14 +32,11 @@ class GraphQLView(HTTPMethodView):

def __init__(self, **kwargs):
super(GraphQLView, self).__init__()

for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)

self._enable_async = self._enable_async and isinstance(kwargs.get('executor'), AsyncioExecutor)

assert not all((self.graphiql, self.batch)), 'Use either graphiql or batch processing'
assert isinstance(self.schema, GraphQLSchema), 'A Schema is required to be provided to GraphQLView.'

# noinspection PyUnusedLocal
Expand All @@ -70,211 +55,103 @@ def get_middleware(self, request):
def get_executor(self, request):
return self.executor

def render_graphiql(self, params, result):
return render_graphiql(
jinja_env=self.jinja_env,
params=params,
result=result,
graphiql_version=self.graphiql_version,
graphiql_template=self.graphiql_template,
)

format_error = staticmethod(default_format_error)
encode = staticmethod(json_encode)

async def dispatch_request(self, request, *args, **kwargs):
try:
if request.method.lower() not in ('get', 'post'):
raise HttpError(SanicException('GraphQL only supports GET and POST requests.', status_code=405))

request_method = request.method.lower()
data = self.parse_body(request)
show_graphiql = self.graphiql and self.can_display_graphiql(request, data)

if self.batch:
responses = []
for entry in data:
responses.append(await self.get_response(request, entry))
show_graphiql = request_method == 'get' and self.should_display_graphiql(request)
catch = HttpQueryError if show_graphiql else None

pretty = self.pretty or show_graphiql or request.args.get('pretty')

result = '[{}]'.format(','.join([response[0] for response in responses]))
status_code = max(responses, key=lambda response: response[1])[1]
else:
result, status_code = await self.get_response(request, data, show_graphiql)
execution_results, all_params = run_http_query(
self.schema,
request_method,
data,
query_data=request.args,
batch_enabled=self.batch,
catch=catch,

# Execute options
return_promise=self._enable_async,
root_value=self.get_root_value(request),
context_value=self.get_context(request),
middleware=self.get_middleware(request),
executor=self.get_executor(request),
)
awaited_execution_results = await Promise.all(execution_results)
result, status_code = encode_execution_results(
awaited_execution_results,
is_batch=isinstance(data, list),
format_error=self.format_error,
encode=partial(self.encode, pretty=pretty)
)

if show_graphiql:
query, variables, operation_name, id = self.get_graphql_params(request, data)
return await render_graphiql(
jinja_env=self.jinja_env,
graphiql_version=self.graphiql_version,
graphiql_template=self.graphiql_template,
query=query,
variables=variables,
operation_name=operation_name,
return await self.render_graphiql(
params=all_params[0],
result=result
)

return HTTPResponse(
status=status_code,
body=result,
content_type='application/json'
result,
status=status_code,
content_type='application/json'
)

except HttpError as e:
except HttpQueryError as e:
return HTTPResponse(
self.json_encode(request, {
'errors': [self.format_error(e)]
self.encode({
'errors': [default_format_error(e)]
}),
status=e.response.status_code,
headers={'Allow': 'GET, POST'},
status=e.status_code,
headers=e.headers,
content_type='application/json'
)

async def get_response(self, request, data, show_graphiql=False):
query, variables, operation_name, id = self.get_graphql_params(request, data)

execution_result = await self.execute_graphql_request(
request,
data,
query,
variables,
operation_name,
show_graphiql
)

status_code = 200
if execution_result:
response = {}

if execution_result.errors:
response['errors'] = [self.format_error(e) for e in execution_result.errors]

if execution_result.invalid:
status_code = 400
else:
status_code = 200
response['data'] = execution_result.data

if self.batch:
response = {
'id': id,
'payload': response,
'status': status_code,
}

result = self.json_encode(request, response, show_graphiql)
else:
result = None

return result, status_code

def json_encode(self, request, d, show_graphiql=False):
pretty = self.pretty or show_graphiql or request.args.get('pretty')
if not pretty:
return json.dumps(d, separators=(',', ':'))

return json.dumps(d, sort_keys=True,
indent=2, separators=(',', ': '))

# noinspection PyBroadException
def parse_body(self, request):
content_type = self.get_content_type(request)
content_type = self.get_mime_type(request)
if content_type == 'application/graphql':
return {'query': request.body.decode()}
return {'query': request.body.decode('utf8')}

elif content_type == 'application/json':
try:
request_json = json.loads(request.body.decode('utf-8'))
if (self.batch and not isinstance(request_json, list)) or (
not self.batch and not isinstance(request_json, dict)):
raise Exception()
except:
raise HttpError(SanicException('POST body sent invalid JSON.', status_code=400))
return request_json

elif content_type == 'application/x-www-form-urlencoded':
return request.form
return load_json_body(request.body.decode('utf8'))

elif content_type == 'multipart/form-data':
elif content_type in ('application/x-www-form-urlencoded', 'multipart/form-data'):
return request.form

return {}

async def execute(self, *args, **kwargs):
result = execute(self.schema, return_promise=self._enable_async, *args, **kwargs)
if isinstance(result, Promise):
return await result
else:
return result

async def execute_graphql_request(self, request, data, query, variables, operation_name, show_graphiql=False):
if not query:
if show_graphiql:
return None
raise HttpError(SanicException('Must provide query string.', status_code=400))

try:
source = Source(query, name='GraphQL request')
ast = parse(source)
validation_errors = validate(self.schema, ast)
if validation_errors:
return ExecutionResult(
errors=validation_errors,
invalid=True,
)
except Exception as e:
return ExecutionResult(errors=[e], invalid=True)

if request.method.lower() == 'get':
operation_ast = get_operation_ast(ast, operation_name)
if operation_ast and operation_ast.operation != 'query':
if show_graphiql:
return None
raise HttpError(SanicException(
'Can only perform a {} operation from a POST request.'.format(operation_ast.operation),
status_code=405,
))

try:
return await self.execute(
ast,
root_value=self.get_root_value(request),
variable_values=variables or {},
operation_name=operation_name,
context_value=self.get_context(request),
middleware=self.get_middleware(request),
executor=self.get_executor(request)
)
except Exception as e:
return ExecutionResult(errors=[e], invalid=True)

@classmethod
def can_display_graphiql(cls, request, data):
raw = 'raw' in request.args or 'raw' in data
return not raw and cls.request_wants_html(request)

@classmethod
def request_wants_html(cls, request):
# Ugly hack
accept = request.headers.get('accept', {})
return 'text/html' in accept or '*/*' in accept

@staticmethod
def get_graphql_params(request, data):
query = request.args.get('query') or data.get('query')
variables = request.args.get('variables') or data.get('variables')
id = request.args.get('id') or data.get('id')

if variables and isinstance(variables, six.text_type):
try:
variables = json.loads(variables)
except:
raise HttpError(SanicException('Variables are invalid JSON.', status_code=400))

operation_name = request.args.get('operationName') or data.get('operationName')

return query, variables, operation_name, id

@staticmethod
def format_error(error):
if isinstance(error, GraphQLError):
return format_graphql_error(error)

return {'message': six.text_type(error)}

@staticmethod
def get_content_type(request):
def get_mime_type(request):
# We use mimetype here since we don't need the other
# information provided by content_type
if 'content-type' not in request.headers:
mimetype = 'text/plain'
else:
mimetype, params = parse_header(request.headers['content-type'])
return None

mimetype, _ = parse_header(request.headers['content-type'])
return mimetype

def should_display_graphiql(self, request):
if not self.graphiql or 'raw' in request.args:
return False

return self.request_wants_html(request)

def request_wants_html(self, request):
accept = request.headers.get('accept', {})
return 'text/html' in accept or '*/*' in accept
16 changes: 11 additions & 5 deletions sanic_graphql/render_graphiql.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,24 @@ def simple_renderer(template, **values):
return template


async def render_graphiql(*, jinja_env=None, graphiql_version=None, graphiql_template=None, **kwargs):
async def render_graphiql(jinja_env=None, graphiql_version=None, graphiql_template=None, params=None, result=None):
graphiql_version = graphiql_version or GRAPHIQL_VERSION
template = graphiql_template or TEMPLATE
kwargs['graphiql_version'] = graphiql_version
template_vars = {
'graphiql_version': graphiql_version,
'query': params and params.query,
'variables': params and params.variables,
'operation_name': params and params.operation_name,
'result': result,
}

if jinja_env:
template = jinja_env.from_string(template)
if jinja_env.is_async:
source = await template.render_async(**kwargs)
source = await template.render_async(**template_vars)
else:
source = template.render(**kwargs)
source = template.render(**template_vars)
else:
source = simple_renderer(template, **kwargs)
source = simple_renderer(template, **template_vars)

return html(source)
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from setuptools import setup, find_packages

required_packages = ['graphql-core>=1.0', 'sanic>=0.4.0', 'pytest-runner']
required_packages = [
'graphql-core>=1.0',
'graphql-server-core>=1.0.dev',
'sanic>=0.4.0',
'pytest-runner'
]

setup(
name='Sanic-GraphQL',
Expand Down
Loading