Skip to content

Commit 663000a

Browse files
authored
Merge union type into schem builder when using Merge (#477)
1 parent ea62821 commit 663000a

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/graphql/SDL/SdlReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ protected IType OutputType(GraphQLType typeDefinition)
201201

202202
if (definition == null)
203203
throw new InvalidOperationException(
204-
$"Could not find a input type definition '{typeName}'.");
204+
$"Could not find a output type definition '{typeName}'.");
205205

206206
switch (definition)
207207
{

src/graphql/Tools/MergeTool.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,21 @@ public static void Schema(SchemaBuilder target, ISchema right)
3838
foreach (var rightType in right.QueryTypes<ComplexType>())
3939
MergeComplexType(right, target, rightType);
4040

41+
// merge unions
42+
foreach (var rightType in right.QueryTypes<UnionType>())
43+
MergeUnionType(right, target, rightType);
44+
4145
// merge complex type field
4246
foreach (var rightType in right.QueryTypes<ComplexType>())
4347
MergeComplexTypeFields(right, target, rightType);
4448
}
4549

50+
private static void MergeUnionType(ISchema right, SchemaBuilder builder, UnionType rightType)
51+
{
52+
if (!builder.TryGetType<UnionType>(rightType.Name, out _))
53+
builder.Include(rightType);
54+
}
55+
4656
private static void MergeEnumType(ISchema right, SchemaBuilder builder, EnumType rightType)
4757
{
4858
// merge values if enum exists in both

tests/graphql.tests/Tools/MergeSchemasFacts.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,47 @@ type Query {
121121
var field = schema.GetField("Query", "currentColor");
122122
Assert.Equal(newEnumType, field.Type);
123123
}
124+
125+
[Fact]
126+
public void Merge_schema_with_new_union()
127+
{
128+
/* Given */
129+
var newTypes = new SchemaBuilder()
130+
.Sdl(@"
131+
type Red {
132+
}
133+
134+
type Green {
135+
}
136+
137+
type Orange {
138+
}
139+
140+
union Color = Red | Orange | Green
141+
142+
type Query {
143+
color: Color
144+
}
145+
")
146+
.Build();
147+
148+
var builder = new SchemaBuilder()
149+
.Sdl(@"
150+
type Query {
151+
}
152+
");
153+
154+
155+
/* When */
156+
var schema = builder.Merge(newTypes)
157+
.Build();
158+
159+
/* Then */
160+
var newUnionType = schema.GetNamedType<UnionType>("Color");
161+
Assert.NotNull(newUnionType);
162+
163+
var field = schema.GetField("Query", "color");
164+
Assert.Equal(newUnionType, field.Type);
165+
}
124166
}
125167
}

0 commit comments

Comments
 (0)