Skip to content

Commit

Permalink
Fix #1704: handle @required nested fields as in @key (#1706)
Browse files Browse the repository at this point in the history
entity resolver tests for nested @requires fields
  • Loading branch information
Carl Dunham authored Nov 18, 2021
1 parent af33b7c commit 8fa6470
Show file tree
Hide file tree
Showing 8 changed files with 403 additions and 3 deletions.
2 changes: 1 addition & 1 deletion example/federation/reviews/graph/generated/federation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugin/federation/federation.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (ec *executionContext) __resolve_entities(ctx context.Context, representati
}

{{ range .Requires }}
entity.{{.Field.JoinGo `.`}}, err = ec.{{.Type.UnmarshalFunc}}(ctx, rep["{{.Name}}"])
entity.{{.Field.JoinGo `.`}}, err = ec.{{.Type.UnmarshalFunc}}(ctx, rep["{{.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 @@ -231,6 +231,47 @@ func TestEntityResolver(t *testing.T) {
require.Equal(t, resp.Entities[1].Name, "mars")
require.Equal(t, resp.Entities[1].Diameter, 10)
})

t.Run("PlanetRequiresNested entities with requires directive having nested field", func(t *testing.T) {
representations := []map[string]interface{}{
{
"__typename": "PlanetRequiresNested",
"name": "earth",
"world": map[string]interface{}{
"foo": "A",
},
}, {
"__typename": "PlanetRequiresNested",
"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{
"PlanetRequiresNested {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 TestMultiEntityResolver(t *testing.T) {
Expand Down
20 changes: 20 additions & 0 deletions plugin/federation/testdata/entityresolver/entity.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ func (r *entityResolver) FindPlanetRequiresByName(ctx context.Context, name stri
}, nil
}

func (r *entityResolver) FindPlanetRequiresNestedByName(ctx context.Context, name string) (*generated.PlanetRequiresNested, error) {
worlds := map[string]*generated.World{
"earth": {
Foo: "A",
},
"mars": {
Foo: "B",
},
}
world, ok := worlds[name]
if !ok {
return nil, fmt.Errorf("unknown planet: %s", name)
}

return &generated.PlanetRequiresNested{
Name: name,
World: world,
}, nil
}

func (r *entityResolver) FindWorldByHelloNameAndFoo(ctx context.Context, helloName string, foo string) (*generated.World, error) {
return &generated.World{
Hello: &generated.Hello{
Expand Down
Loading

0 comments on commit 8fa6470

Please sign in to comment.