/
SelectionSet.cs
104 lines (92 loc) · 3.02 KB
/
SelectionSet.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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using HotChocolate.Language;
namespace HotChocolate.Execution.Processing;
/// <summary>
/// A selection set is primarily composed of field selections.
/// When needed a selection set can preserve fragments so that the execution engine
/// can branch the processing of these fragments.
/// </summary>
internal sealed class SelectionSet : ISelectionSet
{
private static readonly Fragment[] _empty = Array.Empty<Fragment>();
private readonly Selection[] _selections;
private readonly Fragment[] _fragments;
private Flags _flags;
/// <summary>
/// Initializes a new instance of <see cref="SelectionSet"/>.
/// </summary>
/// <param name="id">
/// The selection set unique id.
/// </param>
/// <param name="selections">
/// A list of executable field selections.
/// </param>
/// <param name="fragments">
/// A list of preserved fragments that can be used to branch of
/// some of the execution.
/// </param>
/// <param name="isConditional">
/// Defines if this list needs post processing for skip and include.
/// </param>
public SelectionSet(
int id,
Selection[] selections,
Fragment[]? fragments,
bool isConditional)
{
Id = id;
_selections = selections;
_fragments = fragments ?? _empty;
_flags = isConditional ? Flags.Conditional : Flags.None;
}
/// <inheritdoc />
public int Id { get; }
/// <inheritdoc />
public bool IsConditional => (_flags & Flags.Conditional) == Flags.Conditional;
/// <inheritdoc />
public IReadOnlyList<ISelection> Selections => _selections;
/// <inheritdoc />
public IReadOnlyList<IFragment> Fragments => _fragments;
/// <summary>
/// Completes the selection set without sealing it.
/// </summary>
internal void Complete()
{
if ((_flags & Flags.Sealed) != Flags.Sealed)
{
for (var i = 0; i < _selections.Length; i++)
{
_selections[i].Complete(this);
}
}
}
internal void Seal()
{
if ((_flags & Flags.Sealed) != Flags.Sealed)
{
for (var i = 0; i < _selections.Length; i++)
{
_selections[i].Seal(this);
}
_flags |= Flags.Sealed;
}
}
/// <summary>
/// Returns a reference to the 0th element of the underlying selections array.
/// If the selections array is empty, returns a reference to the location where the 0th element
/// would have been stored. Such a reference may or may not be null.
/// It can be used for pinning but must never be de-referenced.
/// This is only meant for use by the execution engine.
/// </summary>
internal ref Selection GetSelectionsReference()
=> ref MemoryMarshal.GetReference(_selections.AsSpan());
[Flags]
private enum Flags
{
None = 0,
Conditional = 1,
Sealed = 2
}
}