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