Skip to content

Commit 866fdd3

Browse files
authored
IsSelected Refinements (#6957)
1 parent 0ceeb66 commit 866fdd3

35 files changed

+1011
-145
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using HotChocolate.Resolvers;
6+
7+
namespace HotChocolate.Execution.Processing;
8+
9+
internal sealed class EmptySelectionCollection : ISelectionCollection
10+
{
11+
private static readonly ISelection[] _empty = Array.Empty<ISelection>();
12+
13+
public static EmptySelectionCollection Instance { get; } = new();
14+
15+
public int Count => 0;
16+
17+
public ISelection this[int index] => throw new IndexOutOfRangeException();
18+
19+
public ISelectionCollection Select(string fieldName)
20+
=> Instance;
21+
22+
public bool IsSelected(string fieldName)
23+
=> false;
24+
25+
public bool IsSelected(string fieldName1, string fieldName2)
26+
=> false;
27+
28+
public bool IsSelected(string fieldName1, string fieldName2, string fieldName3)
29+
=> false;
30+
31+
public bool IsSelected(ISet<string> fieldNames)
32+
=> false;
33+
34+
public IEnumerator<ISelection> GetEnumerator()
35+
=> _empty.AsEnumerable().GetEnumerator();
36+
37+
IEnumerator IEnumerable.GetEnumerator()
38+
=> GetEnumerator();
39+
}

src/HotChocolate/Core/src/Execution/Processing/MiddlewareContext.Global.cs

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Runtime.CompilerServices;
4-
using System.Runtime.InteropServices;
53
using System.Threading;
64
using System.Threading.Tasks;
75
using HotChocolate.Execution.Properties;
@@ -46,49 +44,7 @@ public IServiceProvider Services
4644
public CancellationToken RequestAborted { get; private set; }
4745

4846
public bool HasCleanupTasks => _cleanupTasks.Count > 0;
49-
50-
public IReadOnlyList<ISelection> GetSelections(
51-
IObjectType typeContext,
52-
ISelection? selection = null,
53-
bool allowInternals = false)
54-
{
55-
if (typeContext is null)
56-
{
57-
throw new ArgumentNullException(nameof(typeContext));
58-
}
59-
60-
selection ??= _selection;
61-
62-
if (selection.SelectionSet is null)
63-
{
64-
return Array.Empty<ISelection>();
65-
}
66-
67-
var selectionSet = _operationContext.CollectFields(selection, typeContext);
68-
69-
if (selectionSet.IsConditional)
70-
{
71-
var operationIncludeFlags = _operationContext.IncludeFlags;
72-
var selectionCount = selectionSet.Selections.Count;
73-
ref var selectionRef = ref ((SelectionSet)selectionSet).GetSelectionsReference();
74-
var finalFields = new List<ISelection>();
75-
76-
for (var i = 0; i < selectionCount; i++)
77-
{
78-
var childSelection = Unsafe.Add(ref selectionRef, i);
79-
80-
if (childSelection.IsIncluded(operationIncludeFlags, allowInternals))
81-
{
82-
finalFields.Add(childSelection);
83-
}
84-
}
85-
86-
return finalFields;
87-
}
88-
89-
return selectionSet.Selections;
90-
}
91-
47+
9248
public void ReportError(string errorMessage)
9349
{
9450
if (string.IsNullOrEmpty(errorMessage))

src/HotChocolate/Core/src/Execution/Processing/MiddlewareContext.Selection.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
using System;
2+
using System.Collections.Generic;
13
using System.Diagnostics.CodeAnalysis;
4+
using System.Runtime.CompilerServices;
25
using HotChocolate.Resolvers;
36
using HotChocolate.Types;
47

@@ -39,4 +42,54 @@ public bool TryCreatePureContext(
3942
context = null;
4043
return false;
4144
}
42-
}
45+
46+
public IReadOnlyList<ISelection> GetSelections(
47+
IObjectType typeContext,
48+
ISelection? selection = null,
49+
bool allowInternals = false)
50+
{
51+
if (typeContext is null)
52+
{
53+
throw new ArgumentNullException(nameof(typeContext));
54+
}
55+
56+
selection ??= _selection;
57+
58+
if (selection.SelectionSet is null)
59+
{
60+
return Array.Empty<ISelection>();
61+
}
62+
63+
var selectionSet = _operationContext.CollectFields(selection, typeContext);
64+
65+
if (selectionSet.IsConditional)
66+
{
67+
var operationIncludeFlags = _operationContext.IncludeFlags;
68+
var selectionCount = selectionSet.Selections.Count;
69+
ref var selectionRef = ref ((SelectionSet)selectionSet).GetSelectionsReference();
70+
var finalFields = new List<ISelection>();
71+
72+
for (var i = 0; i < selectionCount; i++)
73+
{
74+
var childSelection = Unsafe.Add(ref selectionRef, i);
75+
76+
if (childSelection.IsIncluded(operationIncludeFlags, allowInternals))
77+
{
78+
finalFields.Add(childSelection);
79+
}
80+
}
81+
82+
return finalFields;
83+
}
84+
85+
return selectionSet.Selections;
86+
}
87+
88+
public ISelectionCollection Select(string fieldName)
89+
=> new SelectionCollection(
90+
Schema,
91+
Operation,
92+
[Selection,],
93+
_operationContext.IncludeFlags)
94+
.Select(fieldName);
95+
}

0 commit comments

Comments
 (0)