refactor: use GraphQL string scalar for KSUID transit
Marshal and unmarshal both use the base62 KSUID string, replacing the legacy JSON object wrapper and aligning gqlgen scalar behavior. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+5
-12
@@ -1,17 +1,13 @@
|
||||
package dbx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
|
||||
"github.com/segmentio/ksuid"
|
||||
)
|
||||
|
||||
type ksuidGQLTransport struct {
|
||||
KSUIDStr string `json:"ksuid_str"`
|
||||
}
|
||||
|
||||
func unmarshalKSUIDFromGQL(value any, unmarshal func([]byte) error) error {
|
||||
str, ok := value.(string)
|
||||
if !ok {
|
||||
@@ -20,14 +16,11 @@ func unmarshalKSUIDFromGQL(value any, unmarshal func([]byte) error) error {
|
||||
return unmarshal([]byte(str))
|
||||
}
|
||||
|
||||
// marshalKSUIDToGQL writes a JSON-encoded string scalar for gqlgen.
|
||||
func marshalKSUIDToGQL(w io.Writer, id ksuid.KSUID) {
|
||||
transport := ksuidGQLTransport{KSUIDStr: id.String()}
|
||||
data, err := json.Marshal(transport)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("GraphQL failed to JSON-marshal KSUID value: %s", err))
|
||||
}
|
||||
if _, err := w.Write(data); err != nil {
|
||||
panic(fmt.Errorf("GraphQL failed to write KSUID value: %s", string(data)))
|
||||
quoted := strconv.Quote(id.String())
|
||||
if _, err := io.WriteString(w, quoted); err != nil {
|
||||
panic(fmt.Errorf("GraphQL failed to write KSUID value: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user