Skip to content

Commit

Permalink
Fix "error adding scrub fields" for aliases (#126)
Browse files Browse the repository at this point in the history
Co-authored-by: Maxim Filipenko <[email protected]>
  • Loading branch information
prokaktus and Maxim Filipenko authored Mar 3, 2021
1 parent d787b4a commit 1078926
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ func (p *MinQueriesPlanner) generateScrubFieldsWalk(step *QueryPlanStep, selecti
// look over the points in the selection
for _, field := range graphql.SelectedFields(targetSelection) {
// if the field name is what we expected
if field.Name == point {
if field.Name == point || field.Alias == point {
// our next selection set is the fields selection set
targetSelection = field.SelectionSet

Expand Down
66 changes: 66 additions & 0 deletions plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1687,3 +1687,69 @@ func TestPlanQuery_forcedPriorityResolution(t *testing.T) {
assert.Equal(t, "lastName", lastNameField.Name)
assert.Equal(t, location2, lastNameStep.Queryer.(*graphql.SingleRequestQueryer).URL())
}

func TestPlanQuery_scrubWithAlias(t *testing.T) {
schema, _ := graphql.LoadSchema(`
type User {
firstName: String!
catPhotos: [CatPhoto!]!
}
type CatPhoto {
URL: String!
}
type Query {
allUsers: [User!]!
}
`)

// the location of the user service
userLocation := "user-location"
// the location of the cat service
catLocation := "cat-location"

// the location map for fields for this query
locations := FieldURLMap{}
locations.RegisterURL("Query", "allUsers", userLocation)
locations.RegisterURL("User", "firstName", userLocation)
locations.RegisterURL("User", "catPhotos", catLocation)
locations.RegisterURL("CatPhoto", "URL", catLocation)
locations.RegisterURL("CatPhoto", "name", catLocation)

plans, err := (&MinQueriesPlanner{}).Plan(&PlanningContext{
Query: `
{
users: allUsers {
firstName
catPhotos {
URL
}
}
}
`,
Schema: schema,
Locations: locations,
})
// if something went wrong planning the query
if err != nil {
// the test is over
t.Errorf("encountered error when building schema: %s", err.Error())
return
}

// the first step should have all users
firstStep := plans[0].RootStep.Then[0]
// make sure we are grabbing values off of Query since its the root
assert.Equal(t, "Query", firstStep.ParentType)

// make sure there's a selection set
if len(firstStep.SelectionSet) != 1 {
t.Error("first step did not have a selection set")
return
}
// check that allUsers selected and alias presented
firstField := graphql.SelectedFields(firstStep.SelectionSet)[0]
assert.Equal(t, "allUsers", firstField.Name)
assert.Equal(t, "users", firstField.Alias)
}

0 comments on commit 1078926

Please sign in to comment.