Skip to content

Commit

Permalink
fix requires directive with nested field when entityResolver directiv…
Browse files Browse the repository at this point in the history
…e is used (#1863)

Co-authored-by: chedom <chedom@syneforge.com>
  • Loading branch information
chedom and chedom authored Jan 29, 2022
1 parent 25c2cdc commit e0b42f9
Show file tree
Hide file tree
Showing 7 changed files with 542 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugin/federation/federation.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (ec *executionContext) __resolve_entities(ctx context.Context, representati

for i, entity := range entities {
{{- range $entity.Requires }}
entity.{{.Field.JoinGo `.`}}, err = ec.{{.Type.UnmarshalFunc}}(ctx, reps[i]["{{.Name}}"])
entity.{{.Field.JoinGo `.`}}, err = ec.{{.Type.UnmarshalFunc}}(ctx, reps[i]["{{.Field.Join `"].(map[string]interface{})["`}}"])
if err != nil {
return err
}
Expand Down
41 changes: 41 additions & 0 deletions plugin/federation/federation_entityresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,47 @@ func TestMultiEntityResolver(t *testing.T) {
require.Equal(t, resp.Entities[1].Key1, "key1 - 2")
require.Equal(t, resp.Entities[1].Key2, "key2 - 2")
})

t.Run("MultiPlanetRequiresNested entities with requires directive having nested field", func(t *testing.T) {
representations := []map[string]interface{}{
{
"__typename": "MultiPlanetRequiresNested",
"name": "earth",
"world": map[string]interface{}{
"foo": "A",
},
}, {
"__typename": "MultiPlanetRequiresNested",
"name": "mars",
"world": map[string]interface{}{
"foo": "B",
},
},
}

var resp struct {
Entities []struct {
Name string `json:"name"`
World struct {
Foo string `json:"foo"`
} `json:"world"`
} `json:"_entities"`
}

err := c.Post(
entityQuery([]string{
"MultiPlanetRequiresNested {name, world { foo }}",
}),
&resp,
client.Var("representations", representations),
)

require.NoError(t, err)
require.Equal(t, resp.Entities[0].Name, "earth")
require.Equal(t, resp.Entities[0].World.Foo, "A")
require.Equal(t, resp.Entities[1].Name, "mars")
require.Equal(t, resp.Entities[1].World.Foo, "B")
})
}

func entityQuery(queries []string) string {
Expand Down
28 changes: 28 additions & 0 deletions plugin/federation/testdata/entityresolver/entity.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,34 @@ func (r *entityResolver) FindManyMultiHelloWithErrorByNames(ctx context.Context,
return nil, fmt.Errorf("error resolving MultiHelloWorldWithError")
}

func (r *entityResolver) FindManyMultiPlanetRequiresNestedByNames(ctx context.Context, reps []*generated.MultiPlanetRequiresNestedByNamesInput) ([]*generated.MultiPlanetRequiresNested, error) {
worlds := map[string]*generated.World{
"earth": {
Foo: "A",
},
"mars": {
Foo: "B",
},
}

results := make([]*generated.MultiPlanetRequiresNested, len(reps))

for i := range reps {
name := reps[i].Name
world, ok := worlds[name]
if !ok {
return nil, fmt.Errorf("unknown planet: %s", name)
}

results[i] = &generated.MultiPlanetRequiresNested{
Name: name,
World: world,
}
}

return results, nil
}

func (r *entityResolver) FindPlanetMultipleRequiresByName(ctx context.Context, name string) (*generated.PlanetMultipleRequires, error) {
return &generated.PlanetMultipleRequires{Name: name}, nil
}
Expand Down
Loading

0 comments on commit e0b42f9

Please sign in to comment.