< Summary

Information
Class: NGql.Core.Query
Assembly: NGql.Core
File(s): /home/runner/work/NGql/NGql/src/Core/Query.cs
Line coverage
100%
Covered lines: 44
Uncovered lines: 0
Coverable lines: 44
Total lines: 161
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Block()100%11100%
.ctor()100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
get_Name()100%11100%
get_Alias()100%11100%
get_FieldsList()100%11100%
get_Arguments()100%11100%
get_Variables()100%11100%
Variable(...)100%11100%
Variable(...)100%11100%
Select(...)100%11100%
Select(...)100%11100%
Select(...)100%11100%
IncludeAtPath(...)100%11100%
Include(...)100%11100%
Include(...)100%11100%
Include(...)100%11100%
Where(...)100%11100%
Where(...)100%11100%
ToString()100%11100%
AppendTo(...)100%11100%
WriteTo(...)100%11100%
WriteUtf8(...)100%11100%
op_Implicit(...)100%11100%

File(s)

/home/runner/work/NGql/NGql/src/Core/Query.cs

#LineLine coverage
 1using System.Buffers;
 2using System.Text;
 3using NGql.Core.Abstractions;
 4using NGql.Core.Extensions;
 5
 6namespace 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>
 29public sealed class Query
 30{
 249331    public QueryBlock Block { get; }
 32
 633    public Query()
 34    {
 635        Block = new QueryBlock(string.Empty, string.Empty);
 636    }
 37
 37538    public Query(string name, params Variable[] variables)
 39    {
 37540        Block = new QueryBlock(name, "query", null, variables);
 37541    }
 42
 4843    public Query(string name, string? alias, params Variable[] variables)
 44    {
 4845        Block = new QueryBlock(name, "query", alias, variables);
 4846    }
 47
 48    /// <inheritdoc cref="QueryBlock.Name"/>
 2449    public string Name => Block.Name;
 50
 51    /// <inheritdoc cref="QueryBlock.Alias"/>
 2752    public string? Alias => Block.Alias;
 53
 54    /// <inheritdoc cref="QueryBlock.FieldsList"/>
 4555    public IEnumerable<object> FieldsList => Block.FieldsList;
 56
 57    /// <inheritdoc cref="QueryBlock.Arguments"/>
 5158    public IReadOnlyDictionary<string, object> Arguments => Block.Arguments;
 59
 60    /// <inheritdoc cref="QueryBlock.Variables"/>
 6061    public IEnumerable<Variable> Variables => Block.Variables;
 62
 63    /// <inheritdoc cref="QueryBlock.AddVariable(NGql.Core.Variable)"/>
 64    public Query Variable(Variable variable)
 65    {
 2166        Block.AddVariable(variable);
 2167        return this;
 68    }
 69
 70    /// <inheritdoc cref="QueryBlock.AddVariable(String,String)"/>
 71    public Query Variable(string name, string type)
 72    {
 3073        Block.AddVariable(name, type);
 3074        return this;
 75    }
 76
 77    /// <inheritdoc cref="QueryBlock.AddField(System.Collections.Generic.IEnumerable{object})"/>
 78    public Query Select(IEnumerable<object> selectList)
 79    {
 2180        Block.AddField(selectList);
 1881        return this;
 82    }
 83
 84    /// <inheritdoc cref="QueryBlock.AddField(string[])"/>
 85    public Query Select(params string[] selects)
 86    {
 25887        Block.AddField(selects);
 25888        return this;
 89    }
 90
 91    /// <inheritdoc cref="QueryBlock.AddField(QueryBlock)"/>
 92    public Query Select(Query subQuery)
 93    {
 6694        Block.AddField(subQuery.Block);
 6695        return this;
 96    }
 97
 98    /// <inheritdoc cref="QueryBlockObjectExtensions.IncludeAtPath{T}"/>
 99    public Query IncludeAtPath<T>(string path, string name, string? alias = null)
 100    {
 18101        Block.IncludeAtPath<T>(path, name, alias);
 18102        return this;
 103    }
 104
 105    /// <inheritdoc cref="QueryBlockObjectExtensions.Include{T}"/>
 106    public Query Include<T>(string name, string? alias = null)
 107    {
 18108        Block.Include<T>(name, alias);
 18109        return this;
 110    }
 111
 112    /// <inheritdoc cref="QueryBlockObjectExtensions.Include"/>
 113    public Query Include(object obj)
 114    {
 15115        Block.Include(obj);
 15116        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    {
 12128        var query = new Query(name, alias);
 12129        action.Invoke(query);
 12130        Block.AddField(query.Block);
 12131        return this;
 132    }
 133
 134    /// <inheritdoc cref="QueryBlock.AddArgument(string,object)"/>
 135    public Query Where(string key, object where)
 136    {
 204137        Block.AddArgument(key, where);
 183138        return this;
 139    }
 140
 141    /// <inheritdoc cref="QueryBlock.AddArgument(IReadOnlyDictionary&lt;string, object&gt;)"/>
 142    public Query Where(IReadOnlyDictionary<string, object> dict)
 143    {
 39144        Block.AddArgument(dict);
 27145        return this;
 146    }
 147
 148    /// <inheritdoc cref="QueryBlock.ToString()"/>
 138149    public override string ToString() => Block.ToString();
 150
 151    /// <inheritdoc cref="QueryBlock.AppendTo(System.Text.StringBuilder)"/>
 6152    public void AppendTo(StringBuilder builder) => Block.AppendTo(builder);
 153
 154    /// <inheritdoc cref="QueryBlock.WriteTo(System.IO.TextWriter)"/>
 1206155    public void WriteTo(TextWriter writer) => Block.WriteTo(writer);
 156
 157    /// <inheritdoc cref="QueryBlock.WriteUtf8(System.Buffers.IBufferWriter{System.Byte})"/>
 12158    public void WriteUtf8(IBufferWriter<byte> bufferWriter) => Block.WriteUtf8(bufferWriter);
 159
 108160    public static implicit operator string(Query query) => query.Block.ToString();
 161}