Home / Function/ calculateFragmentDepth() — supabase Function Reference

calculateFragmentDepth() — supabase Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

apps/docs/app/api/graphql/validators.ts lines 94–157

function calculateFragmentDepth(
  node: FragmentDefinitionNode,
  context: ValidationContext,
  currentDepth: number,
  visitedFragments: Map<string, number>,
  maxDepth: number
) {
  const fragmentName = node.name.value
  if (visitedFragments.has(fragmentName)) {
    return
  }

  let maxFragmentDepth = 0
  // Process all selections in the fragment
  function processSelections(selections: ReadonlyArray<SelectionNode>, depth: number) {
    for (const selection of selections) {
      if (selection.kind === 'Field') {
        // Skip __typename and introspection fields
        if (selection.name.value === '__typename' || selection.name.value.startsWith('__')) {
          continue
        }

        const newDepth = depth + 1
        if (newDepth > maxFragmentDepth) {
          maxFragmentDepth = newDepth
        }
        if (maxFragmentDepth > maxDepth) {
          // Do not recurse anymore because we might go on indefinitely
          continue
        }

        // If there are nested selections, process them
        if (selection.selectionSet) {
          processSelections(selection.selectionSet.selections, newDepth)
        }
      } else if (selection.kind === 'FragmentSpread') {
        // Process fragment spreads
        const spreadName = selection.name.value
        const fragment = context.getFragment(spreadName)
        if (!fragment) continue

        const fragmentName = fragment.name.value
        if (!visitedFragments.has(fragmentName)) {
          calculateFragmentDepth(fragment, context, depth, visitedFragments, maxDepth)
        }
        const fragmentDepth = visitedFragments.get(fragmentName)!
        const totalDepth = fragmentDepth + depth

        if (totalDepth > maxFragmentDepth) {
          maxFragmentDepth = totalDepth
        }
      } else if (selection.kind === 'InlineFragment' && selection.selectionSet) {
        // Process inline fragments
        processSelections(selection.selectionSet.selections, depth)
      }
    }
  }

  if (node.selectionSet) {
    processSelections(node.selectionSet.selections, currentDepth)
  }

  visitedFragments.set(fragmentName, maxFragmentDepth)
}

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free