99designs / gqlgen

go generate based graphql server library
https://gqlgen.com
MIT License
9.93k stars 1.16k forks source link

gqlgen init error in loading gqlparser. #1402

Closed KellyLSB closed 2 years ago

KellyLSB commented 3 years ago

What happened?

kellylsbkr@penguin:~/go/src/github.com/KellyLSB/angelrin$ go run github.com/99designs/gqlgen init
validation failed: packages.Load: graph/generated/generated.go:16:2: cannot find package "github.com/vektah/gqlparser/v2" in any of:
        /usr/local/go/src/github.com/vektah/gqlparser/v2 (from $GOROOT)
        /home/kellylsbkr/go/src/github.com/vektah/gqlparser/v2 (from $GOPATH)
graph/generated/generated.go:17:2: cannot find package "github.com/vektah/gqlparser/v2/ast" in any of:
        /usr/local/go/src/github.com/vektah/gqlparser/v2/ast (from $GOROOT)
        /home/kellylsbkr/go/src/github.com/vektah/gqlparser/v2/ast (from $GOPATH)
/home/kellylsbkr/go/src/github.com/KellyLSB/angelrin/graph/generated/generated.go:16:12: could not import github.com/vektah/gqlparser/v2 (invalid package name: "")
/home/kellylsbkr/go/src/github.com/KellyLSB/angelrin/graph/generated/generated.go:17:2: could not import github.com/vektah/gqlparser/v2/ast (invalid package name: "")

Exec "go run ./server.go" to start GraphQL server

What did you expect?

A v2 directory within gqlparser; or... something

Minimal graphql.schema and models to reproduce

This is the project init

versions

This has not been a problem in my older application of gqlgen; merely the new init.

diffuse commented 3 years ago

This is referenced in #1105, if you look at the go.mod file for gqlgen, you can see that the module is defined as github.com/vektah/gqlparser/v2.

Like #1105 says, if you're not running in go module mode, I believe $GOPATH/src is searched for github.com/vektah/gqlparser/v2, which won't exist, since go get downloads it to github.com/vektah/gqlparser

If you're ok with using go module mode, then an easy fix for it is to:

If your project is called foo, your go.mod file will end up looking something like this:

module github.com/<your username>/foo

go 1.15

require (
    github.com/99designs/gqlgen v0.13.0
    github.com/vektah/gqlparser/v2 v2.1.0
)

When you run go install ./..., your other dependencies should also be automatically added to this file.

Your previous application probably used a sub-2.0 version of gqlgen, which would have the module defined without the v2 (go.mod)

frederikhors commented 3 years ago

If @diffuse's answer solves it please close this issue, @KellyLSB.

KellyLSB commented 3 years ago

Presently:

kellylsbkr@penguin:~/go/src/github.com/KellyLSB/demondin$ go run github.com/99designs/gqlgen init
package github.com/99designs/gqlgen: no Go files in /home/kellylsbkr/go/src/github.com/KellyLSB/demondin/vendor/github.com/99designs/gqlgen
kellylsbkr@penguin:~/go/src/github.com/KellyLSB/demondin$ ls vendor/github.com/99designs/gqlgen/
complexity  graphql  handler  LICENSE

I've run go mod init && go mod vendor

go.mod

module github.com/KellyLSB/demondin

go 1.15

require (
    github.com/99designs/gqlgen v0.13.0
    github.com/agnivade/levenshtein v1.1.0 // indirect
    github.com/araddon/dateparse v0.0.0-20201001162425-8aadafed4dc4
    github.com/go-macaron/inject v0.0.0-20200308113650-138e5925c53b // indirect
    github.com/go-macaron/session v1.0.1
    github.com/gobuffalo/flect v0.2.2
    github.com/google/uuid v1.1.2
    github.com/gorilla/websocket v1.4.2
    github.com/hashicorp/golang-lru v0.5.4 // indirect
    github.com/jinzhu/gorm v1.9.16
    github.com/joho/godotenv v1.3.0
    github.com/kr/pretty v0.2.1
    github.com/kr/text v0.2.0 // indirect
    github.com/lib/pq v1.8.0
    github.com/stripe/stripe-go v56.1.0+incompatible
    github.com/vektah/gqlparser v1.3.1
    golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9 // indirect
    golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
    gopkg.in/ini.v1 v1.62.0 // indirect
    gopkg.in/macaron.v1 v1.4.0
    gopkg.in/yaml.v2 v2.3.0
)

go run -mod=mod github.com/99designs/gqlgen works

rokiyama commented 3 years ago

I have had the same issue, I have run go get github.com/vektah/gqlparser/v2 but I still get the another error.

$ go run github.com/99designs/gqlgen init
validation failed: packages.Load: /Users/rokiyama/projects/gqlgen-todos/graph/prelude.resolvers.go:19:44: __DirectiveResolver not exported by package generated

full logs:

$ mkdir gqlgen-todos
$ cd gqlgen-todos
$ go mod init github.com/rokiyama/gqlgen-todos
go: creating new go.mod: module github.com/rokiyama/gqlgen-todos

$ go get github.com/99designs/gqlgen
go get: added github.com/99designs/gqlgen v0.13.0

$ go get github.com/vektah/gqlparser/v2
go get: upgraded github.com/vektah/gqlparser/v2 v2.1.0 => v2.2.0

$ go run github.com/99designs/gqlgen init
validation failed: packages.Load: /Users/rokiyama/projects/gqlgen-todos/graph/prelude.resolvers.go:19:44: __DirectiveResolver not exported by package generated

Exec "go run ./server.go" to start GraphQL server

$ go run ./server.go
# github.com/rokiyama/gqlgen-todos/graph
graph/prelude.resolvers.go:19:34: cannot refer to unexported name generated.__DirectiveResolver

my go.mod:

module github.com/rokiyama/gqlgen-todos

go 1.16

require (
        github.com/99designs/gqlgen v0.13.0 // indirect
        github.com/vektah/gqlparser/v2 v2.2.0 // indirect
)
diffuse commented 3 years ago

Looks like this may be an issue with gqlparser v2.2.0 (released 8 days ago as of this post). I haven't looked into the specifics, but I just was able to replicate the behavior you mention on my system, then fix it with the resolution mentioned here.

Basically, in your go.mod file:

Credit to FreezeLook for this solution.

diffuse commented 3 years ago

Looks like this change is being discussed over in https://github.com/vektah/gqlparser/issues/154.

rokiyama commented 3 years ago

@diffuse The solution worked for me. Thank you.

wangzuo commented 3 years ago

I guess https://github.com/99designs/gqlgen/blob/master/graphql/introspection/introspection.go#L12 is missing in v0.13.0, so a new release will fix this issue #1534. go get github.com/99designs/gqlgen@master will also work for now.

frederikhors commented 2 years ago

@KellyLSB please tell me if this is still an issue with v0.16.0 and I'll re-open it.