Skip to content

Commit

Permalink
Fixed result formatting when non-null root field becomes null. (#6844)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Jan 26, 2024
1 parent f3623c9 commit 62d14d2
Show file tree
Hide file tree
Showing 111 changed files with 912 additions and 720 deletions.
12 changes: 11 additions & 1 deletion src/CookieCrumble/src/CookieCrumble/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace CookieCrumble;

public sealed class Snapshot
public class Snapshot
{
private static readonly object _sync = new();
private static readonly UTF8Encoding _encoding = new();
Expand Down Expand Up @@ -49,6 +49,9 @@ public Snapshot(string? postFix = null, string? extension = null)

public static Snapshot Create(string? postFix = null, string? extension = null)
=> new(postFix, extension);

public static DisposableSnapshot Start(string? postFix = null, string? extension = null)
=> new(postFix, extension);

public static void Match(
object? value,
Expand Down Expand Up @@ -474,3 +477,10 @@ public SnapshotSegment(string? name, object? value, ISnapshotValueFormatter form
public ISnapshotValueFormatter Formatter { get; }
}
}

public sealed class DisposableSnapshot(string? postFix = null, string? extension = null)
: Snapshot(postFix, extension)
, IDisposable
{
public void Dispose() => Match();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"code": "AUTH_NO_DEFAULT_POLICY"
}
}
]
],
"data": null
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"code": "AUTH_NOT_AUTHORIZED"
}
}
]
],
"data": null
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"code": "AUTH_NOT_AUTHENTICATED"
}
}
]
],
"data": null
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"code": "AUTH_POLICY_NOT_FOUND"
}
}
]
],
"data": null
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"code": "AUTH_NOT_AUTHORIZED"
}
}
]
],
"data": null
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
]
}
],
"Body": "{\"data\":{\"field\":\"\"}}"
"Body": "{}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
]
}
],
"Body": "{\"data\":{\"field\":\"\"}}"
"Body": "{}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
]
}
],
"Body": "{\"data\":{\"field\":\"\"}}"
"Body": "{}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public interface IQueryResult : IExecutionResult
/// </summary>
/// <value></value>
bool? HasNext { get; }

/// <summary>
/// Specifies if data was explicitly set.
/// If <c>false</c> the data was not set (including null).
/// </summary>
bool IsDataSet { get; }

/// <summary>
/// Serializes this GraphQL result into a dictionary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ internal QueryResult(
string? label,
Path? path,
bool? hasNext,
Func<ValueTask>[] cleanupTasks)
Func<ValueTask>[] cleanupTasks,
bool isDataSet)
: base(cleanupTasks)
{
if (data is null &&
Expand All @@ -45,6 +46,7 @@ incremental is null &&
Label = label;
Path = path;
HasNext = hasNext;
IsDataSet = isDataSet;
}

/// <summary>
Expand Down Expand Up @@ -113,6 +115,9 @@ incremental is null &&
/// <inheritdoc />
public bool? HasNext { get; }

/// <inheritdoc />
public bool IsDataSet { get; }

/// <inheritdoc />
public IReadOnlyDictionary<string, object?> ToDictionary()
=> QueryResultHelper.ToDictionary(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public sealed class QueryResultBuilder : IQueryResultBuilder
private string? _label;
private Path? _path;
private bool? _hasNext;
private bool? _isDataSet;
private Func<ValueTask>[] _cleanupTasks = Array.Empty<Func<ValueTask>>();

public IQueryResultBuilder SetData(IReadOnlyDictionary<string, object?>? data)
{
_data = data;
_items = null;
_isDataSet = true;
return this;
}

Expand Down Expand Up @@ -176,7 +178,8 @@ public IQueryResult Create()
_label,
_path,
_hasNext,
_cleanupTasks);
_cleanupTasks,
_isDataSet ?? false);

public static QueryResultBuilder New() => new();

Expand Down Expand Up @@ -210,6 +213,7 @@ public static QueryResultBuilder FromResult(IQueryResult result)
builder._label = result.Label;
builder._path = result.Path;
builder._hasNext = result.HasNext;
builder._isDataSet = result.IsDataSet;

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public IQueryResult BuildResult()
throw new InvalidOperationException(Resources.ResultHelper_BuildResult_InvalidResult);
}

if (_errors.Count > 0)
if (_errors.Count > 1)
{
_errors.Sort(ErrorComparer.Default);
}
Expand Down Expand Up @@ -252,7 +252,8 @@ public IQueryResult BuildResult()
hasNext: _hasNext,
cleanupTasks: _cleanupTasks.Count == 0
? _emptyCleanupTasks
: _cleanupTasks.ToArray());
: _cleanupTasks.ToArray(),
isDataSet: true);

if (_data is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private void WriteResult(Utf8JsonWriter writer, IQueryResult result)
writer.WriteStartObject();

WriteErrors(writer, result.Errors);
WriteData(writer, result.Data);
WriteData(writer, result);
WriteItems(writer, result.Items);
WriteIncremental(writer, result.Incremental);
WriteExtensions(writer, result.Extensions);
Expand Down Expand Up @@ -281,20 +281,28 @@ private static void WriteHasNext(

private void WriteData(
Utf8JsonWriter writer,
IReadOnlyDictionary<string, object?>? data)
IQueryResult result)
{
if (data is not null)
if (!result.IsDataSet)
{
writer.WritePropertyName(Data);
return;
}

if (data is ObjectResult resultMap)
{
WriteObjectResult(writer, resultMap);
}
else
{
WriteDictionary(writer, data);
}
if (result.Data is null)
{
writer.WriteNull(Data);
return;
}

writer.WritePropertyName(Data);

if (result.Data is ObjectResult resultMap)
{
WriteObjectResult(writer, resultMap);
}
else
{
WriteDictionary(writer, result.Data);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"Extensions": null,
"Incremental": null,
"ContextData": null,
"HasNext": null
"HasNext": null,
"IsDataSet": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"Extensions": null,
"Incremental": null,
"ContextData": null,
"HasNext": null
"HasNext": null,
"IsDataSet": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@ public async Task Assert_UserState_Exists()
"originalValue": "abc"
}
}
]
],
"data": null
}
""");
}
Expand Down
Loading

0 comments on commit 62d14d2

Please sign in to comment.