Skip to content

Commit

Permalink
always merge distinct steps.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmac committed Nov 3, 2021
1 parent 937ba59 commit 5e15cc8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
21 changes: 20 additions & 1 deletion plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bramble

import (
"context"
"strings"
"encoding/json"
"fmt"

Expand Down Expand Up @@ -206,15 +207,32 @@ func extractSelectionSet(ctx *PlanningContext, insertionPoint []string, parentTy
}
}

// Create child steps for all remote field selections
if len(remoteSelections) > 0 {
// Create child steps for all remote field selections
childrenSteps, err := createSteps(ctx, insertionPoint, parentType, location, remoteSelections, true)
if err != nil {
return nil, nil, err
}
childrenStepsResult = append(childrenStepsResult, childrenSteps...)
}

if len(childrenStepsResult) > 1 {
// Merge steps targeting distinct service/path locations
mergedSteps := []*QueryPlanStep{}
mergedStepsMap := map[string]*QueryPlanStep{}
for _, step := range childrenStepsResult {
key := strings.Join(append([]string{step.ServiceURL}, step.InsertionPoint...), "/")
if existingStep, ok := mergedStepsMap[key]; ok {
existingStep.SelectionSet = append(existingStep.SelectionSet, step.SelectionSet...)
existingStep.Then = append(existingStep.Then, step.Then...)
} else {
mergedStepsMap[key] = step
mergedSteps = append(mergedSteps, step)
}
}
childrenStepsResult = mergedSteps
}

parentDef := ctx.Schema.Types[parentType]
// For abstract types, add an id fragment for all possible boundary
// implementations. This assures that abstract boundaries always return
Expand All @@ -241,6 +259,7 @@ func extractSelectionSet(ctx *PlanningContext, insertionPoint []string, parentTy
ObjectDefinition: implementationType,
}
selectionSetResult = append([]ast.Selection{possibleId}, selectionSetResult...)
break
}
}
// Otherwise, add an id selection to boundary types where the result
Expand Down
40 changes: 37 additions & 3 deletions plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestQueryPlanABA2(t *testing.T) {
"SelectionSet": "{ _id: id compTitles(limit: 42) { id compTitles(limit: 666) { id } } }",
"InsertionPoint": ["movies"],
"Then": [
{
{
"ServiceURL": "A",
"ParentType": "Movie",
"SelectionSet": "{ _id: id title }",
Expand Down Expand Up @@ -408,7 +408,7 @@ func TestQueryPlanFragmentSpread2(t *testing.T) {
PlanTestFixture1.Check(t, query, plan)
}

func TestQueryPlanMergeDeepTraversal(t *testing.T) {
func TestQueryPlanCompleteDeepTraversal(t *testing.T) {
query := `
{
shop1 {
Expand Down Expand Up @@ -451,6 +451,40 @@ func TestQueryPlanMergeDeepTraversal(t *testing.T) {
PlanTestFixture6.Check(t, query, plan)
}

func TestQueryPlanMergeInsertionPointSteps(t *testing.T) {
query := `
{
shop1 {
products {
name
}
products {
name
}
}
}`
plan := `{
"RootSteps": [
{
"ServiceURL": "A",
"ParentType": "Query",
"SelectionSet": "{ shop1 { products { _id: id } products { _id: id } } }",
"InsertionPoint": null,
"Then": [
{
"ServiceURL": "B",
"ParentType": "Product",
"SelectionSet": "{ _id: id name _id: id name }",
"InsertionPoint": ["shop1", "products"],
"Then": null
}
]
}
]
}`
PlanTestFixture6.Check(t, query, plan)
}

func TestQueryPlanExpandAbstractTypesWithPossibleBoundaryIds(t *testing.T) {
query := `
{
Expand Down Expand Up @@ -490,7 +524,7 @@ func TestQueryPlanInlineFragmentSpreadOfInterface(t *testing.T) {
{
"ServiceURL": "A",
"ParentType": "Query",
"SelectionSet": "{ animals { ... on Snake { _id: id } ... on Lion { _id: id } name ... on Lion { maneColor __typename } ... on Snake { _id: id __typename } } }",
"SelectionSet": "{ animals { ... on Lion { _id: id } ... on Snake { _id: id } name ... on Lion { maneColor __typename } ... on Snake { _id: id __typename } } }",
"InsertionPoint": null,
"Then": [
{
Expand Down

0 comments on commit 5e15cc8

Please sign in to comment.