Fixed lint

This commit is contained in:
Syrus Akbary 2017-10-25 10:54:13 -07:00
parent de3947351b
commit 5051d3bb61

View File

@ -81,8 +81,10 @@ class GraphQLView(View):
self.graphiql = graphiql self.graphiql = graphiql
self.batch = batch self.batch = batch
assert isinstance(self.schema, GraphQLSchema), 'A Schema is required to be provided to GraphQLView.' assert isinstance(
assert not all((graphiql, batch)), 'Use either graphiql or batch processing' self.schema, GraphQLSchema), 'A Schema is required to be provided to GraphQLView.'
assert not all((graphiql, batch)
), 'Use either graphiql or batch processing'
# noinspection PyUnusedLocal # noinspection PyUnusedLocal
def get_root_value(self, request): def get_root_value(self, request):
@ -98,20 +100,27 @@ class GraphQLView(View):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
try: try:
if request.method.lower() not in ('get', 'post'): if request.method.lower() not in ('get', 'post'):
raise HttpError(HttpResponseNotAllowed(['GET', 'POST'], 'GraphQL only supports GET and POST requests.')) raise HttpError(HttpResponseNotAllowed(
['GET', 'POST'], 'GraphQL only supports GET and POST requests.'))
data = self.parse_body(request) data = self.parse_body(request)
show_graphiql = self.graphiql and self.can_display_graphiql(request, data) show_graphiql = self.graphiql and self.can_display_graphiql(
request, data)
if self.batch: if self.batch:
responses = [self.get_response(request, entry) for entry in data] responses = [self.get_response(
result = '[{}]'.format(','.join([response[0] for response in responses])) request, entry) for entry in data]
status_code = max(responses, key=lambda response: response[1])[1] result = '[{}]'.format(
','.join([response[0] for response in responses]))
status_code = max(
responses, key=lambda response: response[1])[1]
else: else:
result, status_code = self.get_response(request, data, show_graphiql) result, status_code = self.get_response(
request, data, show_graphiql)
if show_graphiql: if show_graphiql:
query, variables, operation_name, id = self.get_graphql_params(request, data) query, variables, operation_name, id = self.get_graphql_params(
request, data)
return self.render_graphiql( return self.render_graphiql(
request, request,
graphiql_version=self.graphiql_version, graphiql_version=self.graphiql_version,
@ -136,7 +145,8 @@ class GraphQLView(View):
return response return response
def get_response(self, request, data, show_graphiql=False): def get_response(self, request, data, show_graphiql=False):
query, variables, operation_name, id = self.get_graphql_params(request, data) query, variables, operation_name, id = self.get_graphql_params(
request, data)
execution_result = self.execute_graphql_request( execution_result = self.execute_graphql_request(
request, request,
@ -152,7 +162,8 @@ class GraphQLView(View):
response = {} response = {}
if execution_result.errors: if execution_result.errors:
response['errors'] = [self.format_error(e) for e in execution_result.errors] response['errors'] = [self.format_error(
e) for e in execution_result.errors]
if execution_result.invalid: if execution_result.invalid:
status_code = 400 status_code = 400
@ -209,7 +220,8 @@ class GraphQLView(View):
except AssertionError as e: except AssertionError as e:
raise HttpError(HttpResponseBadRequest(str(e))) raise HttpError(HttpResponseBadRequest(str(e)))
except (TypeError, ValueError): except (TypeError, ValueError):
raise HttpError(HttpResponseBadRequest('POST body sent invalid JSON.')) raise HttpError(HttpResponseBadRequest(
'POST body sent invalid JSON.'))
elif content_type in ['application/x-www-form-urlencoded', 'multipart/form-data']: elif content_type in ['application/x-www-form-urlencoded', 'multipart/form-data']:
return request.POST return request.POST
@ -223,7 +235,8 @@ class GraphQLView(View):
if not query: if not query:
if show_graphiql: if show_graphiql:
return None return None
raise HttpError(HttpResponseBadRequest('Must provide query string.')) raise HttpError(HttpResponseBadRequest(
'Must provide query string.'))
source = Source(query, name='GraphQL request') source = Source(query, name='GraphQL request')
@ -245,7 +258,8 @@ class GraphQLView(View):
return None return None
raise HttpError(HttpResponseNotAllowed( raise HttpError(HttpResponseNotAllowed(
['POST'], 'Can only perform a {} operation from a POST request.'.format(operation_ast.operation) ['POST'], 'Can only perform a {} operation from a POST request.'.format(
operation_ast.operation)
)) ))
try: try:
@ -283,10 +297,12 @@ class GraphQLView(View):
if variables and isinstance(variables, six.text_type): if variables and isinstance(variables, six.text_type):
try: try:
variables = json.loads(variables) variables = json.loads(variables)
except: except Exception:
raise HttpError(HttpResponseBadRequest('Variables are invalid JSON.')) raise HttpError(HttpResponseBadRequest(
'Variables are invalid JSON.'))
operation_name = request.GET.get('operationName') or data.get('operationName') operation_name = request.GET.get(
'operationName') or data.get('operationName')
if operation_name == "null": if operation_name == "null":
operation_name = None operation_name = None
@ -302,5 +318,6 @@ class GraphQLView(View):
@staticmethod @staticmethod
def get_content_type(request): def get_content_type(request):
meta = request.META meta = request.META
content_type = meta.get('CONTENT_TYPE', meta.get('HTTP_CONTENT_TYPE', '')) content_type = meta.get(
'CONTENT_TYPE', meta.get('HTTP_CONTENT_TYPE', ''))
return content_type.split(';', 1)[0].lower() return content_type.split(';', 1)[0].lower()