-
What happened?I want to bind What did you expect?type User struct {
gorm.Model //<-- here
ID Int
} Minimal graphql.schema and models to reproducetype User {
id:ID!
} versions
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hi there! I've been using go for around the last 8 hours, so I don't know how legal this is, but I had the same question as you and I solved it with this code: Code// generate/main.go
package main
import (
"fmt"
"go/importer"
"go/token"
"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 {
fset := token.NewFileSet()
tpatata, err := importer.ForCompiler(fset, "source", nil).Import("gorm.io/gorm")
if err != nil {
panic("Error importing gorm" + err.Error())
}
for _, model := range b.Models {
model.Fields = append(model.Fields, &modelgen.Field{Type: tpatata.Scope().Lookup("Model").Type()})
for _, field := range model.Fields {
field.Tag += ` test"`
}
}
return b
}
func main() {
cfg, err := config.LoadConfigFromDefaultLocations()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to load config", err.Error())
os.Exit(2)
}
// Attaching the mutation function onto modelgen plugin
p := modelgen.Plugin{
MutateHook: mutateHook,
}
err = api.Generate(cfg, api.ReplacePlugin(&p))
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(3)
}
} It took me a while to figure out how the Code generated// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.
package model
import (
"gorm.io/gorm"
)
type NewTodo struct {
Text string `json:"text" test"`
UserID string `json:"userId" test"`
gorm.Model ` test"`
}
type NewUser struct {
Name string `json:"name" test"`
gorm.Model ` test"`
}
type Todo struct {
ID string `json:"id" test"`
Text string `json:"text" test"`
Done bool `json:"done" test"`
User *User `json:"user" test"`
gorm.Model ` test"`
}
type User struct {
ID string `json:"id" test"`
Name string `json:"name" test"`
gorm.Model ` test"`
} From the following schema: Schema# GraphQL schema example
#
# https://gqlgen.com/getting-started/
type Todo {
id: ID!
text: String!
done: Boolean!
user: User!
}
type User {
id: ID!
name: String!
}
type Query {
todos: [Todo!]!
users: [User!]!
}
input NewTodo {
text: String!
userId: String!
}
input NewUser {
name: String!
}
type Mutation {
createTodo(input: NewTodo!): Todo!
createUser(input: NewUser!): User!
}
type Subscription {
testSub: String
} It's not perfect, but as you can see it's adding |
Beta Was this translation helpful? Give feedback.
-
Hey, so, small update: I've updated the plugin, and I made it react to directives. You can now mark types to make them include gorm.Model and mark fields to add gorm related metadata to their tags. I've also added "belogs to" relationships as a proof of concept and it looks like it working nicely. Basically, you can now turn this: schema.graphqls# GraphQL schema example
#
# https://gqlgen.com/getting-started/
directive @db on OBJECT
directive @gorm(
value: String
belongsTo: String
) on FIELD_DEFINITION
type Todo @db {
id: ID! @gorm(value: "primaryKey")
text: String!
done: Boolean!
user: User! @gorm(belongsTo: "User")
}
type User @db {
id: ID! @gorm(value: "primaryKey")
name: String! @gorm(value: "unique")
}
type Query {
todos: [Todo!]!
users: [User!]!
}
input NewTodo {
text: String!
userId: String!
}
input NewUser {
name: String!
}
type Mutation {
createTodo(input: NewTodo!): Todo!
createUser(input: NewUser!): User!
}
type Subscription {
testSub: String
} Into this: models_gen.go// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.
package model
import (
"gorm.io/gorm"
)
type NewTodo struct {
Text string `json:"text"`
UserID string `json:"userId"`
}
type NewUser struct {
Name string `json:"name"`
}
type Todo struct {
gorm.Model ``
ID string `json:"id" gorm:"primaryKey"`
Text string `json:"text"`
Done bool `json:"done"`
User *User `json:"user"`
UserID string ``
}
type User struct {
gorm.Model ``
ID string `json:"id" gorm:"primaryKey"`
Name string `json:"name" gorm:"unique"`
} The code is a bit more complex now, so if you still have a use case for this concept, let me know and I'll upload the code to a repo Cheers! |
Beta Was this translation helpful? Give feedback.
Hi there! I've been using go for around the last 8 hours, so I don't know how legal this is, but I had the same question as you and I solved it with this code:
Code