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

Infer Entity Resolvers with Patterns #6470

Merged
merged 5 commits into from
Aug 25, 2023
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
Expand Up @@ -11,5 +11,5 @@ public interface ISourceStream : IAsyncDisposable
/// <summary>
/// Reads the subscription result from the pub/sub system.
/// </summary>
IAsyncEnumerable<object> ReadEventsAsync();
IAsyncEnumerable<object?> ReadEventsAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,15 @@ public IAsyncEnumerator<IQueryResult> GetAsyncEnumerator(

private sealed class SubscriptionEnumerator : IAsyncEnumerator<IQueryResult>
{
private readonly IAsyncEnumerator<object> _eventEnumerator;
private readonly IAsyncEnumerator<object?> _eventEnumerator;
private readonly Func<object, Task<IQueryResult>> _onEvent;
private readonly Subscription _subscription;
private readonly IExecutionDiagnosticEvents _diagnosticEvents;
private readonly CancellationToken _requestAborted;
private bool _disposed;

public SubscriptionEnumerator(
IAsyncEnumerator<object> eventEnumerator,
IAsyncEnumerator<object?> eventEnumerator,
Func<object, Task<IQueryResult>> onEvent,
Subscription subscription,
IExecutionDiagnosticEvents diagnosticEvents,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal sealed class EntityMetadata
/// <summary>
/// Gets the list of entity resolvers associated with this entity.
/// </summary>
public List<EntityResolver> EntityResolvers { get; } = new();
public EntityResolversCollection EntityResolvers { get; } = new();

/// <summary>
/// Gets fields that have dependencies on other fields.
Expand All @@ -37,4 +37,4 @@ public override string ToString()

return sb.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Collections;
using System.Text;
using HotChocolate.Language;

namespace HotChocolate.Fusion.Composition;

internal class EntityResolversCollection : ICollection<EntityResolver>
{
private readonly List<EntityResolver> _resolvers = new();
private readonly Dictionary<ResolverKey, EntityResolver> _map = new();

public int Count => _resolvers.Count;

public bool IsReadOnly => false;

public bool Contains(EntityResolver item)
=> _map.ContainsKey(item);

public void Add(EntityResolver item)
{
_map.Add(item, item);
_resolvers.Add(item);
}

public bool TryAdd(EntityResolver item)
{
if (_map.TryAdd(item, item))
{
_resolvers.Add(item);
return true;
}

return false;
}

public bool Remove(EntityResolver item)
{
if(_map.TryGetValue(item, out var resolver))
{
_map.Remove(item);
_resolvers.Remove(resolver);
return true;
}

return false;
}

public void Clear()
{
_resolvers.Clear();
_map.Clear();
}

public void CopyTo(EntityResolver[] array, int arrayIndex)
=> _resolvers.CopyTo(array, arrayIndex);

public IEnumerator<EntityResolver> GetEnumerator()
=> _resolvers.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();

private readonly record struct ResolverKey(string SubgraphName, SelectionSetNode SelectionSet)
{
public bool Equals(ResolverKey? other)
{
if (other is null)
{
return false;
}

return string.Equals(SubgraphName, other.Value.SubgraphName) &&
SyntaxComparer.BySyntax.Equals(SelectionSet, other.Value.SelectionSet);
}

public override int GetHashCode()
=> HashCode.Combine(SubgraphName, SyntaxComparer.BySyntax.GetHashCode(SelectionSet));

private bool PrintMembers(StringBuilder builder)
{
builder.AppendLine(SubgraphName);
builder.AppendLine(SelectionSet.ToString());
return true;
}

public override string ToString()
{
var builder = new StringBuilder();
PrintMembers(builder);
return builder.ToString();
}

public static implicit operator ResolverKey(EntityResolver value)
=> new(value.SubgraphName, value.SelectionSet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static string CreateVariableName(
context.Name.Insert(0, type.Name);
return context.Name.ToString();
}

public static VariableDefinition CreateVariableField(
this InputField argument,
IsDirective directive,
Expand Down Expand Up @@ -85,4 +85,4 @@ private sealed class FieldVariableNameContext : ISyntaxVisitorContext
{
public StringBuilder Name { get; } = new();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public FusionGraphComposer(
new IEntityEnricher[]
{
new RefResolverEntityEnricher(),
new PatternEntityEnricher(),
new RequireEnricher(),
new NodeEntityEnricher()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static void ResolveWithNode(
_idVariable.WithVariable(varNode)));

// Add the new EntityResolver to the entity metadata
entity.Metadata.EntityResolvers.Add(resolver);
entity.Metadata.EntityResolvers.TryAdd(resolver);
}

private static void ResolveWithNodes(
Expand Down Expand Up @@ -159,6 +159,6 @@ private static void ResolveWithNodes(
_idsVariable.WithVariable(varNode)));

// Add the new EntityResolver to the entity metadata
entity.Metadata.EntityResolvers.Add(resolver);
entity.Metadata.EntityResolvers.TryAdd(resolver);
}
}
Loading