outerbanks-api icon indicating copy to clipboard operation
outerbanks-api copied to clipboard

help needed: how to use Gorm models with generated types in Gql

Open DivyanshuBhoyar opened this issue 3 years ago • 0 comments

lets say i need a gorm model like :

type User struct {
	gorm.Model
	Username   string `gorm:"uniqueIndex;not nul"`
	Email      string `gorm:"uniqueIndex;not null"`
	Password   string `gorm:"not null"`
	Bio        *string
	Image      *string
	Followers  []Follow  `gorm:"foreignKey:FollowingID"`
	Followings []Follow  `gorm:"foreignKey:FollowerID"`
	Favorites  []Article `gorm:"many2many:favorites;"`
}

type Follow struct {
	Follower    User
	FollowerID  uint `gorm:"primaryKey" sql:"type:int not null"`
	Following   User
	FollowingID uint `gorm:"primaryKey" sql:"type:int not null"`
}

and my genererated type from gql is

type User struct {
	Username  string  `json:"username"`
	Email     string  `json:"email"`
	Password  string  `json:"password"`
	Bio       *string `json:"bio"`
	Image     *string `json:"image"`
	IsActive  *bool   `json:"isActive"`
	CreatedAt *string `json:"createdAt"`
	UpdatedAt *string `json:"updatedAt"`
}

Do i need to maintain these two seperate types and use them seperately in my resolvers? Noob here with gqlgen

DivyanshuBhoyar avatar Sep 11 '22 08:09 DivyanshuBhoyar