-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
DirectiveCollectionExtensions.cs
221 lines (186 loc) · 7.49 KB
/
DirectiveCollectionExtensions.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using HotChocolate.Language;
using HotChocolate.Types;
using HotChocolate.Utilities;
using IHasDirectives = HotChocolate.Language.IHasDirectives;
namespace HotChocolate.Execution.Processing;
internal static class DirectiveCollectionExtensions
{
public static IValueNode? SkipValue(this IReadOnlyList<DirectiveNode> directives)
{
var directive = directives.GetSkipDirective();
return directive is null ? null : GetIfArgumentValue(directive);
}
public static IValueNode? IncludeValue(this IReadOnlyList<DirectiveNode> directives)
{
var directive = directives.GetIncludeDirective();
return directive is null ? null : GetIfArgumentValue(directive);
}
public static bool IsDeferrable(this InlineFragmentNode fragmentNode) =>
fragmentNode.Directives.IsDeferrable();
public static bool IsDeferrable(this FragmentSpreadNode fragmentSpreadNode) =>
fragmentSpreadNode.Directives.IsDeferrable();
public static bool IsDeferrable(this IReadOnlyList<DirectiveNode> directives)
{
var directive = directives.GetDeferDirective();
var ifValue = directive?.GetIfArgumentValueOrDefault();
// a fragment is not deferrable if we do not find a defer directive or
// if the `if` of the defer directive is a bool literal with a false value.
return directive is not null && ifValue is not BooleanValueNode { Value: false };
}
public static bool IsStreamable(this FieldNode field) =>
field.Directives.GetStreamDirective() is not null;
private static IValueNode GetIfArgumentValue(DirectiveNode directive)
{
if (directive.Arguments.Count == 1)
{
var argument = directive.Arguments[0];
if (string.Equals(
argument.Name.Value,
WellKnownDirectives.IfArgument,
StringComparison.Ordinal))
{
return argument.Value;
}
}
throw ThrowHelper.MissingIfArgument(directive);
}
private static DirectiveNode? GetSkipDirective(
this IReadOnlyList<DirectiveNode> directives) =>
GetDirective(directives, WellKnownDirectives.Skip);
private static DirectiveNode? GetIncludeDirective(
this IReadOnlyList<DirectiveNode> directives) =>
GetDirective(directives, WellKnownDirectives.Include);
internal static DirectiveNode? GetDeferDirective(
this IHasDirectives container) =>
GetDirective(container.Directives, WellKnownDirectives.Defer);
internal static DeferDirective? GetDeferDirective(
this IReadOnlyList<DirectiveNode> directives,
IVariableValueCollection variables)
{
var directiveNode =
GetDirective(directives, WellKnownDirectives.Defer);
if (directiveNode is not null)
{
var @if = true;
string? label = null;
foreach (var argument in directiveNode.Arguments)
{
switch (argument.Name.Value)
{
case WellKnownDirectives.IfArgument:
@if = argument.Value switch
{
VariableNode variable
=> variables.GetVariable<bool>(variable.Name.Value),
BooleanValueNode b => b.Value,
_ => @if
};
break;
case WellKnownDirectives.LabelArgument:
label = argument.Value switch
{
VariableNode variable
=> variables.GetVariable<string?>(variable.Name.Value),
StringValueNode b => b.Value,
_ => label
};
break;
}
}
return new DeferDirective(@if, label);
}
return null;
}
internal static StreamDirective? GetStreamDirective(
this ISelection selection,
IVariableValueCollection variables) =>
selection.SyntaxNode.Directives.GetStreamDirective(variables);
internal static StreamDirective? GetStreamDirective(
this IReadOnlyList<DirectiveNode> directives,
IVariableValueCollection variables)
{
var directiveNode = GetDirective(directives, WellKnownDirectives.Stream);
if (directiveNode is not null)
{
var @if = true;
string? label = null;
var initialCount = 0;
foreach (var argument in directiveNode.Arguments)
{
switch (argument.Name.Value)
{
case WellKnownDirectives.IfArgument:
@if = argument.Value switch
{
VariableNode variable
=> variables.GetVariable<bool>(variable.Name.Value),
BooleanValueNode b => b.Value,
_ => @if
};
break;
case WellKnownDirectives.LabelArgument:
label = argument.Value switch
{
VariableNode variable
=> variables.GetVariable<string?>(variable.Name.Value),
StringValueNode b => b.Value,
_ => label
};
break;
case WellKnownDirectives.InitialCount:
initialCount = argument.Value switch
{
VariableNode variable
=> variables.GetVariable<int>(variable.Name.Value),
IntValueNode b => b.ToInt32(),
_ => initialCount
};
break;
}
}
return new StreamDirective(@if, initialCount, label);
}
return null;
}
internal static IValueNode? GetIfArgumentValueOrDefault(this DirectiveNode directive)
{
for (var i = 0; i < directive.Arguments.Count; i++)
{
var argument = directive.Arguments[i];
if (argument.Name.Value.EqualsOrdinal(WellKnownDirectives.IfArgument))
{
return argument.Value;
}
}
return null;
}
internal static DirectiveNode? GetDeferDirective(
this IReadOnlyList<DirectiveNode> directives) =>
GetDirective(directives, WellKnownDirectives.Defer);
internal static DirectiveNode? GetStreamDirective(
this FieldNode selection) =>
GetDirective(selection.Directives, WellKnownDirectives.Stream);
internal static DirectiveNode? GetStreamDirective(
this IReadOnlyList<DirectiveNode> directives) =>
GetDirective(directives, WellKnownDirectives.Stream);
private static DirectiveNode? GetDirective(
this IReadOnlyList<DirectiveNode> directives,
string name)
{
if (directives.Count == 0)
{
return null;
}
for (var i = 0; i < directives.Count; i++)
{
var directive = directives[i];
if (directive.Name.Value.EqualsOrdinal(name))
{
return directive;
}
}
return null;
}
}