Add BSON struct tag to generated models #1943
Replies: 16 comments 1 reply
-
i need this feature |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
@vektah - should I make a PR for this? |
Beta Was this translation helpful? Give feedback.
-
https://gqlgen.com/recipes/modelgen-hook/ I think it seems convenient to write plugin settings in gqlgen.yml. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
It seems the plugin runs the gqlgen generate procedure automatically along with hook. |
Beta Was this translation helpful? Give feedback.
-
@SergeyYakubov this works, thanks! |
Beta Was this translation helpful? Give feedback.
-
I got Can you provide your bson.go? Thanks. |
Beta Was this translation helpful? Give feedback.
-
@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. |
Beta Was this translation helpful? Give feedback.
-
@SergeyYakubov Wondering if |
Beta Was this translation helpful? Give feedback.
-
This should be implemented as default. It is a must! |
Beta Was this translation helpful? Give feedback.
-
This should be a parameter in gqlgen.yml file |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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.
|
Beta Was this translation helpful? Give feedback.
-
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
Beta Was this translation helpful? Give feedback.
All reactions