gographqlgqlgen

Unable to run gqlgen generate due to missing go.sum entry for module


I'm following the tutorial at https://blog.logrocket.com/gqlgen-build-faster-graphql-server/ and gqlgen init runs fine.

Following the guide, I'm trying to generate new schemas and resolvers (adding the Dog type) using go run github.com/99designs/gqlgen generate, but the command instantly fails.

Example:

go run github.com/99designs/gqlgen generate
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/main.go:20:2: missing go.sum entry for module providing package github.com/urfave/cli/v2 (imported by github.com/99designs/gqlgen); to add:
    go get github.com/99designs/gqlgen@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/codegen/field.go:16:2: missing go.sum entry for module providing package golang.org/x/text/cases (imported by github.com/99designs/gqlgen/codegen); to add:
    go get github.com/99designs/gqlgen/codegen@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/codegen/field.go:17:2: missing go.sum entry for module providing package golang.org/x/text/language (imported by github.com/99designs/gqlgen/codegen); to add:
    go get github.com/99designs/gqlgen/codegen@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/internal/imports/prune.go:15:2: missing go.sum entry for module providing package golang.org/x/tools/go/ast/astutil (imported by github.com/99designs/gqlgen/internal/imports); to add:
    go get github.com/99designs/gqlgen/internal/imports@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/internal/code/packages.go:11:2: missing go.sum entry for module providing package golang.org/x/tools/go/packages (imported by github.com/99designs/gqlgen/codegen/config); to add:
    go get github.com/99designs/gqlgen/codegen/config@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/internal/imports/prune.go:16:2: missing go.sum entry for module providing package golang.org/x/tools/imports (imported by github.com/99designs/gqlgen/internal/imports); to add:
    go get github.com/99designs/gqlgen/internal/imports@v0.17.26

So I run all of these go get for those missing modules in that order, and try to re-run generate and it works! Nice! 👏👏

But when I try to run generate again the very next entered command, the same command fails with the same errors and missing packages.

Why are all those imports missing by initial setup, then deleted and why isn't this working out of the box?


Solution

  • Well, the (stupid) answer is to follow the guide at the gqlgen github repo and create a separate tools.go file, like so:

    Add github.com/99designs/gqlgen to your project's tools.go

    printf '// +build tools\npackage tools\nimport (_ "github.com/99designs/gqlgen"\n _ "github.com/99designs/gqlgen/graphql/introspection")' | gofmt > tools.go

    And then do a final go mod tidy.

    The very "opinionated" conclusion from me (the OP) is that this shouldn't be a thing since the init command works just fine after the initial import. But hey.. it is what it is.