Skip to content

Commit 86b7926

Browse files
aryaniyapsjkimbo
andauthored
add support for validation rules (#83)
Co-authored-by: Aryan Iyappan <[email protected]> Co-authored-by: Jonathan Kim <[email protected]>
1 parent b8705c2 commit 86b7926

File tree

10 files changed

+47
-3
lines changed

10 files changed

+47
-3
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ target/
158158
# pyenv
159159
.python-version
160160

161+
# Pycharm venv
162+
venv/
163+
161164
# pipenv
162165
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
163166
# However, in case of collaboration, if having platform-specific dependencies or dependencies

docs/aiohttp.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ gql_view(request) # <-- the instance is callable and expects a `aiohttp.web.Req
5959
`Template.render_async` instead of `Template.render`. If environment is not set, fallbacks to simple regex-based renderer.
6060
* `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))
6161
* `middleware`: A list of graphql [middlewares](http://docs.graphene-python.org/en/latest/execution/middleware/).
62+
* `validation_rules`: A list of graphql validation rules.
6263
* `max_age`: Sets the response header Access-Control-Max-Age for preflight requests.
6364
* `encode`: the encoder to use for responses (sensibly defaults to `graphql_server.json_encode`).
6465
* `format_error`: the error formatter to use for responses (sensibly defaults to `graphql_server.default_format_error`.

docs/flask.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ More info at [Graphene v3 release notes](https://github.com/graphql-python/graph
5858
* `graphiql_html_title`: The graphiql title to display. Defaults to **"GraphiQL"**.
5959
* `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))
6060
* `middleware`: A list of graphql [middlewares](http://docs.graphene-python.org/en/latest/execution/middleware/).
61+
* `validation_rules`: A list of graphql validation rules.
6162
* `encode`: the encoder to use for responses (sensibly defaults to `graphql_server.json_encode`).
6263
* `format_error`: the error formatter to use for responses (sensibly defaults to `graphql_server.default_format_error`.
6364
* `subscriptions`: The GraphiQL socket endpoint for using subscriptions in graphql-ws.

docs/sanic.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ This will add `/graphql` endpoint to your app and enable the GraphiQL IDE.
5151
`Template.render_async` instead of `Template.render`. If environment is not set, fallbacks to simple regex-based renderer.
5252
* `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))
5353
* `middleware`: A list of graphql [middlewares](http://docs.graphene-python.org/en/latest/execution/middleware/).
54+
* `validation_rules`: A list of graphql validation rules.
5455
* `max_age`: Sets the response header Access-Control-Max-Age for preflight requests.
5556
* `encode`: the encoder to use for responses (sensibly defaults to `graphql_server.json_encode`).
5657
* `format_error`: the error formatter to use for responses (sensibly defaults to `graphql_server.default_format_error`.

docs/webob.md

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This will add `/graphql` endpoint to your app and enable the GraphiQL IDE.
4848
* `graphiql_html_title`: The graphiql title to display. Defaults to **"GraphiQL"**.
4949
* `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))
5050
* `middleware`: A list of graphql [middlewares](http://docs.graphene-python.org/en/latest/execution/middleware/).
51+
* `validation_rules`: A list of graphql validation rules.
5152
* `encode`: the encoder to use for responses (sensibly defaults to `graphql_server.json_encode`).
5253
* `format_error`: the error formatter to use for responses (sensibly defaults to `graphql_server.default_format_error`.
5354
* `enable_async`: whether `async` mode will be enabled.

graphql_server/aiohttp/graphqlview.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import List
55

66
from aiohttp import web
7-
from graphql import ExecutionResult, GraphQLError
7+
from graphql import ExecutionResult, GraphQLError, specified_rules
88
from graphql.type.schema import GraphQLSchema
99

1010
from graphql_server import (
@@ -34,6 +34,7 @@ class GraphQLView:
3434
graphiql_template = None
3535
graphiql_html_title = None
3636
middleware = None
37+
validation_rules = None
3738
batch = False
3839
jinja_env = None
3940
max_age = 86400
@@ -75,6 +76,11 @@ def get_context(self, request):
7576
def get_middleware(self):
7677
return self.middleware
7778

79+
def get_validation_rules(self):
80+
if self.validation_rules is None:
81+
return specified_rules
82+
return self.validation_rules
83+
7884
@staticmethod
7985
async def parse_body(request):
8086
content_type = request.content_type
@@ -149,6 +155,7 @@ async def __call__(self, request):
149155
root_value=self.get_root_value(),
150156
context_value=self.get_context(request),
151157
middleware=self.get_middleware(),
158+
validation_rules=self.get_validation_rules(),
152159
)
153160

154161
exec_res = (

graphql_server/flask/graphqlview.py

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from flask.views import View
88
from graphql.error import GraphQLError
99
from graphql.type.schema import GraphQLSchema
10+
from graphql import specified_rules
1011

1112
from graphql_server import (
1213
GraphQLParams,
@@ -35,6 +36,7 @@ class GraphQLView(View):
3536
graphiql_template = None
3637
graphiql_html_title = None
3738
middleware = None
39+
validation_rules = None
3840
batch = False
3941
subscriptions = None
4042
headers = None
@@ -73,6 +75,11 @@ def get_context(self):
7375
def get_middleware(self):
7476
return self.middleware
7577

78+
def get_validation_rules(self):
79+
if self.validation_rules is None:
80+
return specified_rules
81+
return self.validation_rules
82+
7683
def dispatch_request(self):
7784
try:
7885
request_method = request.method.lower()
@@ -95,6 +102,7 @@ def dispatch_request(self):
95102
root_value=self.get_root_value(),
96103
context_value=self.get_context(),
97104
middleware=self.get_middleware(),
105+
validation_rules=self.get_validation_rules(),
98106
)
99107
result, status_code = encode_execution_results(
100108
execution_results,

graphql_server/quart/graphqlview.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from functools import partial
55
from typing import List
66

7-
from graphql import ExecutionResult
7+
from graphql import ExecutionResult, specified_rules
88
from graphql.error import GraphQLError
99
from graphql.type.schema import GraphQLSchema
1010
from quart import Response, render_template_string, request
@@ -37,6 +37,7 @@ class GraphQLView(View):
3737
graphiql_template = None
3838
graphiql_html_title = None
3939
middleware = None
40+
validation_rules = None
4041
batch = False
4142
enable_async = False
4243
subscriptions = None
@@ -76,6 +77,11 @@ def get_context(self):
7677
def get_middleware(self):
7778
return self.middleware
7879

80+
def get_validation_rules(self):
81+
if self.validation_rules is None:
82+
return specified_rules
83+
return self.validation_rules
84+
7985
async def dispatch_request(self):
8086
try:
8187
request_method = request.method.lower()
@@ -98,6 +104,7 @@ async def dispatch_request(self):
98104
root_value=self.get_root_value(),
99105
context_value=self.get_context(),
100106
middleware=self.get_middleware(),
107+
validation_rules=self.get_validation_rules(),
101108
)
102109
exec_res = (
103110
[

graphql_server/sanic/graphqlview.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from functools import partial
55
from typing import List
66

7-
from graphql import ExecutionResult, GraphQLError
7+
from graphql import ExecutionResult, GraphQLError, specified_rules
88
from graphql.type.schema import GraphQLSchema
99
from sanic.response import HTTPResponse, html
1010
from sanic.views import HTTPMethodView
@@ -36,6 +36,7 @@ class GraphQLView(HTTPMethodView):
3636
graphiql_template = None
3737
graphiql_html_title = None
3838
middleware = None
39+
validation_rules = None
3940
batch = False
4041
jinja_env = None
4142
max_age = 86400
@@ -77,6 +78,11 @@ def get_context(self, request):
7778
def get_middleware(self):
7879
return self.middleware
7980

81+
def get_validation_rules(self):
82+
if self.validation_rules is None:
83+
return specified_rules
84+
return self.validation_rules
85+
8086
async def dispatch_request(self, request, *args, **kwargs):
8187
try:
8288
request_method = request.method.lower()
@@ -103,6 +109,7 @@ async def dispatch_request(self, request, *args, **kwargs):
103109
root_value=self.get_root_value(),
104110
context_value=self.get_context(request),
105111
middleware=self.get_middleware(),
112+
validation_rules=self.get_validation_rules(),
106113
)
107114
exec_res = (
108115
[

graphql_server/webob/graphqlview.py

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from graphql.error import GraphQLError
77
from graphql.type.schema import GraphQLSchema
8+
from graphql import specified_rules
89
from webob import Response
910

1011
from graphql_server import (
@@ -35,6 +36,7 @@ class GraphQLView:
3536
graphiql_template = None
3637
graphiql_html_title = None
3738
middleware = None
39+
validation_rules = None
3840
batch = False
3941
enable_async = False
4042
subscriptions = None
@@ -73,6 +75,11 @@ def get_context(self, request):
7375
def get_middleware(self):
7476
return self.middleware
7577

78+
def get_validation_rules(self):
79+
if self.validation_rules is None:
80+
return specified_rules
81+
return self.validation_rules
82+
7683
def dispatch_request(self, request):
7784
try:
7885
request_method = request.method.lower()
@@ -98,6 +105,7 @@ def dispatch_request(self, request):
98105
root_value=self.get_root_value(),
99106
context_value=self.get_context(request),
100107
middleware=self.get_middleware(),
108+
validation_rules=self.get_validation_rules(),
101109
)
102110
result, status_code = encode_execution_results(
103111
execution_results,

0 commit comments

Comments
 (0)