| | | 1 | | using NGql.Core.Abstractions; |
| | | 2 | | using NGql.Core.Extensions; |
| | | 3 | | |
| | | 4 | | namespace NGql.Core; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Classic-API query builder. Compose with <c>.Where(...)</c> for arguments and |
| | | 8 | | /// <c>.Select(...)</c> for fields; nest queries by passing a <see cref="Query"/> to a parent's |
| | | 9 | | /// <c>Select</c>. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// <para> |
| | | 13 | | /// <b>New code should prefer <see cref="NGql.Core.Builders.QueryBuilder.CreateDefaultBuilder(string)"/>.</b> |
| | | 14 | | /// The fluent <see cref="NGql.Core.Builders.QueryBuilder"/> surface is the recommended way to author |
| | | 15 | | /// queries in NGql 2.x and beyond — it offers a richer API (<c>Include</c>, <c>WithMetadata</c>, |
| | | 16 | | /// sub-field lambdas, <c>PreservationBuilder</c> support, dot-path field composition) over the |
| | | 17 | | /// classic <c>Where</c> / <c>Select</c> idiom. |
| | | 18 | | /// </para> |
| | | 19 | | /// <para> |
| | | 20 | | /// <see cref="Query"/> remains supported for backwards compatibility with NGql 1.x call sites and |
| | | 21 | | /// continues to render the same GraphQL output. It is also the only way to attach arguments to |
| | | 22 | | /// the root field of a <see cref="Mutation"/> built via the classic API; new mutation code should |
| | | 23 | | /// use <see cref="NGql.Core.Builders.QueryBuilder.CreateMutationBuilder(string)"/> instead, which |
| | | 24 | | /// avoids that round-trip. There is currently no removal timeline for <see cref="Query"/>. |
| | | 25 | | /// </para> |
| | | 26 | | /// </remarks> |
| | | 27 | | public sealed class Query |
| | | 28 | | { |
| | 870 | 29 | | public QueryBlock Block { get; } |
| | | 30 | | |
| | 6 | 31 | | public Query() |
| | | 32 | | { |
| | 6 | 33 | | Block = new QueryBlock(string.Empty, string.Empty); |
| | 6 | 34 | | } |
| | | 35 | | |
| | 228 | 36 | | public Query(string name, params Variable[] variables) |
| | | 37 | | { |
| | 228 | 38 | | Block = new QueryBlock(name, "query", null, variables); |
| | 228 | 39 | | } |
| | | 40 | | |
| | 48 | 41 | | public Query(string name, string? alias, params Variable[] variables) |
| | | 42 | | { |
| | 48 | 43 | | Block = new QueryBlock(name, "query", alias, variables); |
| | 48 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc cref="QueryBlock.Name"/> |
| | 24 | 47 | | public string Name => Block.Name; |
| | | 48 | | |
| | | 49 | | /// <inheritdoc cref="QueryBlock.Alias"/> |
| | 27 | 50 | | public string? Alias => Block.Alias; |
| | | 51 | | |
| | | 52 | | /// <inheritdoc cref="QueryBlock.FieldsList"/> |
| | 45 | 53 | | public IEnumerable<object> FieldsList => Block.FieldsList; |
| | | 54 | | |
| | | 55 | | /// <inheritdoc cref="QueryBlock.Arguments"/> |
| | 51 | 56 | | public IReadOnlyDictionary<string, object> Arguments => Block.Arguments; |
| | | 57 | | |
| | | 58 | | /// <inheritdoc cref="QueryBlock.Variables"/> |
| | 60 | 59 | | public IEnumerable<Variable> Variables => Block.Variables; |
| | | 60 | | |
| | | 61 | | /// <inheritdoc cref="QueryBlock.AddVariable(NGql.Core.Variable)"/> |
| | | 62 | | public Query Variable(Variable variable) |
| | | 63 | | { |
| | 21 | 64 | | Block.AddVariable(variable); |
| | 21 | 65 | | return this; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc cref="QueryBlock.AddVariable(String,String)"/> |
| | | 69 | | public Query Variable(string name, string type) |
| | | 70 | | { |
| | 27 | 71 | | Block.AddVariable(name, type); |
| | 27 | 72 | | return this; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc cref="QueryBlock.AddField(System.Collections.Generic.IEnumerable{object})"/> |
| | | 76 | | public Query Select(IEnumerable<object> selectList) |
| | | 77 | | { |
| | 21 | 78 | | Block.AddField(selectList); |
| | 18 | 79 | | return this; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc cref="QueryBlock.AddField(string[])"/> |
| | | 83 | | public Query Select(params string[] selects) |
| | | 84 | | { |
| | 132 | 85 | | Block.AddField(selects); |
| | 132 | 86 | | return this; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <inheritdoc cref="QueryBlock.AddField(QueryBlock)"/> |
| | | 90 | | public Query Select(Query subQuery) |
| | | 91 | | { |
| | 39 | 92 | | Block.AddField(subQuery.Block); |
| | 39 | 93 | | return this; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <inheritdoc cref="QueryBlockObjectExtensions.IncludeAtPath{T}"/> |
| | | 97 | | public Query IncludeAtPath<T>(string path, string name, string? alias = null) |
| | | 98 | | { |
| | 18 | 99 | | Block.IncludeAtPath<T>(path, name, alias); |
| | 18 | 100 | | return this; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <inheritdoc cref="QueryBlockObjectExtensions.Include{T}"/> |
| | | 104 | | public Query Include<T>(string name, string? alias = null) |
| | | 105 | | { |
| | 6 | 106 | | Block.Include<T>(name, alias); |
| | 6 | 107 | | return this; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc cref="QueryBlockObjectExtensions.Include"/> |
| | | 111 | | public Query Include(object obj) |
| | | 112 | | { |
| | 9 | 113 | | Block.Include(obj); |
| | 9 | 114 | | return this; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Adds the given sub query to the <see cref="QueryBlock.FieldsList"/> part of the query. |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="name">A sub-query name.</param> |
| | | 121 | | /// <param name="alias">A sub-query alias.</param> |
| | | 122 | | /// <param name="action">Action to build subquery.</param> |
| | | 123 | | /// <returns>Query</returns> |
| | | 124 | | public Query Include(string name, Action<Query> action, string? alias = null) |
| | | 125 | | { |
| | 12 | 126 | | var query = new Query(name, alias); |
| | 12 | 127 | | action.Invoke(query); |
| | 12 | 128 | | Block.AddField(query.Block); |
| | 12 | 129 | | return this; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <inheritdoc cref="QueryBlock.AddArgument(string,object)"/> |
| | | 133 | | public Query Where(string key, object where) |
| | | 134 | | { |
| | 108 | 135 | | Block.AddArgument(key, where); |
| | 108 | 136 | | return this; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <inheritdoc cref="QueryBlock.AddArgument(IReadOnlyDictionary<string, object>)"/> |
| | | 140 | | public Query Where(IReadOnlyDictionary<string, object> dict) |
| | | 141 | | { |
| | 27 | 142 | | Block.AddArgument(dict); |
| | 27 | 143 | | return this; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <inheritdoc cref="QueryBlock.ToString()"/> |
| | 75 | 147 | | public override string ToString() => Block.ToString(); |
| | 99 | 148 | | public static implicit operator string(Query query) => query.Block.ToString(); |
| | | 149 | | } |