Skip to content

update code configurations for consistency with graphql-python ecosystem #24

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

Closed
wants to merge 5 commits into from
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
2 changes: 2 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
known_third_party = graphql, setuptools
28 changes: 28 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
repos:
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v2.1.0
hooks:
- id: check-merge-conflict
- id: check-json
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
exclude: ^docs/.*$
- id: pretty-format-json
args:
- --autofix
- id: trailing-whitespace
exclude: README.md
- repo: https://github.com/asottile/pyupgrade
rev: v1.12.0
hooks:
- id: pyupgrade
- repo: https://github.com/ambv/black
rev: 19.3b0
hooks:
- id: black
language_version: python3
- repo: https://github.com/PyCQA/flake8
rev: 3.7.7
hooks:
- id: flake8
14 changes: 2 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,19 @@ language: python
matrix:
include:
- python: '3.7'
env: TOX_ENV=black,flake8,mypy,py37
env: TOX_ENV=flake8,mypy,py37,pre-commit
dist: xenial
sudo: true # required workaround for https://github.com/travis-ci/travis-ci/issues/9815
- python: '3.6'
env: TOX_ENV=py36
- python: '3.5'
env: TOX_ENV=py35
- python: '3.4'
env: TOX_ENV=py34
- python: '2.7'
env: TOX_ENV=py27
- python: 'pypy3.5'
env: TOX_ENV=pypy3
- python: 'pypy'
env: TOX_ENV=pypy
cache:
directories:
- $HOME/.cache/pip
- $TRAVIS_BUILD_DIR/.tox
install:
- pip install tox codecov
script:
- tox -e $TOX_ENV -- --cov-report term-missing --cov=graphql_server
- tox -e $TOX_ENV -- --cov-report term-missing
after_success:
- codecov
deploy:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ coverage:
status:
project:
default:
target: auto
target: auto
66 changes: 27 additions & 39 deletions graphql_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,16 @@

import json
from collections import namedtuple

import six
from graphql import get_default_backend
#from graphql import get_default_backend
from graphql.error import format_error as default_format_error
from graphql.execution import ExecutionResult
from graphql.type import GraphQLSchema
from graphql import parse, graphql as graphql_, get_operation_root_type, DocumentNode, OperationDefinitionNode, GraphQLSchema

from .error import HttpQueryError

try: # pragma: no cover (Python >= 3.3)
from collections.abc import MutableMapping
except ImportError: # pragma: no cover (Python < 3.3)
# noinspection PyUnresolvedReferences,PyProtectedMember
from collections import MutableMapping
from collections import MutableMapping

# Necessary for static type checking
# noinspection PyUnreachableCode
if False: # pragma: no cover
# flake8: noqa
from typing import Any, Callable, Dict, List, Optional, Type, Union
from graphql import GraphQLBackend
from typing import Any, Callable, Dict, List, Optional, Type, Union


__all__ = [
Expand All @@ -57,15 +46,19 @@

# The public helper functions

def get_operation_node(doc: DocumentNode) -> OperationDefinitionNode:
operation_node = doc.definitions[0]
assert isinstance(operation_node, OperationDefinitionNode)
return operation_node

def run_http_query(
schema, # type: GraphQLSchema
request_method, # type: str
data, # type: Union[Dict, List[Dict]]
query_data=None, # type: Optional[Dict]
batch_enabled=False, # type: bool
catch=False, # type: bool
**execute_options # type: Any
schema: GraphQLSchema,
request_method: str,
data: Union[Dict, List[Dict]],
query_data: Optional[Dict] = None,
batch_enabled: bool = False,
catch: bool = False,
**execute_options: Any
):
"""Execute GraphQL coming from an HTTP query against a given schema.

Expand All @@ -83,7 +76,7 @@ def run_http_query(
and the list of parameters that have been used for execution as second item.
"""
if not isinstance(schema, GraphQLSchema):
raise TypeError("Expected a GraphQL schema, but received {!r}.".format(schema))
raise TypeError(f"Expected a GraphQL schema, but received {repr(schema)}.")
if request_method not in ("get", "post"):
raise HttpQueryError(
405,
Expand All @@ -104,7 +97,7 @@ def run_http_query(
if not is_batch:
if not isinstance(data, (dict, MutableMapping)):
raise HttpQueryError(
400, "GraphQL params should be a dict. Received {!r}.".format(data)
400, f"GraphQL params should be a dict. Received {repr(data)}."
)
data = [data]
elif not batch_enabled:
Expand Down Expand Up @@ -223,7 +216,7 @@ def load_json_variables(variables):
deserialized from JSON to a dictionary first if necessary. In case of
invalid JSON input, an HttpQueryError will be raised.
"""
if variables and isinstance(variables, six.string_types):
if variables and isinstance(variables, str):
try:
return json.loads(variables)
except Exception:
Expand All @@ -235,7 +228,7 @@ def execute_graphql_request(
schema, # type: GraphQLSchema
params, # type: RequestParams
allow_only_query=False, # type: bool
backend=None, # type: GraphQLBackend
#backend=None, # type: GraphQLBackend
**kwargs # type: Any
):
"""Execute a GraphQL request and return an ExecutionResult.
Expand All @@ -250,26 +243,21 @@ def execute_graphql_request(
if not params.query:
raise HttpQueryError(400, "Must provide query string.")

try:
if not backend:
backend = get_default_backend()
document = backend.document_from_string(schema, params.query)
except Exception as e:
return ExecutionResult(errors=[e], invalid=True)

if allow_only_query:
operation_type = document.get_operation_type(params.operation_name)
if operation_type and operation_type != "query":
document = parse(params.query)

op_node = get_operation_node(document)
query_type = schema.query_type
operation_type = get_operation_root_type(schema, op_node)
if operation_type and operation_type != query_type:
raise HttpQueryError(
405,
"Can only perform a {} operation from a POST request.".format(
operation_type
),
f"Can only perform a {operation_type} operation from a POST request.",
headers={"Allow": "POST"},
)

try:
return document.execute(
return graphql_(
operation_name=params.operation_name, variables=params.variables, **kwargs
)
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[flake8]
exclude = tests,scripts,setup.py,docs
max-line-length = 160
max-line-length = 120

[isort]
known_first_party=graphql_server
Expand Down
8 changes: 1 addition & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

required_packages = ["graphql-core>=2.1,<3", "promise"]
required_packages = ["graphql-core-next~=1.0.5", "promise"]

setup(
name="graphql-server-core",
Expand All @@ -16,12 +16,6 @@
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Topic :: Software Development :: Libraries",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: Implementation :: PyPy",
Expand Down
25 changes: 10 additions & 15 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
[tox]
envlist = black,flake8,mypy,py37,py36,py35,py34,py33,py27,pypy3,pypy
envlist = flake8,mypy,py37,py36,pre-commit
skipsdist = true

[testenv]
setenv =
PYTHONPATH = {toxinidir}
deps =
pytest>=3.0,<4
graphql-core>=2.1,<3
graphql-core-next~=1.0.5
pytest-cov>=2.7
commands =
py{py,27,33,34,35,36,37}: py.test tests {posargs}
py{36,37}: py.test tests {posargs}

[testenv:black]
[testenv:pre-commit]
basepython=python3.7
deps = black
deps =
pre-commit>0.12.0
setenv =
LC_CTYPE=en_US.UTF-8
commands =
black --check graphql_server tests
pre-commit run --all-files


[testenv:flake8]
basepython=python3.7
deps = flake8
commands =
flake8 graphql_server tests

[testenv:isort]
basepython=python3.7
deps =
isort
graphql-core>=2.1
commands =
isort -rc graphql_server/ tests/

[testenv:mypy]
basepython=python3.7
deps = mypy
commands =
mypy graphql_server tests --ignore-missing-imports