Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed result formatting when non-null root field becomes null. #6844

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
Label = label;
Path = path;
HasNext = hasNext;
IsDataSet = isDataSet;
}

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

/// <inheritdoc />

Check warning on line 118 in src/HotChocolate/Core/src/Abstractions/Execution/QueryResult.cs

View check run for this annotation

Codecov / codecov/patch

src/HotChocolate/Core/src/Abstractions/Execution/QueryResult.cs#L118

Added line #L118 was not covered by tests
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 @@
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 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);

Check warning on line 297 in src/HotChocolate/Core/src/Execution/Serialization/JsonResultFormatter.cs

View check run for this annotation

Codecov / codecov/patch

src/HotChocolate/Core/src/Execution/Serialization/JsonResultFormatter.cs#L295-L297

Added lines #L295 - L297 were not covered by tests

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

Check warning on line 305 in src/HotChocolate/Core/src/Execution/Serialization/JsonResultFormatter.cs

View check run for this annotation

Codecov / codecov/patch

src/HotChocolate/Core/src/Execution/Serialization/JsonResultFormatter.cs#L304-L305

Added lines #L304 - L305 were not covered by tests
}
}

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
Loading