Skip to content

Commit

Permalink
add support for validation rules (#83)
Browse files Browse the repository at this point in the history
Co-authored-by: Aryan Iyappan <[email protected]>
Co-authored-by: Jonathan Kim <[email protected]>
  • Loading branch information
3 people authored Aug 10, 2021
1 parent b8705c2 commit 86b7926
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 3 deletions.
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

@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

0 comments on commit 86b7926

Please sign in to comment.