From 107892653e275349638cf703482d2c91573d25a3 Mon Sep 17 00:00:00 2001 From: Maxim Filipenko Date: Thu, 4 Mar 2021 01:41:08 +0300 Subject: [PATCH] Fix "error adding scrub fields" for aliases (#126) Co-authored-by: Maxim Filipenko --- plan.go | 2 +- plan_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/plan.go b/plan.go index d413a17..70a9bdc 100644 --- a/plan.go +++ b/plan.go @@ -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 diff --git a/plan_test.go b/plan_test.go index 99b3fda..ad9864c 100644 --- a/plan_test.go +++ b/plan_test.go @@ -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) +}