Home / Function/ createQueryDepthLimiter() — supabase Function Reference

createQueryDepthLimiter() — supabase Function Reference

Architecture documentation for the createQueryDepthLimiter() function in validators.ts from the supabase codebase.

Entity Profile

Dependency Diagram

graph TD
  8b35c97a_93e7_566e_38f0_4af2d3166902["createQueryDepthLimiter()"]
  e738bd1e_c287_abaf_4e50_f4a2be47fbb6["calculateFragmentDepth()"]
  8b35c97a_93e7_566e_38f0_4af2d3166902 -->|calls| e738bd1e_c287_abaf_4e50_f4a2be47fbb6
  style 8b35c97a_93e7_566e_38f0_4af2d3166902 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

apps/docs/app/api/graphql/validators.ts lines 15–89

export function createQueryDepthLimiter(maxDepth: number): ValidationRule {
  const fragmentDepthMap = new Map<string, number>()

  return function limitQueryDepth(context) {
    return {
      Document: {
        leave() {
          fragmentDepthMap.clear()
        },
      },
      Field: {
        enter(node, _key, _parent, path, ancestors) {
          // Skip __typename and introspection fields
          if (node.name.value === '__typename' || node.name.value.startsWith('__')) {
            return
          }

          // Calculate the depth by counting the ancestors that are field nodes
          const fieldAncestors = ancestors.filter(
            (ancestor) => ancestor && 'kind' in ancestor && ancestor.kind === 'Field'
          )
          const depth = fieldAncestors.length + 1

          // Check if this field exceeds the maximum maxDepth
          if (depth > maxDepth) {
            const pathStr = path.join('.')
            context.reportError(
              new GraphQLError(
                `Query exceeds maximum depth of ${maxDepth}. Got depth ${depth} for ${pathStr}.`,
                { nodes: node }
              )
            )
          }
        },
      },
      FragmentDefinition: {
        enter(node) {
          calculateFragmentDepth(node, context, 0, fragmentDepthMap, maxDepth)
          const depth = fragmentDepthMap.get(node.name.value)!

          if (depth > maxDepth) {
            context.reportError(
              new GraphQLError(
                `Fragment "${node.name.value}" exceeds maximum depth of ${maxDepth}.`,
                { nodes: node }
              )
            )
          }
        },
      },
      FragmentSpread: {
        enter(node, _key, _parent, _path, ancestors) {
          const fragmentName = node.name.value
          const fragmentDepth = fragmentDepthMap.get(fragmentName) || 0

          const fieldAncestors = ancestors.filter(
            (ancestor) => ancestor && 'kind' in ancestor && ancestor.kind === 'Field'
          )
          const currentDepth = fieldAncestors.length

          const totalDepth = currentDepth + fragmentDepth

          if (totalDepth > maxDepth) {
            context.reportError(
              new GraphQLError(
                `Fragment spread "${fragmentName}" causes query to exceed maximum depth of ${maxDepth}.`,
                { nodes: node }
              )
            )
          }
        },
      },
    }
  }
}

Subdomains

Frequently Asked Questions

What does createQueryDepthLimiter() do?
createQueryDepthLimiter() is a function in the supabase codebase.
What does createQueryDepthLimiter() call?
createQueryDepthLimiter() calls 1 function(s): calculateFragmentDepth.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free