Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid problems with
val
being undefined in the federation template. (…
…#1760) * Avoid problems with `val` being undefined in the federation template. When running gqlgen over our schema, we were seeing errors like: ``` assignments/generated/graphql/service.go:300:4: val declared but not used ``` The generated code looks like this: ``` func entityResolverNameForMobileNavigation(ctx context.Context, rep map[string]interface{}) (string, error) { for { var ( m map[string]interface{} val interface{} ok bool ) m = rep if _, ok = m["kaid"]; !ok { break } m = rep if _, ok = m["language"]; !ok { break } return "findMobileNavigationByKaidAndLanguage", nil } return "", fmt.Errorf("%w for MobileNavigation", ErrTypeNotFound) } ``` Looking at the code, it's pretty clear that this happens when there are multiple key-fields, but each of them has only one keyField.Field entry. This is because the old code looked at `len(keyFields)` to decide whether to declare the `val` variable, but looks at `len(keyField.Field)` for each keyField to decide whether to use the `val` variable. The easiest solution, and the one I do in this PR, is to just declare `val` all the time, and use a null-assignment to quiet the compiler when it's not used. * run go generate to update generated files * run go generate to update moar generated files * Adding a test for verify that this fixes the issue. From `plugins/federation`, run the following command and verify that no errors are produced ``` go run github.com/99designs/gqlgen --config testdata/entityresolver/gqlgen.yml ``` Co-authored-by: MiguelCastillo <miguel@khanacademy.org>
- Loading branch information