Skip to content

add support for validation rules #83

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

Merged
merged 5 commits into from
Aug 10, 2021
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ target/
# pyenv
.python-version

# Pycharm venv
venv/

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
Expand Down
1 change: 1 addition & 0 deletions docs/aiohttp.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ gql_view(request) # <-- the instance is callable and expects a `aiohttp.web.Req
`Template.render_async` instead of `Template.render`. If environment is not set, fallbacks to simple regex-based renderer.
* `batch`: Set the GraphQL view as batch (for using in [Apollo-Client](http://dev.apollodata.com/core/network.html#query-batching) or [ReactRelayNetworkLayer](https://github.com/nodkz/react-relay-network-layer))
* `middleware`: A list of graphql [middlewares](http://docs.graphene-python.org/en/latest/execution/middleware/).
* `validation_rules`: A list of graphql validation rules.
* `max_age`: Sets the response header Access-Control-Max-Age for preflight requests.
* `encode`: the encoder to use for responses (sensibly defaults to `graphql_server.json_encode`).
* `format_error`: the error formatter to use for responses (sensibly defaults to `graphql_server.default_format_error`.
Expand Down
1 change: 1 addition & 0 deletions docs/flask.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ More info at [Graphene v3 release notes](https://github.com/graphql-python/graph
* `graphiql_html_title`: The graphiql title to display. Defaults to **"GraphiQL"**.
* `batch`: Set the GraphQL view as batch (for using in [Apollo-Client](http://dev.apollodata.com/core/network.html#query-batching) or [ReactRelayNetworkLayer](https://github.com/nodkz/react-relay-network-layer))
* `middleware`: A list of graphql [middlewares](http://docs.graphene-python.org/en/latest/execution/middleware/).
* `validation_rules`: A list of graphql validation rules.
* `encode`: the encoder to use for responses (sensibly defaults to `graphql_server.json_encode`).
* `format_error`: the error formatter to use for responses (sensibly defaults to `graphql_server.default_format_error`.
* `subscriptions`: The GraphiQL socket endpoint for using subscriptions in graphql-ws.
Expand Down
1 change: 1 addition & 0 deletions docs/sanic.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ This will add `/graphql` endpoint to your app and enable the GraphiQL IDE.
`Template.render_async` instead of `Template.render`. If environment is not set, fallbacks to simple regex-based renderer.
* `batch`: Set the GraphQL view as batch (for using in [Apollo-Client](http://dev.apollodata.com/core/network.html#query-batching) or [ReactRelayNetworkLayer](https://github.com/nodkz/react-relay-network-layer))
* `middleware`: A list of graphql [middlewares](http://docs.graphene-python.org/en/latest/execution/middleware/).
* `validation_rules`: A list of graphql validation rules.
* `max_age`: Sets the response header Access-Control-Max-Age for preflight requests.
* `encode`: the encoder to use for responses (sensibly defaults to `graphql_server.json_encode`).
* `format_error`: the error formatter to use for responses (sensibly defaults to `graphql_server.default_format_error`.
Expand Down
1 change: 1 addition & 0 deletions docs/webob.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ This will add `/graphql` endpoint to your app and enable the GraphiQL IDE.
* `graphiql_html_title`: The graphiql title to display. Defaults to **"GraphiQL"**.
* `batch`: Set the GraphQL view as batch (for using in [Apollo-Client](http://dev.apollodata.com/core/network.html#query-batching) or [ReactRelayNetworkLayer](https://github.com/nodkz/react-relay-network-layer))
* `middleware`: A list of graphql [middlewares](http://docs.graphene-python.org/en/latest/execution/middleware/).
* `validation_rules`: A list of graphql validation rules.
* `encode`: the encoder to use for responses (sensibly defaults to `graphql_server.json_encode`).
* `format_error`: the error formatter to use for responses (sensibly defaults to `graphql_server.default_format_error`.
* `enable_async`: whether `async` mode will be enabled.
Expand Down
9 changes: 8 additions & 1 deletion graphql_server/aiohttp/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List

from aiohttp import web
from graphql import ExecutionResult, GraphQLError
from graphql import ExecutionResult, GraphQLError, specified_rules
from graphql.type.schema import GraphQLSchema

from graphql_server import (
Expand Down Expand Up @@ -34,6 +34,7 @@ class GraphQLView:
graphiql_template = None
graphiql_html_title = None
middleware = None
validation_rules = None
batch = False
jinja_env = None
max_age = 86400
Expand Down Expand Up @@ -75,6 +76,11 @@ def get_context(self, request):
def get_middleware(self):
return self.middleware

def get_validation_rules(self):
if self.validation_rules is None:
return specified_rules
return self.validation_rules
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return the default validation rules if validation_rules is None:

from graphql import specified_rules


@staticmethod
async def parse_body(request):
content_type = request.content_type
Expand Down Expand Up @@ -149,6 +155,7 @@ async def __call__(self, request):
root_value=self.get_root_value(),
context_value=self.get_context(request),
middleware=self.get_middleware(),
validation_rules=self.get_validation_rules(),
)

exec_res = (
Expand Down
8 changes: 8 additions & 0 deletions graphql_server/flask/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from flask.views import View
from graphql.error import GraphQLError
from graphql.type.schema import GraphQLSchema
from graphql import specified_rules

from graphql_server import (
GraphQLParams,
Expand Down Expand Up @@ -35,6 +36,7 @@ class GraphQLView(View):
graphiql_template = None
graphiql_html_title = None
middleware = None
validation_rules = None
batch = False
subscriptions = None
headers = None
Expand Down Expand Up @@ -73,6 +75,11 @@ def get_context(self):
def get_middleware(self):
return self.middleware

def get_validation_rules(self):
if self.validation_rules is None:
return specified_rules
return self.validation_rules

def dispatch_request(self):
try:
request_method = request.method.lower()
Expand All @@ -95,6 +102,7 @@ def dispatch_request(self):
root_value=self.get_root_value(),
context_value=self.get_context(),
middleware=self.get_middleware(),
validation_rules=self.get_validation_rules(),
)
result, status_code = encode_execution_results(
execution_results,
Expand Down
9 changes: 8 additions & 1 deletion graphql_server/quart/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import partial
from typing import List

from graphql import ExecutionResult
from graphql import ExecutionResult, specified_rules
from graphql.error import GraphQLError
from graphql.type.schema import GraphQLSchema
from quart import Response, render_template_string, request
Expand Down Expand Up @@ -37,6 +37,7 @@ class GraphQLView(View):
graphiql_template = None
graphiql_html_title = None
middleware = None
validation_rules = None
batch = False
enable_async = False
subscriptions = None
Expand Down Expand Up @@ -76,6 +77,11 @@ def get_context(self):
def get_middleware(self):
return self.middleware

def get_validation_rules(self):
if self.validation_rules is None:
return specified_rules
return self.validation_rules

async def dispatch_request(self):
try:
request_method = request.method.lower()
Expand All @@ -98,6 +104,7 @@ async def dispatch_request(self):
root_value=self.get_root_value(),
context_value=self.get_context(),
middleware=self.get_middleware(),
validation_rules=self.get_validation_rules(),
)
exec_res = (
[
Expand Down
9 changes: 8 additions & 1 deletion graphql_server/sanic/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import partial
from typing import List

from graphql import ExecutionResult, GraphQLError
from graphql import ExecutionResult, GraphQLError, specified_rules
from graphql.type.schema import GraphQLSchema
from sanic.response import HTTPResponse, html
from sanic.views import HTTPMethodView
Expand Down Expand Up @@ -36,6 +36,7 @@ class GraphQLView(HTTPMethodView):
graphiql_template = None
graphiql_html_title = None
middleware = None
validation_rules = None
batch = False
jinja_env = None
max_age = 86400
Expand Down Expand Up @@ -77,6 +78,11 @@ def get_context(self, request):
def get_middleware(self):
return self.middleware

def get_validation_rules(self):
if self.validation_rules is None:
return specified_rules
return self.validation_rules

async def dispatch_request(self, request, *args, **kwargs):
try:
request_method = request.method.lower()
Expand All @@ -103,6 +109,7 @@ async def dispatch_request(self, request, *args, **kwargs):
root_value=self.get_root_value(),
context_value=self.get_context(request),
middleware=self.get_middleware(),
validation_rules=self.get_validation_rules(),
)
exec_res = (
[
Expand Down
8 changes: 8 additions & 0 deletions graphql_server/webob/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from graphql.error import GraphQLError
from graphql.type.schema import GraphQLSchema
from graphql import specified_rules
from webob import Response

from graphql_server import (
Expand Down Expand Up @@ -35,6 +36,7 @@ class GraphQLView:
graphiql_template = None
graphiql_html_title = None
middleware = None
validation_rules = None
batch = False
enable_async = False
subscriptions = None
Expand Down Expand Up @@ -73,6 +75,11 @@ def get_context(self, request):
def get_middleware(self):
return self.middleware

def get_validation_rules(self):
if self.validation_rules is None:
return specified_rules
return self.validation_rules

def dispatch_request(self, request):
try:
request_method = request.method.lower()
Expand All @@ -98,6 +105,7 @@ def dispatch_request(self, request):
root_value=self.get_root_value(),
context_value=self.get_context(request),
middleware=self.get_middleware(),
validation_rules=self.get_validation_rules(),
)
result, status_code = encode_execution_results(
execution_results,
Expand Down