-
-
Notifications
You must be signed in to change notification settings - Fork 724
/
MaxExecutionDepthVisitor.cs
63 lines (53 loc) · 1.84 KB
/
MaxExecutionDepthVisitor.cs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using HotChocolate.Language;
using HotChocolate.Language.Visitors;
using HotChocolate.Validation.Options;
namespace HotChocolate.Validation.Rules
{
internal sealed class MaxExecutionDepthVisitor : DocumentValidatorVisitor
{
private readonly IMaxExecutionDepthOptionsAccessor _options;
public MaxExecutionDepthVisitor(IMaxExecutionDepthOptionsAccessor options)
{
_options = options;
}
protected override ISyntaxVisitorAction Enter(
OperationDefinitionNode node,
IDocumentValidatorContext context)
{
context.Count = 0;
return base.Enter(node, context);
}
protected override ISyntaxVisitorAction Leave(
OperationDefinitionNode node,
IDocumentValidatorContext context)
{
context.Max = context.Count > context.Max ? context.Count : context.Max;
if (_options.MaxAllowedExecutionDepth.HasValue &&
_options.MaxAllowedExecutionDepth < context.Max)
{
context.Errors.Add(context.MaxExecutionDepth(
node, _options.MaxAllowedExecutionDepth.Value, context.Max));
return Break;
}
return base.Leave(node, context);
}
protected override ISyntaxVisitorAction Enter(
FieldNode node,
IDocumentValidatorContext context)
{
context.Fields.Push(node);
if (context.Count < context.Fields.Count)
{
context.Count = context.Fields.Count;
}
return Continue;
}
protected override ISyntaxVisitorAction Leave(
FieldNode node,
IDocumentValidatorContext context)
{
context.Fields.Pop();
return Continue;
}
}
}