| | | 1 | | using System.Text.Json.Serialization; |
| | | 2 | | using NGql.Core.Builders; |
| | | 3 | | |
| | | 4 | | namespace NGql.Core.Abstractions; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents a query definition. |
| | | 8 | | /// </summary> |
| | 9543 | 9 | | public sealed record QueryDefinition(string Name, string Description = "") |
| | | 10 | | { |
| | | 11 | | internal Dictionary<string, FieldDefinition>? _fields; |
| | | 12 | | internal SortedSet<Variable>? _variables; |
| | | 13 | | internal Dictionary<string, object?>? _metadata; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// The name of the query. |
| | | 17 | | /// </summary> |
| | | 18 | | [JsonPropertyName("name")] |
| | 51468 | 19 | | public string Name { get; init; } = Name; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// The description of the query. |
| | | 23 | | /// </summary> |
| | | 24 | | [JsonPropertyName("description")] |
| | 9543 | 25 | | public string Description { get; init; } = Description; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The collection of fields related to <see cref="QueryDefinition"/>. |
| | | 29 | | /// </summary> |
| | | 30 | | [JsonPropertyName("fields")] |
| | | 31 | | public Dictionary<string, FieldDefinition> Fields |
| | | 32 | | { |
| | 75156 | 33 | | get => _fields ??= new(StringComparer.OrdinalIgnoreCase); |
| | 3 | 34 | | internal set => _fields = value; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// The collection of variables related to fields or arguments. |
| | | 39 | | /// </summary> |
| | | 40 | | [JsonIgnore] |
| | | 41 | | public SortedSet<Variable> Variables |
| | | 42 | | { |
| | 7545 | 43 | | get => _variables ??= new(); |
| | 3 | 44 | | internal set => _variables = value; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Metadata associated with the query definition. |
| | | 49 | | /// This can include additional information such as descriptions, tags, or any other relevant data. |
| | | 50 | | /// |
| | | 51 | | /// Not used during query text generation but can be useful for documentation or introspection purposes. |
| | | 52 | | /// </summary> |
| | | 53 | | [JsonPropertyName("metadata")] |
| | | 54 | | public Dictionary<string, object?> Metadata |
| | | 55 | | { |
| | 228 | 56 | | get => _metadata ??= []; |
| | 69 | 57 | | set => _metadata = value; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Merging strategy for this query definition. |
| | | 62 | | /// </summary> |
| | | 63 | | [JsonPropertyName("mergingStrategy")] |
| | 1635 | 64 | | public MergingStrategy MergingStrategy { get; set; } = MergingStrategy.MergeByDefault; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// The kind of root operation rendered by this definition (query or mutation). |
| | | 68 | | /// Selected by the factory on <see cref="QueryBuilder"/>; consumers do not set this directly. |
| | | 69 | | /// </summary> |
| | | 70 | | [JsonIgnore] |
| | 8124 | 71 | | internal OperationType OperationType { get; set; } = OperationType.Query; |
| | | 72 | | |
| | | 73 | | /// <inheritdoc cref="QueryBlock.ToString()"/> |
| | | 74 | | public override string ToString() |
| | | 75 | | { |
| | 8106 | 76 | | var builder = QueryTextBuilder.GetFromPool(); |
| | | 77 | | try |
| | | 78 | | { |
| | 8106 | 79 | | return builder.Build(this); |
| | | 80 | | } |
| | | 81 | | finally |
| | | 82 | | { |
| | 8106 | 83 | | QueryTextBuilder.ReturnToPool(builder); |
| | 8106 | 84 | | } |
| | 8106 | 85 | | } |
| | | 86 | | |
| | 3 | 87 | | public static implicit operator string(QueryDefinition query) => query.ToString(); |
| | | 88 | | } |