-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom mongodb scalar and generate commands
- Loading branch information
1 parent
31160dd
commit 429aca9
Showing
12 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2.0.1 | ||
2.0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters