this creates the graph folder and internal structures.
go run github.com/99designs/gqlgen init
Step-4 : Add the db package and update the resolver
I prefer to work base on interface so my application doesn't know or care on the implementation of the persistence layer.
I recommend this way since it's compliant to solid principles, and it's easier to mock the db.
Here this is implemented on persistance package:
type LibraryDb interface {
CreateAuthor(ctx context.Context, data model.Author) (model.Author, error)
UpdateAuthor(ctx context.Context, data model.Author) error
DeleteAuthor(ctx context.Context, Id uint) error
GetAllAuthors(ctx context.Context) ([]*model.Author, error)
GetAuthorById(ctx context.Context, Id uint) (*model.Author, error)
}
type libraryDb struct {
Db *gorm.DB
}
func NewLibraryDb(connectionString string) (LibraryDb, error){}
and on the resolver.go (graph/resolver.go) we update the dependency
package graph
import "github.com/go-related/graphql/persistance"
type Resolver struct {
DB persistance.LibraryDb
}
Step-5 : Update the schema for queries and mutation
The file can be found at graph/schema.graphqls
After adding out types/query/mutation we run the following command to update the corresponding models