|
| 1 | +using Flowthru.Core.Data; |
| 2 | +using Flowthru.Core.Data.Storage; |
| 3 | +using StrawberryShake; |
| 4 | + |
| 5 | +namespace Flowthru.Extensions.GQL.Data; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Factory methods for creating collection GQL catalog entries. |
| 9 | +/// </summary> |
| 10 | +public static partial class GqlItemFactory |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Factory methods for <see cref="Item{T}"/> backed by a collection GraphQL query. |
| 14 | + /// </summary> |
| 15 | + public static class Enumerable |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Creates a non-paginated collection catalog entry from a StrawberryShake query. |
| 19 | + /// The server is expected to return all results in a single response. |
| 20 | + /// </summary> |
| 21 | + /// <typeparam name="TResult"> |
| 22 | + /// The StrawberryShake-generated result data type (e.g. <c>IGetUsersResult</c>). |
| 23 | + /// </typeparam> |
| 24 | + /// <typeparam name="T"> |
| 25 | + /// The target element type (e.g. <c>GetUsers_User</c>). |
| 26 | + /// </typeparam> |
| 27 | + /// <param name="label">Catalog entry label used in the pipeline DAG and validation messages.</param> |
| 28 | + /// <param name="queryFunc">Delegate that executes the StrawberryShake query operation.</param> |
| 29 | + /// <param name="selectData"> |
| 30 | + /// Projects the result data envelope to the collection of <typeparamref name="T"/>. |
| 31 | + /// Return <c>null</c> to yield empty (subject to <paramref name="allowEmptyData"/>). |
| 32 | + /// </param> |
| 33 | + /// <param name="allowEmptyData"> |
| 34 | + /// If <c>true</c>, an empty or null result collection is valid during pre-flight inspection. |
| 35 | + /// Defaults to <c>false</c>. |
| 36 | + /// </param> |
| 37 | + public static Item<IEnumerable<T>> Query<TResult, T>( |
| 38 | + string label, |
| 39 | + Func<CancellationToken, Task<IOperationResult<TResult>>> queryFunc, |
| 40 | + Func<TResult, IEnumerable<T>?> selectData, |
| 41 | + bool allowEmptyData = false |
| 42 | + ) |
| 43 | + where TResult : class |
| 44 | + where T : class |
| 45 | + { |
| 46 | + var adapter = new GqlEnumerableStorageAdapter<TResult, T>( |
| 47 | + label, |
| 48 | + queryFunc, |
| 49 | + selectData, |
| 50 | + allowEmptyData |
| 51 | + ); |
| 52 | + return new Item<IEnumerable<T>>(label, adapter); |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Creates a Relay cursor-paginated collection catalog entry. |
| 57 | + /// The adapter iterates pages until <c>HasNextPage</c> is <c>false</c>, yielding |
| 58 | + /// a flat <c>IEnumerable<T></c> to the pipeline. |
| 59 | + /// </summary> |
| 60 | + /// <typeparam name="TResult"> |
| 61 | + /// The StrawberryShake-generated result data type (e.g. <c>IGetSessionsResult</c>). |
| 62 | + /// </typeparam> |
| 63 | + /// <typeparam name="T"> |
| 64 | + /// The target element type (e.g. <c>GetSessions_Session</c>). |
| 65 | + /// </typeparam> |
| 66 | + /// <param name="label">Catalog entry label used in the pipeline DAG and validation messages.</param> |
| 67 | + /// <param name="pagedQueryFunc"> |
| 68 | + /// Delegate accepting <c>(cursor, pageSize, cancellationToken)</c>. Map <c>cursor</c> to |
| 69 | + /// the GraphQL <c>after</c> argument and <c>pageSize</c> to <c>first</c>. |
| 70 | + /// </param> |
| 71 | + /// <param name="pagination"> |
| 72 | + /// Relay pagination strategy created via <see cref="Pagination.Relay{TResult,T}"/>. |
| 73 | + /// </param> |
| 74 | + /// <param name="pageSize">Items to fetch per page. Defaults to 100.</param> |
| 75 | + /// <param name="allowEmptyData"> |
| 76 | + /// If <c>true</c>, an empty result set is valid during pre-flight inspection. |
| 77 | + /// Defaults to <c>false</c>. |
| 78 | + /// </param> |
| 79 | + public static Item<IEnumerable<T>> PagedQuery<TResult, T>( |
| 80 | + string label, |
| 81 | + Func<string?, int, CancellationToken, Task<IOperationResult<TResult>>> pagedQueryFunc, |
| 82 | + RelayPaginationStrategy<TResult, T> pagination, |
| 83 | + int pageSize = 100, |
| 84 | + bool allowEmptyData = false |
| 85 | + ) |
| 86 | + where TResult : class |
| 87 | + where T : class |
| 88 | + { |
| 89 | + var adapter = new GqlEnumerableStorageAdapter<TResult, T>( |
| 90 | + label, |
| 91 | + pagedQueryFunc, |
| 92 | + pagination, |
| 93 | + pageSize, |
| 94 | + allowEmptyData |
| 95 | + ); |
| 96 | + return new Item<IEnumerable<T>>(label, adapter); |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Creates an offset-paginated collection catalog entry. |
| 101 | + /// The adapter advances the offset until all items (per <c>getTotal</c>) are fetched |
| 102 | + /// or a page returns no items, yielding a flat <c>IEnumerable<T></c> to the pipeline. |
| 103 | + /// </summary> |
| 104 | + /// <typeparam name="TResult"> |
| 105 | + /// The StrawberryShake-generated result data type (e.g. <c>IGetProductsResult</c>). |
| 106 | + /// </typeparam> |
| 107 | + /// <typeparam name="T"> |
| 108 | + /// The target element type (e.g. <c>GetProducts_Product</c>). |
| 109 | + /// </typeparam> |
| 110 | + /// <param name="label">Catalog entry label used in the pipeline DAG and validation messages.</param> |
| 111 | + /// <param name="pagedQueryFunc"> |
| 112 | + /// Delegate accepting <c>(offset, limit, cancellationToken)</c>. Map directly to the |
| 113 | + /// GraphQL <c>skip</c>/<c>take</c> (or equivalent) arguments. |
| 114 | + /// </param> |
| 115 | + /// <param name="pagination"> |
| 116 | + /// Offset pagination strategy created via <see cref="Pagination.Offset{TResult,T}"/>. |
| 117 | + /// </param> |
| 118 | + /// <param name="pageSize">Items to fetch per page. Defaults to 100.</param> |
| 119 | + /// <param name="allowEmptyData"> |
| 120 | + /// If <c>true</c>, an empty result set is valid during pre-flight inspection. |
| 121 | + /// Defaults to <c>false</c>. |
| 122 | + /// </param> |
| 123 | + public static Item<IEnumerable<T>> PagedQuery<TResult, T>( |
| 124 | + string label, |
| 125 | + Func<int, int, CancellationToken, Task<IOperationResult<TResult>>> pagedQueryFunc, |
| 126 | + OffsetPaginationStrategy<TResult, T> pagination, |
| 127 | + int pageSize = 100, |
| 128 | + bool allowEmptyData = false |
| 129 | + ) |
| 130 | + where TResult : class |
| 131 | + where T : class |
| 132 | + { |
| 133 | + var adapter = new GqlEnumerableStorageAdapter<TResult, T>( |
| 134 | + label, |
| 135 | + pagedQueryFunc, |
| 136 | + pagination, |
| 137 | + pageSize, |
| 138 | + allowEmptyData |
| 139 | + ); |
| 140 | + return new Item<IEnumerable<T>>(label, adapter); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments