Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d22e9d
commit 0f00e67
Showing
12 changed files
with
112 additions
and
80 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.core.handlers.wsgi import WSGIRequest | ||
|
||
from slothql import utils | ||
from slothql.operation import Operation | ||
|
||
|
||
def get_query_from_request(request: WSGIRequest) -> str: | ||
def get_operation_from_request(request: WSGIRequest) -> Operation: | ||
if request.method == 'GET': | ||
return request.GET.get('query', '') | ||
return Operation.from_dict(request.GET) | ||
|
||
if not request.content_type: | ||
raise ValidationError('content-type not specified') | ||
|
||
if request.content_type == 'application/graphql': | ||
return request.body.decode() | ||
elif request.content_type == 'application/json': | ||
if request.content_type in ('application/graphql', 'application/json', 'multipart/form-data'): | ||
try: | ||
return utils.query_from_raw_json(request.body.decode('utf-8')) | ||
return Operation.from_string(request.body.decode()) | ||
except ValueError as e: | ||
raise ValidationError(str(e)) | ||
elif request.content_type in ('application/x-www-form-urlencoded', 'multipart/form-data'): | ||
return request.POST.get('query', '') | ||
elif request.content_type in ('application/x-www-form-urlencoded',): | ||
return Operation.from_dict(request.POST) | ||
raise ValidationError(f'Unsupported content-type {request.content_type}') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import json | ||
|
||
from typing import NamedTuple | ||
|
||
|
||
def from_raw_json(data: str) -> dict: | ||
try: | ||
json_query = json.loads(data) | ||
except json.JSONDecodeError as e: | ||
raise ValueError(str(e)) | ||
|
||
if not isinstance(json_query, dict): | ||
raise ValueError('GraphQL queries must a dictionary') | ||
|
||
if 'query' not in json_query: | ||
raise ValueError('"query" not found in json object') | ||
return json_query | ||
|
||
|
||
class Operation(NamedTuple): | ||
query: str | ||
variables: dict | ||
operation_name: str | ||
|
||
@classmethod | ||
def from_string(cls, string: str) -> 'class': | ||
return cls.from_dict(from_raw_json(string)) | ||
|
||
@classmethod | ||
def from_dict(cls, data: dict): | ||
return cls( | ||
query=data.get('query'), | ||
variables=data.get('variables'), | ||
operation_name=data.get('operationName') or data.get('operation_name'), | ||
) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
from .attr import get_attr_fields, is_magic_name, get_attrs | ||
from .case import snake_to_camelcase | ||
from .query import query_from_raw_json | ||
from .laziness import LazyInitMixin | ||
|
||
__all__ = ( | ||
'get_attr_fields', 'is_magic_name', 'get_attrs', | ||
'snake_to_camelcase', | ||
'query_from_raw_json', | ||
'LazyInitMixin', | ||
) |
This file was deleted.
Oops, something went wrong.