contrib
contrib copied to clipboard
feat(entproto): add support for custom package in `gen-entgrpc`
This PR allows you to generate services when database assets are not generated in the schema
parent directory and/or the package name is not ent
.
Currently entc.LoadGraph
uses an empty configuration, setting the Package
value to the schema parent directory name by default.
It may lead to broken imports if the database assets are generated by providing a different Target
to entc.Generate
.
For example, if the schema directory is in ent/schema
and the entc.Generate
target is db
as in the diagram below, entpb_user_service.go
will try to import the User
entity from "project/ent/user"
instead of "project/db/user"
.
project
├───ent
│ └───schema
│ │ user.go
│ │ ...
└───db
└───user
│ │ user.go
│ │ ...
└───proto
└───entpb
│ │ entpb.proto
│ │ entpb_user_service.go
│ │ ...
│ runtime.go
│ ...
Using the go_package
flag it is possible to pass a custom Package
value to entc.LoadGraph
configuration and in the templates, hard-coded ent.*
are accordingly replaced with $entPackage
variable to allow for different package names.
Hey @marcozac !
Thanks for the contribution. Could you please elaborate more on this change, why/when is it needed?
Hey @marcozac !
Thanks for the contribution. Could you please elaborate more on this change, why/when is it needed?
Hi @rotemtam, sure!
Sorry, I opened this draft PR before realising that it was almost 2:00 am 😅.
I'll update it shortly. I'm writing an example and a test just now!
This would be really helpful for us as well. We use the target flag to generate ent into a generated
folder. That works fine for ent and for entgql, but entproto doesn't have a way to handle it. We ended up having to write a script that rewrote some of the imports to include our target directory.