Skip to content
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

Add support for validation rules #1475

Merged
merged 7 commits into from Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/settings.rst
Expand Up @@ -269,3 +269,14 @@ Default: ``False``


.. _GraphiQLDocs: https://graphiql-test.netlify.app/typedoc/modules/graphiql_react#graphiqlprovider-2


``MAX_VALIDATION_ERRORS``
------------------------------------

In case ``validation_rules`` are provided to ``GraphQLView``, if this is set to a non-negative ``int`` value,
``graphql.validation.validate`` will stop validation after this number of errors has been reached.
If not set or set to ``None``, the maximum number of errors will follow ``graphql.validation.validate`` default
*i.e.* 100.

Default: ``None``
1 change: 1 addition & 0 deletions graphene_django/settings.py
Expand Up @@ -43,6 +43,7 @@
"GRAPHIQL_INPUT_VALUE_DEPRECATION": False,
"ATOMIC_MUTATIONS": False,
"TESTING_ENDPOINT": "/graphql",
"MAX_VALIDATION_ERRORS": None,
}

if settings.DEBUG:
Expand Down
11 changes: 10 additions & 1 deletion graphene_django/views.py
Expand Up @@ -96,6 +96,7 @@ class GraphQLView(View):
batch = False
subscription_path = None
execution_context_class = None
validation_rules = None

def __init__(
self,
Expand All @@ -107,6 +108,7 @@ def __init__(
batch=False,
subscription_path=None,
execution_context_class=None,
validation_rules=None,
):
if not schema:
schema = graphene_settings.SCHEMA
Expand Down Expand Up @@ -135,6 +137,8 @@ def __init__(
), "A Schema is required to be provided to GraphQLView."
assert not all((graphiql, batch)), "Use either graphiql or batch processing"

self.validation_rules = validation_rules

# noinspection PyUnusedLocal
def get_root_value(self, request):
return self.root_value
Expand Down Expand Up @@ -332,7 +336,12 @@ def execute_graphql_request(
)
)

validation_errors = validate(schema, document)
validation_errors = validate(
schema,
document,
self.validation_rules,
graphene_settings.MAX_VALIDATION_ERRORS,
)

if validation_errors:
return ExecutionResult(data=None, errors=validation_errors)
Expand Down