Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IsSelected Refinements #6957

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading