Skip to content

Commit 22f60d7

Browse files
authored
Rework of FederatedSchemaPrinter fix (graphql-dotnet#2751)
1 parent 4bd1f1d commit 22f60d7

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/GraphQL.Tests/Utilities/Federation/FederatedSchemaPrinterTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ namespace GraphQL.Tests.Utilities.Federation
66
{
77
public class FederatedSchemaPrinterTests
88
{
9-
[Fact]
10-
public void PrintObject_ReturnsEmptyString_GivenQueryTypeHasOnlyFederatedFields()
9+
[Theory]
10+
[InlineData(@"type X @key(fields: ""id"") { id: ID! }", "type Query")]
11+
[InlineData(@"schema { query: MyQuery } type MyQuery type X @key(fields: ""id"") { id: ID! }", "type MyQuery")]
12+
public void PrintObject_ReturnsEmptyString_GivenQueryTypeHasOnlyFederatedFields(string definitions, string expected)
1113
{
1214
// Arrange
13-
var schema = FederatedSchema.For(@"type X @key(fields: ""id"") { id: ID! }");
15+
var schema = FederatedSchema.For(definitions);
1416
SchemaPrinterOptions options = default;
1517

1618
schema.Initialize();
@@ -22,7 +24,7 @@ public void PrintObject_ReturnsEmptyString_GivenQueryTypeHasOnlyFederatedFields(
2224
string result = federatedSchemaPrinter.PrintObject(query);
2325

2426
// Assert
25-
Assert.Equal(string.Empty, result);
27+
Assert.Equal(expected, result);
2628
}
2729
}
2830
}

src/GraphQL/Utilities/Federation/FederatedSchemaPrinter.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ public override string PrintObject(IObjectGraphType type)
6666
{
6767
Schema?.Initialize();
6868

69-
// Do not return an empty query type: "Query { }" as it is not valid as part of the sdl.
70-
if (type != null && string.Equals(type.Name, "Query", StringComparison.Ordinal) && !type.Fields.Any(x => !IsFederatedType(x.ResolvedType!.GetNamedType().Name)))
71-
return string.Empty;
72-
7369
var isExtension = type!.IsExtensionType();
7470

7571
var interfaces = type!.ResolvedInterfaces.List.Select(x => x.Name).ToList();
@@ -82,7 +78,10 @@ public override string PrintObject(IObjectGraphType type)
8278

8379
var extended = isExtension ? "extend " : "";
8480

85-
return FormatDescription(type.Description) + "{1}type {2}{3}{4} {{{0}{5}{0}}}".ToFormat(Environment.NewLine, extended, type.Name, implementedInterfaces, federatedDirectives, PrintFields(type));
81+
if (type.Fields.Any(x => !IsFederatedType(x.ResolvedType!.GetNamedType().Name)))
82+
return FormatDescription(type.Description) + "{1}type {2}{3}{4} {{{0}{5}{0}}}".ToFormat(Environment.NewLine, extended, type.Name, implementedInterfaces, federatedDirectives, PrintFields(type));
83+
else
84+
return FormatDescription(type.Description) + "{0}type {1}{2}{3}".ToFormat(extended, type.Name, implementedInterfaces, federatedDirectives);
8685
}
8786

8887
public override string PrintInterface(IInterfaceGraphType type)

src/GraphQL/Utilities/SchemaPrinter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ public virtual string PrintObject(IObjectGraphType type)
215215
? " implements {0}".ToFormat(string.Join(delimiter, interfaces))
216216
: "";
217217

218-
return FormatDescription(type.Description) + "type {1}{2} {{{0}{3}{0}}}".ToFormat(Environment.NewLine, type.Name, implementedInterfaces, PrintFields(type));
218+
if (type.Fields.Count > 0)
219+
return FormatDescription(type.Description) + "type {1}{2} {{{0}{3}{0}}}".ToFormat(Environment.NewLine, type.Name, implementedInterfaces, PrintFields(type));
220+
else
221+
return FormatDescription(type.Description) + "type {0}{1}".ToFormat(type.Name, implementedInterfaces);
219222
}
220223

221224
public virtual string PrintInterface(IInterfaceGraphType type)

0 commit comments

Comments
 (0)