< 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: 41
Uncovered lines: 0
Coverable lines: 41
Total lines: 149
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%
op_Implicit(...)100%11100%

File(s)

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

#LineLine coverage
 1using NGql.Core.Abstractions;
 2using NGql.Core.Extensions;
 3
 4namespace 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>
 27public sealed class Query
 28{
 87029    public QueryBlock Block { get; }
 30
 631    public Query()
 32    {
 633        Block = new QueryBlock(string.Empty, string.Empty);
 634    }
 35
 22836    public Query(string name, params Variable[] variables)
 37    {
 22838        Block = new QueryBlock(name, "query", null, variables);
 22839    }
 40
 4841    public Query(string name, string? alias, params Variable[] variables)
 42    {
 4843        Block = new QueryBlock(name, "query", alias, variables);
 4844    }
 45
 46    /// <inheritdoc cref="QueryBlock.Name"/>
 2447    public string Name => Block.Name;
 48
 49    /// <inheritdoc cref="QueryBlock.Alias"/>
 2750    public string? Alias => Block.Alias;
 51
 52    /// <inheritdoc cref="QueryBlock.FieldsList"/>
 4553    public IEnumerable<object> FieldsList => Block.FieldsList;
 54
 55    /// <inheritdoc cref="QueryBlock.Arguments"/>
 5156    public IReadOnlyDictionary<string, object> Arguments => Block.Arguments;
 57
 58    /// <inheritdoc cref="QueryBlock.Variables"/>
 6059    public IEnumerable<Variable> Variables => Block.Variables;
 60
 61    /// <inheritdoc cref="QueryBlock.AddVariable(NGql.Core.Variable)"/>
 62    public Query Variable(Variable variable)
 63    {
 2164        Block.AddVariable(variable);
 2165        return this;
 66    }
 67
 68    /// <inheritdoc cref="QueryBlock.AddVariable(String,String)"/>
 69    public Query Variable(string name, string type)
 70    {
 2771        Block.AddVariable(name, type);
 2772        return this;
 73    }
 74
 75    /// <inheritdoc cref="QueryBlock.AddField(System.Collections.Generic.IEnumerable{object})"/>
 76    public Query Select(IEnumerable<object> selectList)
 77    {
 2178        Block.AddField(selectList);
 1879        return this;
 80    }
 81
 82    /// <inheritdoc cref="QueryBlock.AddField(string[])"/>
 83    public Query Select(params string[] selects)
 84    {
 13285        Block.AddField(selects);
 13286        return this;
 87    }
 88
 89    /// <inheritdoc cref="QueryBlock.AddField(QueryBlock)"/>
 90    public Query Select(Query subQuery)
 91    {
 3992        Block.AddField(subQuery.Block);
 3993        return this;
 94    }
 95
 96    /// <inheritdoc cref="QueryBlockObjectExtensions.IncludeAtPath{T}"/>
 97    public Query IncludeAtPath<T>(string path, string name, string? alias = null)
 98    {
 1899        Block.IncludeAtPath<T>(path, name, alias);
 18100        return this;
 101    }
 102
 103    /// <inheritdoc cref="QueryBlockObjectExtensions.Include{T}"/>
 104    public Query Include<T>(string name, string? alias = null)
 105    {
 6106        Block.Include<T>(name, alias);
 6107        return this;
 108    }
 109
 110    /// <inheritdoc cref="QueryBlockObjectExtensions.Include"/>
 111    public Query Include(object obj)
 112    {
 9113        Block.Include(obj);
 9114        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    {
 12126        var query = new Query(name, alias);
 12127        action.Invoke(query);
 12128        Block.AddField(query.Block);
 12129        return this;
 130    }
 131
 132    /// <inheritdoc cref="QueryBlock.AddArgument(string,object)"/>
 133    public Query Where(string key, object where)
 134    {
 108135        Block.AddArgument(key, where);
 108136        return this;
 137    }
 138
 139    /// <inheritdoc cref="QueryBlock.AddArgument(IReadOnlyDictionary&lt;string, object&gt;)"/>
 140    public Query Where(IReadOnlyDictionary<string, object> dict)
 141    {
 27142        Block.AddArgument(dict);
 27143        return this;
 144    }
 145
 146    /// <inheritdoc cref="QueryBlock.ToString()"/>
 75147    public override string ToString() => Block.ToString();
 99148    public static implicit operator string(Query query) => query.Block.ToString();
 149}