-
Notifications
You must be signed in to change notification settings - Fork 13
/
depth.go
39 lines (34 loc) · 1.17 KB
/
depth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package depth
import (
"context"
"github.com/99designs/gqlgen/graphql"
"github.com/vektah/gqlparser/v2/ast"
)
func GetOperationDepth(ctx context.Context) int {
return findSelectionDepth(graphql.GetOperationContext(ctx).Operation.SelectionSet)
}
func findSelectionDepth(selections ast.SelectionSet) int {
maxDepth := 0
for _, selection := range selections {
if field, isField := selection.(*ast.Field); isField && field != nil {
if len(field.SelectionSet) > 0 {
if depth := findSelectionDepth(field.SelectionSet); depth+1 > maxDepth {
maxDepth = depth + 1
}
}
} else if fragment, isFragmentSpread := selection.(*ast.FragmentSpread); isFragmentSpread && fragment != nil {
if len(fragment.Definition.SelectionSet) > 0 {
if depth := findSelectionDepth(fragment.Definition.SelectionSet); depth+1 > maxDepth {
maxDepth = depth + 1
}
}
} else if inlineFragment, isInlineFragment := selection.(*ast.InlineFragment); isInlineFragment && inlineFragment != nil {
if len(inlineFragment.SelectionSet) > 0 {
if depth := findSelectionDepth(inlineFragment.SelectionSet); depth+1 > maxDepth {
maxDepth = depth + 1
}
}
}
}
return maxDepth
}