-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change moves the ID scalar implementation out of `graphql.go` and into its own file `id.go` for consistency with the Time scalar implementation.
- Loading branch information
Showing
2 changed files
with
30 additions
and
23 deletions.
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package graphql | ||
|
||
import ( | ||
"errors" | ||
"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 = errors.New("wrong type") | ||
} | ||
return err | ||
} | ||
|
||
func (id ID) MarshalJSON() ([]byte, error) { | ||
return strconv.AppendQuote(nil, string(id)), nil | ||
} |