Skip to content

Commit

Permalink
example/starwars: change Episode enum implementation to int type
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed May 29, 2018
1 parent 6ef2066 commit 3c5bc11
Showing 1 changed file with 53 additions and 17 deletions.
70 changes: 53 additions & 17 deletions example/starwars/starwars.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,47 @@ var Schema = `
union SearchResult = Human | Droid | Starship
`

type episode int

const (
newHopeEpisode episode = iota
empireEpisode
jediEpisode
)

var nameToEp = map[string]episode{
"NEWHOPE": newHopeEpisode,
"EMPIRE": empireEpisode,
"JEDI": jediEpisode,
}

var epToName = map[episode]string{
newHopeEpisode: "NEWHOPE",
empireEpisode: "EMPIRE",
jediEpisode: "JEDI",
}

func (e episode) String() string {
return epToName[e]
}

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

func (e *episode) UnmarshalGraphQL(input interface{}) error {
if str, ok := input.(string); ok {
*e = nameToEp[str]
return nil
}
return fmt.Errorf("wrong type for Episode: %T", input)
}

type human struct {
ID graphql.ID
Name string
Friends []graphql.ID
AppearsIn []string
AppearsIn []episode
Height float64
Mass int
Starships []graphql.ID
Expand All @@ -158,7 +194,7 @@ var humans = []*human{
ID: "1000",
Name: "Luke Skywalker",
Friends: []graphql.ID{"1002", "1003", "2000", "2001"},
AppearsIn: []string{"NEWHOPE", "EMPIRE", "JEDI"},
AppearsIn: []episode{newHopeEpisode, empireEpisode, jediEpisode},
Height: 1.72,
Mass: 77,
Starships: []graphql.ID{"3001", "3003"},
Expand All @@ -167,7 +203,7 @@ var humans = []*human{
ID: "1001",
Name: "Darth Vader",
Friends: []graphql.ID{"1004"},
AppearsIn: []string{"NEWHOPE", "EMPIRE", "JEDI"},
AppearsIn: []episode{newHopeEpisode, empireEpisode, jediEpisode},
Height: 2.02,
Mass: 136,
Starships: []graphql.ID{"3002"},
Expand All @@ -176,7 +212,7 @@ var humans = []*human{
ID: "1002",
Name: "Han Solo",
Friends: []graphql.ID{"1000", "1003", "2001"},
AppearsIn: []string{"NEWHOPE", "EMPIRE", "JEDI"},
AppearsIn: []episode{newHopeEpisode, empireEpisode, jediEpisode},
Height: 1.8,
Mass: 80,
Starships: []graphql.ID{"3000", "3003"},
Expand All @@ -185,15 +221,15 @@ var humans = []*human{
ID: "1003",
Name: "Leia Organa",
Friends: []graphql.ID{"1000", "1002", "2000", "2001"},
AppearsIn: []string{"NEWHOPE", "EMPIRE", "JEDI"},
AppearsIn: []episode{newHopeEpisode, empireEpisode, jediEpisode},
Height: 1.5,
Mass: 49,
},
{
ID: "1004",
Name: "Wilhuff Tarkin",
Friends: []graphql.ID{"1001"},
AppearsIn: []string{"NEWHOPE"},
AppearsIn: []episode{newHopeEpisode},
Height: 1.8,
Mass: 0,
},
Expand All @@ -211,7 +247,7 @@ type droid struct {
ID graphql.ID
Name string
Friends []graphql.ID
AppearsIn []string
AppearsIn []episode
PrimaryFunction string
}

Expand All @@ -220,14 +256,14 @@ var droids = []*droid{
ID: "2000",
Name: "C-3PO",
Friends: []graphql.ID{"1000", "1002", "1003", "2001"},
AppearsIn: []string{"NEWHOPE", "EMPIRE", "JEDI"},
AppearsIn: []episode{newHopeEpisode, empireEpisode, jediEpisode},
PrimaryFunction: "Protocol",
},
{
ID: "2001",
Name: "R2-D2",
Friends: []graphql.ID{"1000", "1002", "1003"},
AppearsIn: []string{"NEWHOPE", "EMPIRE", "JEDI"},
AppearsIn: []episode{newHopeEpisode, empireEpisode, jediEpisode},
PrimaryFunction: "Astromech",
},
}
Expand Down Expand Up @@ -282,18 +318,18 @@ type review struct {
commentary *string
}

var reviews = make(map[string][]*review)
var reviews = make(map[episode][]*review)

type Resolver struct{}

func (r *Resolver) Hero(args struct{ Episode string }) *characterResolver {
if args.Episode == "EMPIRE" {
func (r *Resolver) Hero(args struct{ Episode episode }) *characterResolver {
if args.Episode == empireEpisode {
return &characterResolver{&humanResolver{humanData["1000"]}}
}
return &characterResolver{&droidResolver{droidData["2001"]}}
}

func (r *Resolver) Reviews(args struct{ Episode string }) []*reviewResolver {
func (r *Resolver) Reviews(args struct{ Episode episode }) []*reviewResolver {
var l []*reviewResolver
for _, review := range reviews[args.Episode] {
l = append(l, &reviewResolver{review})
Expand Down Expand Up @@ -353,7 +389,7 @@ func (r *Resolver) Starship(args struct{ ID graphql.ID }) *starshipResolver {
}

func (r *Resolver) CreateReview(args *struct {
Episode string
Episode episode
Review *reviewInput
}) *reviewResolver {
review := &review{
Expand All @@ -374,7 +410,7 @@ type character interface {
Name() string
Friends() *[]*characterResolver
FriendsConnection(friendsConnectionArgs) (*friendsConnectionResolver, error)
AppearsIn() []string
AppearsIn() []episode
}

type characterResolver struct {
Expand Down Expand Up @@ -423,7 +459,7 @@ func (r *humanResolver) FriendsConnection(args friendsConnectionArgs) (*friendsC
return newFriendsConnectionResolver(r.h.Friends, args)
}

func (r *humanResolver) AppearsIn() []string {
func (r *humanResolver) AppearsIn() []episode {
return r.h.AppearsIn
}

Expand Down Expand Up @@ -455,7 +491,7 @@ func (r *droidResolver) FriendsConnection(args friendsConnectionArgs) (*friendsC
return newFriendsConnectionResolver(r.d.Friends, args)
}

func (r *droidResolver) AppearsIn() []string {
func (r *droidResolver) AppearsIn() []episode {
return r.d.AppearsIn
}

Expand Down

0 comments on commit 3c5bc11

Please sign in to comment.