id.go 567 B

123456789101112131415161718192021222324252627282930
  1. package graphql
  2. import (
  3. "errors"
  4. "strconv"
  5. )
  6. // ID represents GraphQL's "ID" scalar type. A custom type may be used instead.
  7. type ID string
  8. func (ID) ImplementsGraphQLType(name string) bool {
  9. return name == "ID"
  10. }
  11. func (id *ID) UnmarshalGraphQL(input interface{}) error {
  12. var err error
  13. switch input := input.(type) {
  14. case string:
  15. *id = ID(input)
  16. case int32:
  17. *id = ID(strconv.Itoa(int(input)))
  18. default:
  19. err = errors.New("wrong type")
  20. }
  21. return err
  22. }
  23. func (id ID) MarshalJSON() ([]byte, error) {
  24. return strconv.AppendQuote(nil, string(id)), nil
  25. }