| | | 1 | | using NGql.Core.Abstractions; |
| | | 2 | | |
| | | 3 | | namespace NGql.Core; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Builds a GraphQL <c>mutation</c> operation. Use <see cref="Select(NGql.Core.Query)"/> to |
| | | 7 | | /// embed a sub-<see cref="Query"/> that carries arguments via <see cref="Query.Where(string, object)"/>, |
| | | 8 | | /// or <see cref="Select(string[])"/> for plain field names. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// <para> |
| | | 12 | | /// <b>New code should prefer <see cref="Builders.QueryBuilder.CreateMutationBuilder(string)"/>.</b> |
| | | 13 | | /// The fluent <see cref="Builders.QueryBuilder"/> surface is the recommended way to author both |
| | | 14 | | /// queries and mutations in NGql 2.x and beyond — it offers a richer API (<c>Include</c>, |
| | | 15 | | /// <c>WithMetadata</c>, sub-field lambdas, <c>PreservationBuilder</c> support) and avoids the |
| | | 16 | | /// classic <see cref="Query"/>+<c>Where</c> idiom required by <see cref="Mutation"/> for |
| | | 17 | | /// argument-bearing root fields. |
| | | 18 | | /// </para> |
| | | 19 | | /// <para> |
| | | 20 | | /// <see cref="Mutation"/> remains supported for backwards compatibility with NGql 1.x call sites |
| | | 21 | | /// and continues to render the same GraphQL output. There is currently no removal timeline. |
| | | 22 | | /// </para> |
| | | 23 | | /// </remarks> |
| | | 24 | | /// <param name="name">Operation name (rendered as <c>mutation Name(...)</c>).</param> |
| | | 25 | | /// <param name="variables">Operation variables; their <c>$name:Type</c> declarations appear in the operation signature. |
| | 33 | 26 | | public sealed class Mutation(string name, params Variable[] variables) |
| | | 27 | | { |
| | 33 | 28 | | private readonly QueryBlock _block = new(name, "mutation", variables: variables); |
| | | 29 | | |
| | | 30 | | /// <inheritdoc cref="QueryBlock.Name"/> |
| | 3 | 31 | | public string Name => _block.Name; |
| | | 32 | | |
| | | 33 | | /// <inheritdoc cref="QueryBlock.FieldsList"/> |
| | 6 | 34 | | public IEnumerable<object> FieldsList => _block.FieldsList; |
| | | 35 | | |
| | | 36 | | /// <inheritdoc cref="QueryBlock.Variables"/> |
| | 6 | 37 | | public IEnumerable<Variable> Variables => _block.Variables; |
| | | 38 | | |
| | | 39 | | /// <inheritdoc cref="QueryBlock.AddVariable(NGql.Core.Variable)"/> |
| | | 40 | | public Mutation Variable(Variable variable) |
| | | 41 | | { |
| | 9 | 42 | | _block.AddVariable(variable); |
| | 9 | 43 | | return this; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc cref="QueryBlock.AddVariable(string,string)"/> |
| | | 47 | | public Mutation Variable(string name, string type) |
| | | 48 | | { |
| | 3 | 49 | | _block.AddVariable(name, type); |
| | 3 | 50 | | return this; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc cref="QueryBlock.AddField(System.Collections.Generic.IEnumerable{object})"/> |
| | | 54 | | public Mutation Select(IEnumerable<object> selectList) |
| | | 55 | | { |
| | 3 | 56 | | _block.AddField(selectList); |
| | 3 | 57 | | return this; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc cref="QueryBlock.AddField(string[])"/> |
| | | 61 | | public Mutation Select(params string[] selects) |
| | | 62 | | { |
| | 15 | 63 | | _block.AddField(selects); |
| | 15 | 64 | | return this; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc cref="QueryBlock.AddField(QueryBlock)"/> |
| | | 68 | | public Mutation Select(Query subQuery) |
| | | 69 | | { |
| | 15 | 70 | | _block.AddField(subQuery.Block); |
| | 15 | 71 | | return this; |
| | | 72 | | } |
| | | 73 | | |
| | 9 | 74 | | public override string ToString() => _block.ToString(); |
| | 15 | 75 | | public static implicit operator string(Mutation mutation) => mutation._block.ToString(); |
| | | 76 | | } |