11using System ;
2+ using System . Diagnostics ;
3+ using static HotChocolate . Types . Properties . CursorResources ;
24
35namespace HotChocolate . Types . Pagination ;
46
7+ /// <summary>
8+ /// Represents an edge in a connection.
9+ /// </summary>
10+ /// <typeparam name="T"></typeparam>
511public 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+ }
0 commit comments