Skip to content

Commit e9ddd7d

Browse files
authored
[Fusion] Added structure for post-merge validation (#7969)
1 parent 12587ea commit e9ddd7d

File tree

84 files changed

+229
-96
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+229
-96
lines changed

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/Errors/ErrorHelper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public static CompositionError SourceSchemaParsingFailed()
1010
public static CompositionError SourceSchemaValidationFailed()
1111
=> new(ErrorHelper_SourceSchemaValidationFailed);
1212

13+
public static CompositionError PostMergeValidationFailed()
14+
=> new(ErrorHelper_PostMergeValidationFailed);
15+
1316
public static CompositionError PreMergeValidationFailed()
1417
=> new(ErrorHelper_PreMergeValidationFailed);
1518
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace HotChocolate.Fusion.Events.Contracts;
2+
3+
internal interface IEvent;

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/Events/IEventHandler.cs renamed to src/HotChocolate/Fusion-vnext/src/Fusion.Composition/Events/Contracts/IEventHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace HotChocolate.Fusion.Events;
1+
namespace HotChocolate.Fusion.Events.Contracts;
22

33
internal interface IEventHandler<in TEvent> where TEvent : IEvent
44
{

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Events.cs renamed to src/HotChocolate/Fusion-vnext/src/Fusion.Composition/Events/GroupEvents.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.Collections.Immutable;
2-
using HotChocolate.Fusion.Events;
2+
using HotChocolate.Fusion.Events.Contracts;
33
using HotChocolate.Fusion.Info;
44

5-
namespace HotChocolate.Fusion.PreMergeValidation;
5+
namespace HotChocolate.Fusion.Events;
66

77
internal record EnumTypeGroupEvent(
88
string TypeName,

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/Events/IEvent.cs

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/HotChocolate/Fusion-vnext/src/Fusion.Composition/SourceSchemaValidation/Events.cs renamed to src/HotChocolate/Fusion-vnext/src/Fusion.Composition/Events/SchemaEvents.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System.Collections.Immutable;
2-
using HotChocolate.Fusion.Events;
2+
using HotChocolate.Fusion.Events.Contracts;
33
using HotChocolate.Language;
44
using HotChocolate.Skimmed;
55

6-
namespace HotChocolate.Fusion.SourceSchemaValidation;
6+
namespace HotChocolate.Fusion.Events;
77

88
internal record DirectiveArgumentEvent(
99
InputFieldDefinition Argument,
@@ -16,6 +16,19 @@ internal record FieldArgumentEvent(
1616
INamedTypeDefinition Type,
1717
SchemaDefinition Schema) : IEvent;
1818

19+
internal record InputFieldEvent(
20+
InputFieldDefinition InputField,
21+
InputObjectTypeDefinition InputType,
22+
SchemaDefinition Schema) : IEvent;
23+
24+
internal record InputTypeEvent(
25+
InputObjectTypeDefinition InputType,
26+
SchemaDefinition Schema) : IEvent;
27+
28+
internal record InterfaceTypeEvent(
29+
InterfaceTypeDefinition InterfaceType,
30+
SchemaDefinition Schema) : IEvent;
31+
1932
internal record KeyFieldEvent(
2033
Directive KeyDirective,
2134
ComplexTypeDefinition EntityType,
@@ -47,6 +60,10 @@ internal record KeyFieldsInvalidTypeEvent(
4760
ComplexTypeDefinition EntityType,
4861
SchemaDefinition Schema) : IEvent;
4962

63+
internal record ObjectTypeEvent(
64+
ObjectTypeDefinition ObjectType,
65+
SchemaDefinition Schema) : IEvent;
66+
5067
internal record OutputFieldEvent(
5168
OutputFieldDefinition Field,
5269
INamedTypeDefinition Type,
Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,83 @@
11
using System.Collections.Immutable;
2+
using HotChocolate.Fusion.Errors;
3+
using HotChocolate.Fusion.Events;
4+
using HotChocolate.Fusion.Events.Contracts;
5+
using HotChocolate.Fusion.Logging.Contracts;
26
using HotChocolate.Fusion.Results;
37
using HotChocolate.Skimmed;
48

59
namespace HotChocolate.Fusion;
610

7-
#pragma warning disable CS9113 // Parameter is unread.
811
internal sealed class PostMergeValidator(
912
SchemaDefinition mergedSchema,
10-
ImmutableArray<object> rules)
11-
#pragma warning restore CS9113 // Parameter is unread.
13+
ImmutableArray<object> rules,
14+
ImmutableSortedSet<SchemaDefinition> sourceSchemas,
15+
ICompositionLog log)
1216
{
1317
public CompositionResult Validate()
1418
{
15-
// FIXME: Implement.
16-
return CompositionResult.Success();
19+
PublishEvents();
20+
21+
return log.HasErrors
22+
? ErrorHelper.PostMergeValidationFailed()
23+
: CompositionResult.Success();
24+
}
25+
26+
private void PublishEvents()
27+
{
28+
var context = new CompositionContext(sourceSchemas, log);
29+
30+
PublishEvent(new SchemaEvent(mergedSchema), context);
31+
32+
foreach (var type in mergedSchema.Types)
33+
{
34+
switch (type)
35+
{
36+
case InputObjectTypeDefinition inputType:
37+
PublishEvent(new InputTypeEvent(inputType, mergedSchema), context);
38+
39+
foreach (var field in inputType.Fields)
40+
{
41+
PublishEvent(new InputFieldEvent(field, inputType, mergedSchema), context);
42+
}
43+
44+
break;
45+
46+
case InterfaceTypeDefinition interfaceType:
47+
PublishEvent(new InterfaceTypeEvent(interfaceType, mergedSchema), context);
48+
break;
49+
50+
case ObjectTypeDefinition objectType:
51+
PublishEvent(new ObjectTypeEvent(objectType, mergedSchema), context);
52+
break;
53+
}
54+
55+
if (type is ComplexTypeDefinition complexType)
56+
{
57+
foreach (var field in complexType.Fields)
58+
{
59+
PublishEvent(new OutputFieldEvent(field, complexType, mergedSchema), context);
60+
61+
foreach (var argument in field.Arguments)
62+
{
63+
PublishEvent(
64+
new FieldArgumentEvent(argument, field, complexType, mergedSchema),
65+
context);
66+
}
67+
}
68+
}
69+
}
70+
}
71+
72+
private void PublishEvent<TEvent>(TEvent @event, CompositionContext context)
73+
where TEvent : IEvent
74+
{
75+
foreach (var rule in rules)
76+
{
77+
if (rule is IEventHandler<TEvent> handler)
78+
{
79+
handler.Handle(@event, context);
80+
}
81+
}
1782
}
1883
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System.Collections.Immutable;
22
using HotChocolate.Fusion.Events;
3+
using HotChocolate.Fusion.Events.Contracts;
34
using HotChocolate.Fusion.Extensions;
45
using static HotChocolate.Fusion.Logging.LogEntryHelper;
56

6-
namespace HotChocolate.Fusion.PreMergeValidation.Rules;
7+
namespace HotChocolate.Fusion.PreMergeValidationRules;
78

89
/// <summary>
910
/// <para>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System.Collections.Immutable;
22
using HotChocolate.Fusion.Events;
3+
using HotChocolate.Fusion.Events.Contracts;
34
using HotChocolate.Fusion.Extensions;
45
using HotChocolate.Language;
56
using static HotChocolate.Fusion.Logging.LogEntryHelper;
67

7-
namespace HotChocolate.Fusion.PreMergeValidation.Rules;
8+
namespace HotChocolate.Fusion.PreMergeValidationRules;
89

910
/// <summary>
1011
/// This rule ensures that arguments on fields marked as <c>@external</c> have default values
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System.Collections.Immutable;
22
using HotChocolate.Fusion.Events;
3+
using HotChocolate.Fusion.Events.Contracts;
34
using HotChocolate.Fusion.Extensions;
45
using static HotChocolate.Fusion.Logging.LogEntryHelper;
56

6-
namespace HotChocolate.Fusion.PreMergeValidation.Rules;
7+
namespace HotChocolate.Fusion.PreMergeValidationRules;
78

89
/// <summary>
910
/// This rule ensures that any field marked as <c>@external</c> in a source schema is actually

0 commit comments

Comments
 (0)