torchbox / wagtail-grapple

A Wagtail app that makes building GraphQL endpoints a breeze!
https://wagtail-grapple.readthedocs.io/en/latest/
Other
151 stars 57 forks source link

Document the approach to limiting query depth #361

Open zerolab opened 10 months ago

zerolab commented 10 months ago

Refs:

325 could help with some of the nesting for pages

kbayliss commented 5 months ago

Graphene now includes optional, configurable validation for this, so I don't think we need to do anything within grapple.

To limit query depth, you can append the rule to a custom GraphQLView, e.g.:

from typing import List

import graphql
from django.conf import settings
from graphene import validation as graphene_validation
from graphene_django import views as graphene_django_views
from graphql.error import graphql_error
from graphql.language import ast

class CustomGraphQLView(graphene_django_views.GraphQLView):
    ...

    def validate_query(self, document_ast: ast.DocumentNode) -> List[graphql_error.GraphQLError]:
        return graphql.validate(
            schema=self.schema.graphql_schema,
            document_ast=document_ast,
            rules=[
                graphene_validation.depth_limit_validator(
                    max_depth=settings.MAX_GRAPHQL_QUERY_DEPTH
                ),
            ],
        )
zerolab commented 5 months ago

@kbayliss that's neat. Updated the issue to reflect that this is now a documentation issue. IMHO we want to document this as not everyone will scour the graphene docs