For gqlgen to generate the correct files and generated files
Get the following error:
panic: unable to find type trackq/api/app/internal/model.Todo
I am trying the use github.com/99designs/gqlgen@next for Go Modules support when I run
go run cmd/trackq/main.go -v to generate the files I get panic: unable to find type trackq/api/app/internal/model.Todo
gqlgen.yml
schema: "./internal/schema/schema.graphql"
# Let gqlgen know where to put the generated server
exec:
filename: ./internal/schema/generated.go
package: generated
# Let gqlgen know where to put the generated models (if any)
model:
filename: ./internal/model/generated.go
package: model
# Optional, turns on resolver stub generation
resolver:
filename: ./internal/resolver/resolver.go # where to write them
type: Resolver # what's the resolver root implementation type called?
# Tell gqlgen about any existing models you want to reuse for
# graphql. These normally come from the db or a remote api.
models:
Todo:
model: "trackq/api/app/internal/model.Todo"
schema.graphql
schema {
query: Query
}
type Todo {
id: ID!
text: String!
done: Boolean!
}
type Query {
todos: [Todo!]!
}
Basically I'm unsure how to setup this project with GoMods and because it's not an official release yet there seems to be no docs around how to setup a project of gqlgen with GoMods enabled
From the release notes that introduced the concept of internal packages:
To create such a package, place it in a directory named internal or in a subdirectory of a directory named internal. When the go command sees an import of a package with internal in its path, it verifies that the package doing the import is within the tree rooted at the parent of the internal directory. For example, a package .../a/b/c/internal/d/e/f can be imported only by code in the directory tree rooted at .../a/b/c. It cannot be imported by code in .../a/b/g or in any other repository.
So in effect, you're hiding your models from gqlgen 馃槃
So what do we need to put inside the gqlgen.yml for this to work? I have moved the files outside the internal folder and have this as my gqlgen.yml file now
schema: "./app/schema/schema.graphql"
# Let gqlgen know where to put the generated server
exec:
filename: ./app/schema/generated.go
package: schema
# Let gqlgen know where to put the generated models (if any)
model:
filename: ./app/model/generated.go
package: model
# Optional, turns on resolver stub generation
resolver:
filename: ./app/resolver/resolver.go # where to write them
type: Resolver # what's the resolver root implementation type called?
# Tell gqlgen about any existing models you want to reuse for
# graphql. These normally come from the db or a remote api.
models:
Todo:
model: "trackq/app/model.Todo"
But now I am receiving this error
merging failed: unable to build object definition: unable to find type trackq/app/model.Todo
And I am unsure how to fix it
For anyone still struggling with this,
take a look at this answer
Make sure to run go mod tidy after conversion
Most helpful comment
For anyone still struggling with this,
take a look at this answer
Make sure to run
go mod tidyafter conversion