Skip to content

How to bind a struct to all generated Models? #1841

Answered by rmortes
darmie asked this question in Q&A
Discussion options

You must be logged in to vote

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, &mo…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by darmie
Comment options

You must be logged in to vote
5 replies
@nephix
Comment options

@rmortes
Comment options

@nephix
Comment options

@shanehughes1990
Comment options

@nephix
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #1840 on January 24, 2022 16:34.