Skip to content
Draft
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
82 changes: 46 additions & 36 deletions src/HotChocolate/Core/src/Types.CursorPagination/ConnectionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@ internal ConnectionType(
string connectionName,
TypeReference nodeType,
bool includeTotalCount,
bool includeNodesField)
bool includeNodesField,
INamingConventions namingConventions)
{
ArgumentException.ThrowIfNullOrEmpty(connectionName);
ArgumentNullException.ThrowIfNull(nodeType);

ConnectionName = connectionName;
var edgeTypeName = NameHelper.CreateEdgeName(connectionName);
var edgeTypeName = NameHelper.CreateEdgeName(namingConventions, connectionName);

var edgesType =
TypeReference.Parse(
$"[{edgeTypeName}!]",
TypeContext.Output,
factory: _ => new EdgeType(connectionName, nodeType));
factory: c => new EdgeType(connectionName, nodeType, c.Naming));

Configuration = CreateConfiguration(includeTotalCount, includeNodesField, edgesType);
Configuration.Name = NameHelper.CreateConnectionName(connectionName);
Configuration.Dependencies.Add(new(nodeType));
Configuration.Name = NameHelper.CreateConnectionName(namingConventions, connectionName);
Configuration.Dependencies.Add(new TypeDependency(nodeType));
Configuration.Tasks.Add(
new OnCompleteTypeSystemConfigurationTask(
(c, _) => EdgeType = c.GetType<IEdgeType>(TypeReference.Create(edgeTypeName)),
Expand Down Expand Up @@ -68,14 +69,14 @@ internal ConnectionType(TypeReference nodeType, bool includeTotalCount, bool inc
TypeReference.Create(
ContextDataKeys.EdgeType,
nodeType,
_ => new EdgeType(nodeType),
c => new EdgeType(nodeType, c.Naming),
TypeContext.Output);

// the property is set later in the configuration
ConnectionName = null!;
Configuration = CreateConfiguration(includeTotalCount, includeNodesField);
Configuration.Dependencies.Add(new(nodeType));
Configuration.Dependencies.Add(new(edgeType));
Configuration.Dependencies.Add(new TypeDependency(nodeType));
Configuration.Dependencies.Add(new TypeDependency(edgeType));
Configuration.NeedsNameCompletion = true;

Configuration.Tasks.Add(
Expand All @@ -88,9 +89,9 @@ internal ConnectionType(TypeReference nodeType, bool includeTotalCount, bool inc
var definition = (ObjectTypeConfiguration)d;
var edges = definition.Fields.First(IsEdgesField);

definition.Name = NameHelper.CreateConnectionName(ConnectionName);
definition.Name = NameHelper.CreateConnectionName(c.DescriptorContext.Naming, ConnectionName);
edges.Type = TypeReference.Parse(
$"[{NameHelper.CreateEdgeName(ConnectionName)}!]",
$"[{NameHelper.CreateEdgeName(c.DescriptorContext.Naming, ConnectionName)}!]",
TypeContext.Output);

if (includeNodesField)
Expand Down Expand Up @@ -128,7 +129,7 @@ protected override void OnBeforeRegisterDependencies(
ITypeDiscoveryContext context,
TypeSystemConfiguration configuration)
{
context.Dependencies.Add(new(
context.Dependencies.Add(new TypeDependency(
context.TypeInspector.GetOutputTypeRef(typeof(PageInfoType))));

base.OnBeforeRegisterDependencies(context, configuration);
Expand Down Expand Up @@ -156,37 +157,46 @@ private static ObjectTypeConfiguration CreateConfiguration(
RuntimeType = typeof(Connection)
};

definition.Fields.Add(new(
Names.PageInfo,
ConnectionType_PageInfo_Description,
TypeReference.Parse("PageInfo!"),
pureResolver: GetPagingInfo));

definition.Fields.Add(new(
Names.Edges,
ConnectionType_Edges_Description,
edgesType,
pureResolver: GetEdges)
{ Flags = CoreFieldFlags.ConnectionEdgesField });
definition.Fields.Add(
new ObjectFieldConfiguration(
Names.PageInfo,
ConnectionType_PageInfo_Description,
TypeReference.Parse("PageInfo!"),
pureResolver: GetPagingInfo));

definition.Fields.Add(
new ObjectFieldConfiguration(
Names.Edges,
ConnectionType_Edges_Description,
edgesType,
pureResolver: GetEdges)
{
Flags = CoreFieldFlags.ConnectionEdgesField
});

if (includeNodesField)
{
definition.Fields.Add(new(
Names.Nodes,
ConnectionType_Nodes_Description,
pureResolver: GetNodes)
{ Flags = CoreFieldFlags.ConnectionNodesField });
definition.Fields.Add(
new ObjectFieldConfiguration(
Names.Nodes,
ConnectionType_Nodes_Description,
pureResolver: GetNodes)
{
Flags = CoreFieldFlags.ConnectionNodesField
});
}

if (includeTotalCount)
{
definition.Fields.Add(new(
Names.TotalCount,
ConnectionType_TotalCount_Description,
type: TypeReference.Parse($"{ScalarNames.Int}!"),
pureResolver: GetTotalCount)
{
Flags = CoreFieldFlags.TotalCount
});
definition.Fields.Add(
new ObjectFieldConfiguration(
Names.TotalCount,
ConnectionType_TotalCount_Description,
type: TypeReference.Parse($"{ScalarNames.Int}!"),
pureResolver: GetTotalCount)
{
Flags = CoreFieldFlags.TotalCount
});
}

return definition;
Expand Down
9 changes: 5 additions & 4 deletions src/HotChocolate/Core/src/Types.CursorPagination/EdgeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ internal sealed class EdgeType : ObjectType, IEdgeType
{
internal EdgeType(
string connectionName,
TypeReference nodeType)
TypeReference nodeType,
INamingConventions namingConventions)
{
ArgumentException.ThrowIfNullOrEmpty(connectionName);
ArgumentNullException.ThrowIfNull(nodeType);

ConnectionName = connectionName;
Configuration = CreateConfiguration(nodeType);
Configuration.Name = NameHelper.CreateEdgeName(connectionName);
Configuration.Name = NameHelper.CreateEdgeName(namingConventions, connectionName);
Configuration.Tasks.Add(
new OnCompleteTypeSystemConfigurationTask(
(c, _) => NodeType = c.GetType<IOutputType>(nodeType),
Configuration,
ApplyConfigurationOn.BeforeCompletion));
}

internal EdgeType(TypeReference nodeType)
internal EdgeType(TypeReference nodeType, INamingConventions namingConventions)
{
ArgumentNullException.ThrowIfNull(nodeType);

Expand All @@ -40,7 +41,7 @@ internal EdgeType(TypeReference nodeType)
{
var type = c.GetType<IType>(nodeType);
ConnectionName = type.NamedType().Name;
((ObjectTypeConfiguration)d).Name = NameHelper.CreateEdgeName(ConnectionName);
((ObjectTypeConfiguration)d).Name = NameHelper.CreateEdgeName(namingConventions, ConnectionName);
},
Configuration,
ApplyConfigurationOn.BeforeNaming,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,12 @@ private static TypeReference CreateConnectionType(
: TypeReference.Create(
connectionName + "Connection",
TypeContext.Output,
factory: _ => new ConnectionType(
factory: c => new ConnectionType(
connectionName,
nodeType,
includeTotalCount,
includeNodesField));
includeNodesField,
c.Naming));
}

private static string EnsureConnectionNameCasing(string connectionName)
Expand Down
14 changes: 10 additions & 4 deletions src/HotChocolate/Core/src/Types.CursorPagination/NameHelper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
using HotChocolate.Types.Descriptors;

namespace HotChocolate.Types.Pagination;

internal static class NameHelper
{
public static string CreateConnectionName(string connectionName)
=> connectionName + "Connection";
public static string CreateConnectionName(
INamingConventions namingConventions,
string connectionName)
=> namingConventions.GetTypeName(connectionName + "Connection", TypeKind.Object);

public static string CreateEdgeName(string connectionName)
=> connectionName + "Edge";
public static string CreateEdgeName(
INamingConventions namingConventions,
string connectionName)
=> namingConventions.GetTypeName(connectionName + "Edge", TypeKind.Object);
}
Loading