Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static List<IDirective> GetPossibleFusionLookupDirectivesById(

public static void RemoveUnreferencedDefinitions(
this MutableSchemaDefinition schema,
ImmutableSortedSet<MutableSchemaDefinition> sourceSchemas)
IReadOnlySet<string> preservedTypeNames)
{
var touchedDefinitions = new HashSet<ITypeSystemMember>();
var backlog = new Stack<ITypeSystemMember>();
Expand All @@ -161,8 +161,6 @@ public static void RemoveUnreferencedDefinitions(
backlog.Push(schema.SubscriptionType);
}

var preservedTypeNames = GetPreservedTypeNames(sourceSchemas);

foreach (var typeName in preservedTypeNames)
{
if (schema.Types.TryGetType(typeName, out var inputType))
Expand Down Expand Up @@ -260,7 +258,8 @@ private static List<IDirective> GetFusionLookupDirectives(
/// Returns a list of type names for types that must be preserved in the merged schema
/// even if they are not directly referenced.
/// </summary>
private static HashSet<string> GetPreservedTypeNames(ImmutableSortedSet<MutableSchemaDefinition> sourceSchemas)
public static HashSet<string> GetPreservedTypeNames(
ImmutableSortedSet<MutableSchemaDefinition> sourceSchemas)
{
var preservedTypeNames = new HashSet<string>();

Expand Down
12 changes: 12 additions & 0 deletions src/HotChocolate/Fusion/src/Fusion.Composition/SchemaComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ public CompositionResult<MutableSchemaDefinition> Compose()
return enrichmentResult.Errors;
}

// Prune unreachable definitions from each source schema before validation, so types
// stripped by @excludeByTag (or otherwise unreferenced) are not validated or merged.
if (_schemaComposerOptions.Merger.RemoveUnreferencedDefinitions)
{
var preservedTypeNames = MutableSchemaDefinitionExtensions.GetPreservedTypeNames(schemas);

foreach (var schema in schemas)
{
schema.RemoveUnreferencedDefinitions(preservedTypeNames);
}
}

// Validate Source Schemas
var validationResult =
new SourceSchemaValidator(schemas, s_sourceSchemaRules, _log).Validate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public CompositionResult<MutableSchemaDefinition> Merge()
// Remove unreferenced definitions.
if (_options.RemoveUnreferencedDefinitions)
{
mergedSchema.RemoveUnreferencedDefinitions(_schemas);
mergedSchema.RemoveUnreferencedDefinitions(
MutableSchemaDefinitionExtensions.GetPreservedTypeNames(_schemas));
}

// Add Fusion definitions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,76 @@ type Product {
entry.Message);
}

[Fact]
public void Compose_OrphanedTypeAfterTagExclusion_DoesNotProduceShareableError()
{
// arrange
// Schema A's Product is only reachable via Mutation. When Mutation is removed by
// tag exclusion, Product becomes orphaned. Without pruning, Product.name would
// collide with Schema B's Product.name and trigger an InvalidFieldSharing error.
var schemaComposer = new SchemaComposer(
[
new SourceSchemaText(
"A",
"""
type Query {
book(id: ID!): Book @lookup
}

type Mutation @tag(name: "internal") {
createProduct(name: String!): Product
}

type Book {
id: ID!
title: String!
}

type Product {
id: ID!
name: String!
}

directive @tag(name: String!) repeatable on OBJECT
"""),
new SourceSchemaText(
"B",
"""
type Query {
productById(id: ID!): Product @lookup
}

type Product {
id: ID!
name: String!
}
""")
],
new SchemaComposerOptions
{
Merger = { AddFusionDefinitions = false },
SourceSchemas =
{
["A"] = new SourceSchemaOptions
{
Preprocessor = new SourceSchemaPreprocessorOptions
{
ExcludeByTag = ["internal"]
}
}
}
},
new CompositionLog());

// act
var result = schemaComposer.Compose();

// assert
Assert.True(result.IsSuccess);
Assert.False(result.Value.Types.ContainsName("Mutation"));
Assert.True(result.Value.Types.ContainsName("Product"));
}

[Fact]
public void Compose_WithExtensions_AppliesExtensions()
{
Expand Down
Loading