Skip to content

Commit

Permalink
[TT-5199] Extracted pre-merge subgraph normalization into separate me…
Browse files Browse the repository at this point in the history
…thod.

[changelog]
internal: Extracted pre-merge subgraph normalization into separate method.
  • Loading branch information
David Stutt committed May 6, 2022
1 parent 560e9e1 commit 83295b3
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions pkg/federation/sdlmerge/sdlmerge.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,8 @@ func MergeSDLs(SDLs ...string) (string, error) {
rawDocs := make([]string, 0, len(SDLs)+1)
rawDocs = append(rawDocs, rootOperationTypeDefinitions)
rawDocs = append(rawDocs, SDLs...)
n := astnormalization.NewSubgraphDefinitionNormalizer()
for i, subgraph := range rawDocs {
doc, report := astparser.ParseGraphqlDocumentString(subgraph)
if report.HasErrors() {
return "", fmt.Errorf("parse graphql document string: %s", report.Error())
}
n.NormalizeDefinition(&doc, &report)
if report.HasErrors() {
return "", fmt.Errorf("merge ast: %s", report.Error())
}
out, err := astprinter.PrintString(&doc, nil)
if err != nil {
return "", fmt.Errorf("stringify schema: %s", err.Error())
}
rawDocs[i] = out
if err := normalizeSubgraphs(rawDocs); err != nil {
return "", err
}

doc, report := astparser.ParseGraphqlDocumentString(strings.Join(rawDocs, "\n"))
Expand All @@ -72,6 +59,26 @@ func MergeSDLs(SDLs ...string) (string, error) {
return out, nil
}

func normalizeSubgraphs(subgraphs []string) error {
subgraphNormalizer := astnormalization.NewSubgraphDefinitionNormalizer()
for i, subgraph := range subgraphs {
doc, report := astparser.ParseGraphqlDocumentString(subgraph)
if report.HasErrors() {
return fmt.Errorf("parse graphql document string: %s", report.Error())
}
subgraphNormalizer.NormalizeDefinition(&doc, &report)
if report.HasErrors() {
return fmt.Errorf("normalize subgraph: %s", report.Error())
}
out, err := astprinter.PrintString(&doc, nil)
if err != nil {
return fmt.Errorf("stringify schema: %s", err.Error())
}
subgraphs[i] = out
}
return nil
}

type normalizer struct {
walkers []*astvisitor.Walker
}
Expand Down

0 comments on commit 83295b3

Please sign in to comment.