Skip to content

Commit

Permalink
Custom mongodb scalar and generate commands
Browse files Browse the repository at this point in the history
  • Loading branch information
adampresley committed Jul 16, 2022
1 parent 31160dd commit 429aca9
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes will be docmented here.

## 2.0.2

* Added custom scalar in GraphQL configuration for MongoDB ObjectID
* Added go generate action in main.go for API Application and JS Application

## 2.0.1

* Fixed readme issue
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.1
2.0.2
5 changes: 5 additions & 0 deletions internal/generators/ApiAppGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func ApiAppGenerator(context *answercontext.Context, localFS filesystem.FileSyst
{TemplateName: "schema.resolvers.go.tmpl", OutputName: filepath.Join("cmd", context.AppName, "graph", "schema.resolvers.go")},
}

if context.WantDatabase && context.WhatTypeOfDatabase == "MongoDB" {
mapping = append(mapping, mappings.MappingType{TemplateName: "bson.go.tmpl", OutputName: filepath.Join("cmd", context.AppName, "internal", "hooks", "bson.go")})
mapping = append(mapping, mappings.MappingType{TemplateName: "ObjectID.go.tmpl", OutputName: filepath.Join("cmd", context.AppName, "internal", "scalars", "ObjectID.go")})
}

dir.MakeDirs(localFS, dirs)
templates.Execute(localFS, templateFS, "templates/apiapp/*.tmpl", mapping, context)
}
5 changes: 5 additions & 0 deletions internal/generators/JSAppGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func JSAppGenerator(context *answercontext.Context, localFS filesystem.FileSyste
{TemplateName: "About.js.tmpl", OutputName: filepath.Join("cmd", context.AppName, "app", "static", "views", "About.js")},
}

if context.WantDatabase && context.WhatTypeOfDatabase == "MongoDB" {
mapping = append(mapping, mappings.MappingType{TemplateName: "bson.go.tmpl", OutputName: filepath.Join("cmd", context.AppName, "internal", "hooks", "bson.go")})
mapping = append(mapping, mappings.MappingType{TemplateName: "ObjectID.go.tmpl", OutputName: filepath.Join("cmd", context.AppName, "internal", "scalars", "ObjectID.go")})
}

dir.MakeDirs(localFS, dirs)
templates.Execute(localFS, templateFS, "templates/jsapp/*.tmpl", mapping, context)
}
26 changes: 26 additions & 0 deletions templates/apiapp/ObjectID.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package scalars

import (
"fmt"
"io"

"github.com/99designs/gqlgen/graphql"
"github.com/globalsign/mgo/bson"
)

func MarshalObjectIdScalar(o bson.ObjectId) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
w.Write([]byte(fmt.Sprintf(`"%s"`, o.Hex())))
})
}

func UnmarshalObjectIdScalar(v interface{}) (bson.ObjectId, error) {
switch v := v.(type) {
case string:
return bson.ObjectId(v), nil

default:
return bson.ObjectId(""), fmt.Errorf("%T is not a bson.ObjectId", v)
}
}

45 changes: 45 additions & 0 deletions templates/apiapp/bson.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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)
}
}

4 changes: 4 additions & 0 deletions templates/apiapp/gqlgen.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ models:
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int64
- github.com/99designs/gqlgen/graphql.Int32
{{- if and .WantDatabase (eq .WhatTypeOfDatabase "MongoDB")}}
ObjectID:
model: {{.GithubPath}}/cmd/{{.AppName}}/internal/scalars.ObjectIdScalar
{{- end}}

5 changes: 5 additions & 0 deletions templates/apiapp/main.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//go:generate go run github.com/99designs/gqlgen generate
{{- if and .WantDatabase (eq .WhatTypeOfDatabase "MongoDB")}}
//go:generate go run internal/hooks/bson.go
{{- end}}

/*
* Copyright © {{.Year}}. App Nerds LLC All Rights Reserved
*/
Expand Down
26 changes: 26 additions & 0 deletions templates/jsapp/ObjectID.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package scalars

import (
"fmt"
"io"

"github.com/99designs/gqlgen/graphql"
"github.com/globalsign/mgo/bson"
)

func MarshalObjectIdScalar(o bson.ObjectId) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
w.Write([]byte(fmt.Sprintf(`"%s"`, o.Hex())))
})
}

func UnmarshalObjectIdScalar(v interface{}) (bson.ObjectId, error) {
switch v := v.(type) {
case string:
return bson.ObjectId(v), nil

default:
return bson.ObjectId(""), fmt.Errorf("%T is not a bson.ObjectId", v)
}
}

45 changes: 45 additions & 0 deletions templates/jsapp/bson.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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)
}
}

4 changes: 4 additions & 0 deletions templates/jsapp/gqlgen.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ models:
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int64
- github.com/99designs/gqlgen/graphql.Int32
{{- if and .WantDatabase (eq .WhatTypeOfDatabase "MongoDB")}}
ObjectID:
model: {{.GithubPath}}/cmd/{{.AppName}}/internal/scalars.ObjectIdScalar
{{- end}}

5 changes: 5 additions & 0 deletions templates/jsapp/main.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//go:generate go run github.com/99designs/gqlgen generate
{{- if and .WantDatabase (eq .WhatTypeOfDatabase "MongoDB")}}
//go:generate go run internal/hooks/bson.go
{{- end}}

/*
* Copyright © {{.Year}}. App Nerds LLC All Rights Reserved
*/
Expand Down

0 comments on commit 429aca9

Please sign in to comment.