Skip to content

Commit 9d0741d

Browse files
authored
Bugfix: Treat operation name with empty string as null (graphql-dotnet#4078)
* Fix issue 4077 * Update * update
1 parent 927ff77 commit 9d0741d

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

src/GraphQL.Tests/Bugs/Bug1772.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public async Task DocumentExecuter_works_for_valid_operation(string? operationNa
2626
}
2727

2828
[Theory]
29-
[InlineData("")]
3029
[InlineData("thirdQuery")]
3130
[InlineData("query")]
3231
[InlineData("test")]

src/GraphQL.Tests/Bugs/Bug4078.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using GraphQL.Types;
2+
3+
namespace GraphQL.Tests.Bugs;
4+
5+
// https://github.com/graphql-dotnet/graphql-dotnet/pull/4078
6+
public class Bug4078
7+
{
8+
[Fact]
9+
public async Task empty_string_for_operation_name_is_treated_as_null()
10+
{
11+
var schema = new Schema { Query = new QueryGraphType() };
12+
13+
var response = await schema.ExecuteAsync(_ =>
14+
{
15+
// to trigger the bug, the document must contain a named operation,
16+
_.Query = "query abc { test }";
17+
// along with an empty string for the operation name
18+
_.OperationName = "";
19+
});
20+
21+
response.ShouldBeCrossPlatJson("""
22+
{
23+
"data": {
24+
"test": "abc"
25+
}
26+
}
27+
""");
28+
}
29+
30+
public class QueryGraphType : ObjectGraphType
31+
{
32+
public QueryGraphType()
33+
{
34+
Field<StringGraphType>("test").Resolve(ctx => "abc");
35+
}
36+
}
37+
}

src/GraphQL.Tests/Bugs/Issue4075.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Numerics;
22
using GraphQL.Types;
33

4-
namespace GraphQL.Tests.Bugs.Bug1046;
4+
namespace GraphQL.Tests.Bugs;
55

66
public class Issue4075
77
{

src/GraphQL/Execution/DocumentExecuter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ protected virtual ExecutionContext BuildExecutionContext(ExecutionOptions option
324324
/// <exception cref="NoOperationError">Thrown when the document does not include any operations.</exception>
325325
protected virtual GraphQLOperationDefinition GetOperation(string? operationName, GraphQLDocument document)
326326
{
327-
if (operationName == null)
327+
if (string.IsNullOrEmpty(operationName))
328328
{
329329
GraphQLOperationDefinition? match = null;
330330
foreach (var def in document.Definitions)
@@ -346,7 +346,7 @@ protected virtual GraphQLOperationDefinition GetOperation(string? operationName,
346346
return op;
347347
}
348348

349-
throw new InvalidOperationNameError(operationName);
349+
throw new InvalidOperationNameError(operationName!);
350350
}
351351

352352
/// <summary>

0 commit comments

Comments
 (0)