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

Aligned Fusion more with the Fusion spec. #6485

Merged
merged 6 commits into from
Sep 5, 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,9 +11,9 @@ internal static class FusionDirectiveArgumentNames
public const string NameArg = "name";

/// <summary>
/// Gets the name of the client name argument.
/// Gets the name of the client grouping argument.
/// </summary>
public const string ClientNameArg = "clientName";
public const string ClientGroupArg = "group";

/// <summary>
/// Gets the name of the select argument.
Expand Down Expand Up @@ -66,7 +66,7 @@ internal static class FusionDirectiveArgumentNames
public const string VersionArg = "version";

/// <summary>
/// Gets the name of the base address argument.
/// Gets the name of the location argument.
/// </summary>
public const string BaseAddressArg = "baseAddress";
public const string LocationArg = "location";
}
13 changes: 4 additions & 9 deletions src/HotChocolate/Fusion/src/Abstractions/FusionEnumValueNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,17 @@ namespace HotChocolate.Fusion;
internal static class FusionEnumValueNames
{
/// <summary>
/// Gets the name of the query resolver kind.
/// Gets the name of the fetch resolver kind.
/// </summary>
public const string Query = "QUERY";
public const string Fetch = "FETCH";

/// <summary>
/// Gets the name of the batch resolver kind.
/// </summary>
public const string Batch = "BATCH";

/// <summary>
/// Gets the name of the batch by key resolver kind.
/// Gets the name of the subscribe resolver kind.
/// </summary>
public const string BatchByKey = "BATCH_BY_KEY";

/// <summary>
/// Gets the name of the subscription resolver kind.
/// </summary>
public const string Subscription = "SUBSCRIPTION";
public const string Subscribe = "SUBSCRIBE";
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,9 @@ internal static class FusionTypeBaseNames
public static string ReEncodeIdDirective = "reEncodeId";

/// <summary>
/// The base name of the HTTP directive.
/// The base name of the transport directive.
/// </summary>
public const string HttpDirective = "httpClient";

/// <summary>
/// The base name of the HTTP directive.
/// </summary>
public const string WebSocketDirective = "webSocketClient";
public const string TransportDirective = "transport";

/// <summary>
/// The base name of the fusion directive.
Expand Down
25 changes: 7 additions & 18 deletions src/HotChocolate/Fusion/src/Abstractions/FusionTypeNames.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Data;
using System.Diagnostics.CodeAnalysis;
using HotChocolate.Language;
using HotChocolate.Utilities;
Expand All @@ -21,8 +20,7 @@ private FusionTypeNames(
string isDirective,
string nodeDirective,
string reEncodeIdDirective,
string httpDirective,
string webSocketDirective,
string transportDirective,
string fusionDirective,
string selectionScalar,
string selectionSetScalar,
Expand All @@ -39,8 +37,7 @@ private FusionTypeNames(
IsDirective = isDirective;
NodeDirective = nodeDirective;
ReEncodeIdDirective = reEncodeIdDirective;
HttpDirective = httpDirective;
WebSocketDirective = webSocketDirective;
TransportDirective = transportDirective;
FusionDirective = fusionDirective;
SelectionScalar = selectionScalar;
SelectionSetScalar = selectionSetScalar;
Expand All @@ -56,8 +53,7 @@ private FusionTypeNames(
_fusionDirectives.Add(isDirective);
_fusionDirectives.Add(nodeDirective);
_fusionDirectives.Add(reEncodeIdDirective);
_fusionDirectives.Add(httpDirective);
_fusionDirectives.Add(webSocketDirective);
_fusionDirectives.Add(transportDirective);
_fusionDirectives.Add(fusionDirective);

_fusionTypes.Add(selectionScalar);
Expand Down Expand Up @@ -106,14 +102,9 @@ private FusionTypeNames(


/// <summary>
/// Gets the name of the http directive.
/// Gets the name of the transport directive.
/// </summary>
public string HttpDirective { get; }

/// <summary>
/// Gets the name of the http directive.
/// </summary>
public string WebSocketDirective { get; }
public string TransportDirective { get; }

/// <summary>
/// Gets the name of the fusion directive.
Expand Down Expand Up @@ -205,8 +196,7 @@ public static FusionTypeNames Create(string? prefix = null, bool prefixSelf = fa
$"{prefix}_{FusionTypeBaseNames.IsDirective}",
$"{prefix}_{FusionTypeBaseNames.NodeDirective}",
$"{prefix}_{FusionTypeBaseNames.ReEncodeIdDirective}",
$"{prefix}_{FusionTypeBaseNames.HttpDirective}",
$"{prefix}_{FusionTypeBaseNames.WebSocketDirective}",
$"{prefix}_{FusionTypeBaseNames.TransportDirective}",
prefixSelf
? $"{prefix}_{FusionTypeBaseNames.FusionDirective}"
: FusionTypeBaseNames.FusionDirective,
Expand All @@ -227,8 +217,7 @@ public static FusionTypeNames Create(string? prefix = null, bool prefixSelf = fa
FusionTypeBaseNames.IsDirective,
FusionTypeBaseNames.NodeDirective,
FusionTypeBaseNames.ReEncodeIdDirective,
FusionTypeBaseNames.HttpDirective,
FusionTypeBaseNames.WebSocketDirective,
FusionTypeBaseNames.TransportDirective,
FusionTypeBaseNames.FusionDirective,
$"_{FusionTypeBaseNames.Selection}",
$"_{FusionTypeBaseNames.SelectionSet}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ public enum EntityResolverKind
{
Single = 0,
Batch = 1,
BatchWithKey = 2,
Subscription = 3
Subscribe = 2
}
123 changes: 63 additions & 60 deletions src/HotChocolate/Fusion/src/Composition/FusionTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,22 @@ public FusionTypes(Schema fusionGraph, string? prefix = null, bool prefixSelf =
nameof(fusionGraph));
}

if (!_fusionGraph.Types.TryGetType<ScalarType>(SpecScalarTypes.Boolean, out var boolean))
if (!_fusionGraph.Types.TryGetType<ScalarType>(SpecScalarTypes.Boolean, out var booleanType))
{
boolean = new ScalarType(SpecScalarTypes.Boolean) { IsSpecScalar = true };
_fusionGraph.Types.Add(boolean);
booleanType = new ScalarType(SpecScalarTypes.Boolean) { IsSpecScalar = true };
_fusionGraph.Types.Add(booleanType);
}

if (!_fusionGraph.Types.TryGetType<ScalarType>(SpecScalarTypes.Int, out var integer))
if (!_fusionGraph.Types.TryGetType<ScalarType>(SpecScalarTypes.Int, out var intType))
{
integer = new ScalarType(SpecScalarTypes.Int) { IsSpecScalar = true };
_fusionGraph.Types.Add(integer);
intType = new ScalarType(SpecScalarTypes.Int) { IsSpecScalar = true };
_fusionGraph.Types.Add(intType);
}

if (!_fusionGraph.Types.TryGetType<ScalarType>(SpecScalarTypes.String, out var stringType))
{
stringType = new ScalarType(SpecScalarTypes.String) { IsSpecScalar = true };
_fusionGraph.Types.Add(stringType);
}

Selection = RegisterScalarType(names.SelectionScalar);
Expand Down Expand Up @@ -76,14 +82,11 @@ public FusionTypes(Schema fusionGraph, string? prefix = null, bool prefixSelf =
Fusion = RegisterFusionDirectiveType(
names.FusionDirective,
TypeName,
boolean,
integer);
HttpClient = RegisterHttpDirectiveType(
names.HttpDirective,
TypeName,
Uri);
WebSocketClient = RegisterWebSocketDirectiveType(
names.WebSocketDirective,
booleanType,
intType);
Transport = RegisterTransportDirectiveType(
names.TransportDirective,
stringType,
TypeName,
Uri);
}
Expand Down Expand Up @@ -114,9 +117,7 @@ public FusionTypes(Schema fusionGraph, string? prefix = null, bool prefixSelf =

public DirectiveType ReEncodeId { get; }

public DirectiveType HttpClient { get; }

public DirectiveType WebSocketClient { get; }
public DirectiveType Transport { get; }

public DirectiveType Fusion { get; }

Expand Down Expand Up @@ -144,10 +145,9 @@ private InputObjectType RegisterArgumentDefType(
private EnumType RegisterResolverKindType(string name)
{
var resolverKind = new EnumType(name);
resolverKind.Values.Add(new EnumValue(FusionEnumValueNames.Query));
resolverKind.Values.Add(new EnumValue(FusionEnumValueNames.Fetch));
resolverKind.Values.Add(new EnumValue(FusionEnumValueNames.Batch));
resolverKind.Values.Add(new EnumValue(FusionEnumValueNames.BatchByKey));
resolverKind.Values.Add(new EnumValue(FusionEnumValueNames.Subscription));
resolverKind.Values.Add(new EnumValue(FusionEnumValueNames.Subscribe));
resolverKind.ContextData.Add(WellKnownContextData.IsFusionType, true);
_fusionGraph.Types.Add(resolverKind);
return resolverKind;
Expand Down Expand Up @@ -237,8 +237,7 @@ public Directive CreateResolverDirective(
var kindValue = kind switch
{
EntityResolverKind.Batch => FusionEnumValueNames.Batch,
EntityResolverKind.BatchWithKey => FusionEnumValueNames.BatchByKey,
EntityResolverKind.Subscription => FusionEnumValueNames.Subscription,
EntityResolverKind.Subscribe => FusionEnumValueNames.Subscribe,
_ => throw new NotSupportedException()
};

Expand All @@ -256,12 +255,11 @@ private DirectiveType RegisterResolverDirectiveType(
EnumType resolverKind)
{
var directiveType = new DirectiveType(name);
directiveType.Locations |= DirectiveLocation.Object;
directiveType.Arguments.Add(new InputField(SelectArg, new NonNullType(selectionSet)));
directiveType.Arguments.Add(new InputField(SubgraphArg, new NonNullType(typeName)));
directiveType.Arguments.Add(
new InputField(ArgumentsArg, new ListType(new NonNullType(argumentDef))));
directiveType.Arguments.Add(new InputField(ArgumentsArg, new ListType(new NonNullType(argumentDef))));
directiveType.Arguments.Add(new InputField(KindArg, resolverKind));
directiveType.Locations |= DirectiveLocation.Object;
directiveType.ContextData.Add(WellKnownContextData.IsFusionType, true);
_fusionGraph.DirectiveTypes.Add(directiveType);
return directiveType;
Expand All @@ -279,11 +277,25 @@ public Directive CreateSourceDirective(string subgraphName, string? originalName

private DirectiveType RegisterSourceDirectiveType(string name, ScalarType typeName)
{
var directiveType = new DirectiveType(name);
directiveType.Locations = DirectiveLocation.FieldDefinition;
directiveType.Arguments.Add(new InputField(SubgraphArg, new NonNullType(typeName)));
directiveType.Arguments.Add(new InputField(NameArg, typeName));
directiveType.ContextData.Add(WellKnownContextData.IsFusionType, true);
var directiveType = new DirectiveType(name)
{
Locations = DirectiveLocation.Object |
DirectiveLocation.FieldDefinition |
DirectiveLocation.Enum |
DirectiveLocation.EnumValue |
DirectiveLocation.InputObject |
DirectiveLocation.InputFieldDefinition |
DirectiveLocation.Scalar,
Arguments =
{
new InputField(SubgraphArg, new NonNullType(typeName)),
new InputField(NameArg, typeName)
},
ContextData =
{
[WellKnownContextData.IsFusionType] = true
}
};
_fusionGraph.DirectiveTypes.Add(directiveType);
return directiveType;
}
Expand All @@ -310,59 +322,50 @@ private DirectiveType RegisterNodeDirectiveType(string name, ScalarType typeName
return directiveType;
}

public Directive CreateHttpDirective(string subgraphName, string? clientName, Uri baseAddress)
public Directive CreateHttpDirective(string subgraphName, string? clientName, Uri location)
=> clientName is null
? new Directive(
HttpClient,
Transport,
new Argument(SubgraphArg, subgraphName),
new Argument(BaseAddressArg, baseAddress.ToString()))
new Argument(LocationArg, location.ToString()),
new Argument(KindArg, "HTTP"))
: new Directive(
HttpClient,
Transport,
new Argument(SubgraphArg, subgraphName),
new Argument(ClientNameArg, clientName),
new Argument(BaseAddressArg, baseAddress.ToString()));
new Argument(ClientGroupArg, clientName),
new Argument(LocationArg, location.ToString()),
new Argument(KindArg, "HTTP"));

private DirectiveType RegisterHttpDirectiveType(
private DirectiveType RegisterTransportDirectiveType(
string name,
ScalarType stringType,
ScalarType typeName,
ScalarType uri)
{
var directiveType = new DirectiveType(name);
directiveType.Locations = DirectiveLocation.FieldDefinition;
directiveType.Arguments.Add(new InputField(SubgraphArg, new NonNullType(typeName)));
directiveType.Arguments.Add(new InputField(ClientNameArg, typeName));
directiveType.Arguments.Add(new InputField(BaseAddressArg, uri));
directiveType.Arguments.Add(new InputField(ClientGroupArg, typeName));
directiveType.Arguments.Add(new InputField(LocationArg, uri));
directiveType.Arguments.Add(new InputField(KindArg, new NonNullType(stringType)));
directiveType.ContextData.Add(WellKnownContextData.IsFusionType, true);
_fusionGraph.DirectiveTypes.Add(directiveType);
return directiveType;
}

public Directive CreateWebSocketDirective(string subgraphName, string? clientName, Uri baseAddress)
public Directive CreateWebSocketDirective(string subgraphName, string? clientName, Uri location)
=> clientName is null
? new Directive(
WebSocketClient,
Transport,
new Argument(SubgraphArg, subgraphName),
new Argument(BaseAddressArg, baseAddress.ToString()))
new Argument(LocationArg, location.ToString()),
new Argument(KindArg, "WebSocket"))
: new Directive(
WebSocketClient,
Transport,
new Argument(SubgraphArg, subgraphName),
new Argument(ClientNameArg, clientName),
new Argument(BaseAddressArg, baseAddress.ToString()));

private DirectiveType RegisterWebSocketDirectiveType(
string name,
ScalarType typeName,
ScalarType uri)
{
var directiveType = new DirectiveType(name);
directiveType.Locations = DirectiveLocation.FieldDefinition;
directiveType.Arguments.Add(new InputField(SubgraphArg, new NonNullType(typeName)));
directiveType.Arguments.Add(new InputField(ClientNameArg, typeName));
directiveType.Arguments.Add(new InputField(BaseAddressArg, uri));
directiveType.ContextData.Add(WellKnownContextData.IsFusionType, true);
_fusionGraph.DirectiveTypes.Add(directiveType);
return directiveType;
}
new Argument(ClientGroupArg, clientName),
new Argument(LocationArg, location.ToString()),
new Argument(KindArg, "WebSocket"));

private DirectiveType RegisterFusionDirectiveType(
string name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private static void ResolveWithNodes(

// Create a new EntityResolver for the entity
var resolver = new EntityResolver(
EntityResolverKind.BatchWithKey,
EntityResolverKind.Batch,
selectionSet,
sourceType.Name,
sourceSchema.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private static void TryRegisterBatchEntityResolver(

// Create a new EntityResolver for the entity
var resolver = new EntityResolver(
EntityResolverKind.BatchWithKey,
EntityResolverKind.Batch,
selectionSet,
entityType.Name,
schema.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ValueTask EnrichAsync(

// Create a new EntityResolver for the entity
var resolver = new EntityResolver(
EntityResolverKind.BatchWithKey,
EntityResolverKind.Batch,
selectionSet,
type.Name,
schema.Name);
Expand Down Expand Up @@ -92,6 +92,7 @@ private static void TryRegisterEntityResolver(
if ((entityResolverField.Type == entityType ||
(entityResolverField.Type.Kind is TypeKind.NonNull &&
entityResolverField.Type.InnerType() == entityType)) &&
entityResolverField.Arguments.Count > 0 &&
entityResolverField.Arguments.All(t => t.ContainsIsDirective()))
{
var arguments = new List<ArgumentNode>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static Directive CreateResolverDirective(
subgraphName,
selectionSet,
arguments,
EntityResolverKind.Subscription);
EntityResolverKind.Subscribe);

private static Directive CreateVariableDirective(
CompositionContext context,
Expand Down
Loading