Khan / genqlient

a truly type-safe Go GraphQL client
MIT License
1.02k stars 99 forks source link

Improve package-sniffing and bind correctly to types in the same package #316

Closed benjaminjkraft closed 4 months ago

benjaminjkraft commented 4 months ago

The original motivation here is to fix #283, which is that if you try to use bindings to bind to a type in the same package as the generated code, we generate a self-import, which Go doesn't allow. Fixing that is easy -- the three lines in imports.go -- once you know the package-path of the generated code. (The test that that all fits together is in integration-tests because that was the easiest place to set up the right situation.)

Determining the package-path is not too much harder: you ask go/packages, which we already use for package_bindings. But once we're doing that, and handling errors, it's kinda silly that we ask you to specify the package your generated code will use, because we have that too, usually. So I rewrote that handling too, making package now rarely necessary (see for example the example config), and warning if it looks wrong. This is the changes in config.go, and is the more substantial non-test change. (I also renamed some of the testdata dirs to be valid package-names, to exercise more of that code, or in the case of find-config, just for consistency.)

I have: