Skip to content

Commit 4c00c2e

Browse files
committed
Added a constructor for lazy cursors to the edge.
1 parent 99dc6f0 commit 4c00c2e

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed
Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
using System;
2+
using System.Diagnostics;
3+
using static HotChocolate.Types.Properties.CursorResources;
24

35
namespace HotChocolate.Types.Pagination;
46

7+
/// <summary>
8+
/// Represents an edge in a connection.
9+
/// </summary>
10+
/// <typeparam name="T"></typeparam>
511
public class Edge<T> : IEdge
612
{
13+
private readonly Func<T, string>? _resolveCursor;
14+
private string? _cursor;
15+
16+
/// <summary>
17+
/// Initializes a new instance of <see cref="Edge{T}" />.
18+
/// </summary>
19+
/// <param name="node">
20+
/// The node that the edge will wrap.
21+
/// </param>
22+
/// <param name="cursor">
23+
/// The cursor which identifies the <paramref name="node" /> in the current data set.
24+
/// </param>
25+
/// <exception cref="ArgumentNullException">
26+
/// <paramref name="cursor" /> is <see langword="null" /> or empty.
27+
/// </exception>
728
public Edge(T node, string cursor)
829
{
930
if (string.IsNullOrEmpty(cursor))
@@ -12,12 +33,57 @@ public Edge(T node, string cursor)
1233
}
1334

1435
Node = node;
15-
Cursor = cursor;
36+
_cursor = cursor;
37+
}
38+
39+
/// <summary>
40+
/// Initializes a new instance of <see cref="Edge{T}" />.
41+
/// </summary>
42+
/// <param name="node">
43+
/// The node that the edge will wrap.
44+
/// </param>
45+
/// <param name="resolveCursor">
46+
/// A delegate that resolves the cursor which identifies the <paramref name="node" /> in the
47+
/// </param>
48+
/// <exception cref="ArgumentNullException">
49+
/// <paramref name="resolveCursor" /> is <see langword="null" />.
50+
/// </exception>
51+
public Edge(T node, Func<T, string> resolveCursor)
52+
{
53+
Node = node;
54+
_resolveCursor = resolveCursor ??
55+
throw new ArgumentNullException(nameof(resolveCursor));
1656
}
1757

58+
/// <summary>
59+
/// Gets the node.
60+
/// </summary>
1861
public T Node { get; }
1962

2063
object? IEdge.Node => Node;
2164

22-
public string Cursor { get; }
23-
}
65+
/// <summary>
66+
/// Gets the cursor which identifies the <see cref="Node" /> in the current data set.
67+
/// </summary>
68+
/// <exception cref="InvalidOperationException">
69+
/// The edge was initialized with a cursor resolver and the resolver returned <see langword="null" />.
70+
/// </exception>
71+
public string Cursor
72+
{
73+
get
74+
{
75+
if (_cursor is null)
76+
{
77+
if (_resolveCursor is null)
78+
{
79+
throw new InvalidOperationException(Edge_Cursor_CursorAndResolverNull);
80+
}
81+
82+
_cursor = _resolveCursor(Node);
83+
Debug.Assert(_cursor is not null, "The edge's cursor resolver returned null.");
84+
}
85+
86+
return _cursor;
87+
}
88+
}
89+
}

src/HotChocolate/Core/src/Types.CursorPagination/IEdge.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace HotChocolate.Types.Pagination;
22

3+
/// <summary>
4+
/// Represents an edge in a connection.
5+
/// </summary>
36
public interface IEdge
47
{
58
/// <summary>

src/HotChocolate/Core/src/Types.CursorPagination/Properties/CursorResources.Designer.cs

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/HotChocolate/Core/src/Types.CursorPagination/Properties/CursorResources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@
4242
<data name="ThrowHelper_PagingHandler_NoBoundariesSet" xml:space="preserve">
4343
<value>You must provide a `first` or `last` value to properly paginate the `{0}`.</value>
4444
</data>
45+
<data name="Edge_Cursor_CursorAndResolverNull" xml:space="preserve">
46+
<value>The edge state is invalid and has no cursor.</value>
47+
</data>
4548
</root>

0 commit comments

Comments
 (0)