Skip to content

Commit

Permalink
IsSelected Refinements (#6957)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Mar 2, 2024
1 parent 0ceeb66 commit 866fdd3
Show file tree
Hide file tree
Showing 35 changed files with 1,011 additions and 145 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Resolvers;

namespace HotChocolate.Execution.Processing;

internal sealed class EmptySelectionCollection : ISelectionCollection
{
private static readonly ISelection[] _empty = Array.Empty<ISelection>();

public static EmptySelectionCollection Instance { get; } = new();

public int Count => 0;

public ISelection this[int index] => throw new IndexOutOfRangeException();

public ISelectionCollection Select(string fieldName)
=> Instance;

public bool IsSelected(string fieldName)
=> false;

public bool IsSelected(string fieldName1, string fieldName2)
=> false;

public bool IsSelected(string fieldName1, string fieldName2, string fieldName3)
=> false;

public bool IsSelected(ISet<string> fieldNames)
=> false;

public IEnumerator<ISelection> GetEnumerator()
=> _empty.AsEnumerable().GetEnumerator();

IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Execution.Properties;
Expand Down Expand Up @@ -46,49 +44,7 @@ public IServiceProvider Services
public CancellationToken RequestAborted { get; private set; }

public bool HasCleanupTasks => _cleanupTasks.Count > 0;

public IReadOnlyList<ISelection> GetSelections(
IObjectType typeContext,
ISelection? selection = null,
bool allowInternals = false)
{
if (typeContext is null)
{
throw new ArgumentNullException(nameof(typeContext));
}

selection ??= _selection;

if (selection.SelectionSet is null)
{
return Array.Empty<ISelection>();
}

var selectionSet = _operationContext.CollectFields(selection, typeContext);

if (selectionSet.IsConditional)
{
var operationIncludeFlags = _operationContext.IncludeFlags;
var selectionCount = selectionSet.Selections.Count;
ref var selectionRef = ref ((SelectionSet)selectionSet).GetSelectionsReference();
var finalFields = new List<ISelection>();

for (var i = 0; i < selectionCount; i++)
{
var childSelection = Unsafe.Add(ref selectionRef, i);

if (childSelection.IsIncluded(operationIncludeFlags, allowInternals))
{
finalFields.Add(childSelection);
}
}

return finalFields;
}

return selectionSet.Selections;
}


public void ReportError(string errorMessage)
{
if (string.IsNullOrEmpty(errorMessage))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using HotChocolate.Resolvers;
using HotChocolate.Types;

Expand Down Expand Up @@ -39,4 +42,54 @@ public bool TryCreatePureContext(
context = null;
return false;
}
}

public IReadOnlyList<ISelection> GetSelections(
IObjectType typeContext,
ISelection? selection = null,
bool allowInternals = false)
{
if (typeContext is null)
{
throw new ArgumentNullException(nameof(typeContext));
}

selection ??= _selection;

if (selection.SelectionSet is null)
{
return Array.Empty<ISelection>();
}

var selectionSet = _operationContext.CollectFields(selection, typeContext);

if (selectionSet.IsConditional)
{
var operationIncludeFlags = _operationContext.IncludeFlags;
var selectionCount = selectionSet.Selections.Count;
ref var selectionRef = ref ((SelectionSet)selectionSet).GetSelectionsReference();
var finalFields = new List<ISelection>();

for (var i = 0; i < selectionCount; i++)
{
var childSelection = Unsafe.Add(ref selectionRef, i);

if (childSelection.IsIncluded(operationIncludeFlags, allowInternals))
{
finalFields.Add(childSelection);
}
}

return finalFields;
}

return selectionSet.Selections;
}

public ISelectionCollection Select(string fieldName)
=> new SelectionCollection(
Schema,
Operation,
[Selection,],
_operationContext.IncludeFlags)
.Select(fieldName);
}
Loading

0 comments on commit 866fdd3

Please sign in to comment.