commit bc33810cb0ba61ba10207c1bdfd34d56792a69fb Author: Darko Luketic Date: Fri Nov 29 15:29:38 2024 +0100 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..75e1ff8 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# xidgql + +xid graphql gqlgen marshaller and unmarshaller also in combination with ent. + +In gqlgen.yml +```yaml +models: + ID: + model: + - code.icod.de/dalu/xidgql/xidgql.ID +``` + +as an ID field in an ent schema +```go +package schema + +import ( + "github.com/rs/xid" +) + +// Fields of the Model. +func (Model) Fields() []ent.Field { + return []ent.Field{ + field.String("id"). + GoType(xid.ID{}). + DefaultFunc(xid.New). + Annotations( + entsql.Annotation{Size: 12}, + ). + Unique(), + } +} +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6a2ada6 --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module code.icod.de/dalu/xidgql + +go 1.23 + +require ( + github.com/99designs/gqlgen v0.17.57 + github.com/rs/xid v1.6.0 +) + +require ( + github.com/google/uuid v1.6.0 // indirect + github.com/sosodev/duration v1.3.1 // indirect + github.com/vektah/gqlparser/v2 v2.5.19 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..99b34fc --- /dev/null +++ b/go.sum @@ -0,0 +1,22 @@ +github.com/99designs/gqlgen v0.17.57 h1:Ak4p60BRq6QibxY0lEc0JnQhDurfhxA67sp02lMjmPc= +github.com/99designs/gqlgen v0.17.57/go.mod h1:Jx61hzOSTcR4VJy/HFIgXiQ5rJ0Ypw8DxWLjbYDAUw0= +github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ= +github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU= +github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= +github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= +github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= +github.com/sosodev/duration v1.3.1 h1:qtHBDMQ6lvMQsL15g4aopM4HEfOaYuhWBw3NPTtlqq4= +github.com/sosodev/duration v1.3.1/go.mod h1:RQIBBX0+fMLc/D9+Jb/fwvVmo0eZvDDEERAikUR6SDg= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/vektah/gqlparser/v2 v2.5.19 h1:bhCPCX1D4WWzCDvkPl4+TP1N8/kLrWnp43egplt7iSg= +github.com/vektah/gqlparser/v2 v2.5.19/go.mod h1:y7kvl5bBlDeuWIvLtA9849ncyvx6/lj06RsMrEjVy3U= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/xid.go b/xid.go new file mode 100644 index 0000000..33ddcf0 --- /dev/null +++ b/xid.go @@ -0,0 +1,24 @@ +package xidgql + +import ( + "fmt" + "io" + "strconv" + + "github.com/99designs/gqlgen/graphql" + "github.com/rs/xid" +) + +func MarshalID(id xid.ID) graphql.Marshaler { + return graphql.WriterFunc(func(w io.Writer) { + _, _ = io.WriteString(w, strconv.Quote(id.String())) + }) +} + +func UnmarshalID(v interface{}) (id xid.ID, err error) { + s, ok := v.(string) + if !ok { + return id, fmt.Errorf("invalid type %T, expect string", v) + } + return xid.FromString(s) +}