作者:php.com | 来源:互联网 | 2023-10-15 13:26
What happened?
We have some existing GraphQL schema that uses lower cased names and existing Go structs that are upper cased to be exportable. The type with the lower cased name also includes another type in its fields. Autobinding does not automatically match these two but that is easy enough to work around. I have tried both using the goModel directive and by explicit binding in gqlgen.yml. In both of these cases the binding works but some of the generated functions are lowercased and so not exported. Specifically it is a problem with the child resolver.
I get this error
1
| galleryResolver not exported by package generated |
with the example code below.
What did you expect?
I expected generated functions to be uppercased and so exported as they would be if the GraphQL type name was upper cased but then bound to the lowercased GraphQL schema name.
Minimal graphql.schema and models to reproduce
Schema
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| directive (model: String, models: [String!]) on OBJECT
| INPUT_OBJECT
| SCALAR
| ENUM
| INTERFACE
| UNION
type Query {
galleries: [gallery!]!
}
type gallery (model: "tmp/gqlgen-bug/graph/model.Gallery") {
id: ID!
images: [Image]
}
type Image {
url: String
} |
Model
1 2 3 4 5 6 7 8
| type Gallery struct {
ID string
Images []GalleryImage
}
type GalleryImage struct {
URL string
} |
versions
- v0.11.3
- 1.14.3
- dep or go modules? Go modules
该提问来源于开源项目:99designs/gqlgen
Sure the
doesn't do it perfectly unfortunately you can't just manually fix
changes need to be made to generated.go also. I think this is an opportunity to improve generation of both files.
The fix seems fairly straight forward, in generated.go the Resolver interface needs to expose the gallery method and the galleryResolver interface needs to be exposed. Only after those changes can you make a change like this in schema.resolvers.go which allows it all to work.
1
| func (r *Resolver) Gallery() generated.GalleryResolver { return &galleryResolver{r} } |