summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/graph-gophers/graphql-go/id.go
blob: 80bdac906bb5a2e0f2f5a4ae1b8d765934a3a569 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package graphql

import (
	"fmt"
	"strconv"
)

// ID represents GraphQL's "ID" scalar type. A custom type may be used instead.
type ID string

func (ID) ImplementsGraphQLType(name string) bool {
	return name == "ID"
}

func (id *ID) UnmarshalGraphQL(input interface{}) error {
	var err error
	switch input := input.(type) {
	case string:
		*id = ID(input)
	case int32:
		*id = ID(strconv.Itoa(int(input)))
	default:
		err = fmt.Errorf("wrong type for ID: %T", input)
	}
	return err
}

func (id ID) MarshalJSON() ([]byte, error) {
	return strconv.AppendQuote(nil, string(id)), nil
}