Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Type Rename Merge Middleware for new Gateway (#5142)
- Loading branch information
1 parent
76da4a1
commit 74f5a82
Showing
30 changed files
with
1,134 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...Chocolate/Stitching/src/Stitching.Types/Pipeline/ApplyRenaming/ApplyRenamingMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using HotChocolate.Language; | ||
|
||
namespace HotChocolate.Stitching.Types.Pipeline.ApplyRenaming; | ||
|
||
public sealed class ApplyRenamingMiddleware | ||
{ | ||
private readonly RenameIndexer _indexer = new(); | ||
private readonly RenameRewriter _rewriter = new(); | ||
private readonly MergeSchema _next; | ||
|
||
public ApplyRenamingMiddleware(MergeSchema next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async ValueTask InvokeAsync(ISchemaMergeContext context) | ||
{ | ||
for (var i = 0; i < context.Documents.Count; i++) | ||
{ | ||
Document doc = context.Documents[i]; | ||
|
||
var renameContext = new RenameContext(doc.Name); | ||
_indexer.Visit(doc.SyntaxTree, renameContext); | ||
|
||
if (renameContext.RenamedFields.Count > 0) | ||
{ | ||
foreach ((var coordinate, var renameInfo) in renameContext.RenamedFields.ToArray()) | ||
{ | ||
var possibleInterface = coordinate.Name.Value; | ||
if (renameContext.ImplementedBy.TryGetValue(possibleInterface, out var types)) | ||
{ | ||
foreach (var implementor in types) | ||
{ | ||
var implCoordinate = coordinate.WithName(new(implementor)); | ||
renameContext.RenamedFields[implCoordinate] = renameInfo; | ||
} | ||
} | ||
} | ||
|
||
foreach (var coordinate in renameContext.RenamedFields.Keys) | ||
{ | ||
renameContext.TypesWithFieldRenames.Add(coordinate.Name.Value); | ||
} | ||
} | ||
|
||
var syntaxTree = (DocumentNode)_rewriter.Rewrite(doc.SyntaxTree, renameContext); | ||
context.Documents = context.Documents.SetItem(i, new Document(doc.Name, syntaxTree)); | ||
} | ||
|
||
await _next(context); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/HotChocolate/Stitching/src/Stitching.Types/Pipeline/ApplyRenaming/RenameContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using HotChocolate.Language; | ||
using HotChocolate.Language.Visitors; | ||
|
||
namespace HotChocolate.Stitching.Types.Pipeline.ApplyRenaming; | ||
|
||
internal sealed class RenameContext : INavigatorContext | ||
{ | ||
public RenameContext(string sourceName) | ||
{ | ||
SourceName = sourceName; | ||
} | ||
|
||
public string SourceName { get; } | ||
|
||
public Dictionary<string, RenameInfo> RenamedTypes { get; } = | ||
new(StringComparer.Ordinal); | ||
|
||
public Dictionary<string, HashSet<string>> ImplementedBy { get; } = | ||
new(StringComparer.Ordinal); | ||
|
||
public Dictionary<SchemaCoordinateNode, RenameInfo> RenamedFields { get; } = | ||
new(SyntaxComparer.BySyntax); | ||
|
||
public HashSet<string> TypesWithFieldRenames { get; } = | ||
new HashSet<string>(StringComparer.Ordinal); | ||
|
||
public ISyntaxNavigator Navigator { get; } = new DefaultSyntaxNavigator(); | ||
} |
Oops, something went wrong.