-
Notifications
You must be signed in to change notification settings - Fork 1.2k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add BSON struct tag to generated models #865
Comments
i need this feature |
What about the possibility to add options in graphql schema file like this: input WithMultiPartMember {
multi_part_member: String #`json:"multi_part_member" bson:"multi_part_member,omitempty"`
} I need to add bson omitempty for mongo-go updates with optional fields. Another use case is this: type Car {
id: ID! # `json:"id" bson:"_id"`
...
} Like this the _id in MongoDB is automatically well Marshaled and UnMarshaled |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@vektah - should I make a PR for this? |
https://gqlgen.com/recipes/modelgen-hook/ I think it seems convenient to write plugin settings in gqlgen.yml. |
Following the tips from @vvakame, I created a file called runner.go inside an otherwise empty folder with the following code: package main
import (
"fmt"
"os"
"github.com/99designs/gqlgen/api"
"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/plugin/modelgen"
)
func mutateHook(b *modelgen.ModelBuild) *modelgen.ModelBuild {
for _, model := range b.Models {
for _, field := range model.Fields {
name := field.Name
if name == "id" {
name = "_id"
}
field.Tag += ` bson:"` + name + `"`
}
}
return b
}
func main() {
cfg, err := config.LoadConfigFromDefaultLocations()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to load config", err.Error())
os.Exit(2)
}
p := modelgen.Plugin{
MutateHook: mutateHook,
}
err = api.Generate(cfg,
api.NoPlugins(),
api.AddPlugin(&p),
)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(3)
}
} I then call it with |
You demonstrated how to mannually run the hook, the thing is, how could the mutate hook run alongside with the normal gqlgen generate procedure? This recipe here doesn't show whether we should run the code separately, as what you did. Or we could incorporate the hook into gqlgen generate. @vvakame Please advise. |
It seems the plugin runs the gqlgen generate procedure automatically along with hook. |
@SergeyYakubov this works, thanks! |
I got Can you provide your bson.go? Thanks. |
@kockok - it is exactly the same example showed above #865 (comment), which itself a slight modification of a gqlgen recipe, so your errors must be somewhere else. |
@SergeyYakubov Wondering if |
This should be implemented as default. It is a must! |
This should be a parameter in gqlgen.yml file |
We had the same problem and adapted and expanded the code example above by @kockok a bit further and are now happily generating the models with mongodb specific tags so |
Does anyone face issue when using federated services?
Then during running hook I have following error: failed to load schema: graph/schema.graphqls:48: Undefined directive key.
|
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
What happened?
Generated models (with multi-part member names) cannot be used with MongoDB
What did you expect?
The ability to directly parse the resulting BSON from MongoDB queries into the models generated by gqlgen.
This is handy when the graphql schema exactly matches the models stored in MongoDB
Minimal graphql.schema and models to reproduce
Would currently generate...
Would like it to generate...
Possible fix
Replace plugin/modelgen/models.go:181
with
versions
gqlgen version
v0.9.3-devgo version
1.13Can generate PR if you are happy with it
The text was updated successfully, but these errors were encountered: