forked from graph-gophers/graphql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an enum example (graph-gophers#587)
- Loading branch information
1 parent
25e8656
commit 04340de
Showing
7 changed files
with
241 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>GraphiQL</title> | ||
<style> | ||
body { | ||
height: 100%; | ||
margin: 0; | ||
width: 100%; | ||
overflow: hidden; | ||
} | ||
#graphiql { | ||
height: 100vh; | ||
} | ||
</style> | ||
<script src="https://unpkg.com/react@17/umd/react.development.js" integrity="sha512-Vf2xGDzpqUOEIKO+X2rgTLWPY+65++WPwCHkX2nFMu9IcstumPsf/uKKRd5prX3wOu8Q0GBylRpsDB26R6ExOg==" crossorigin="anonymous"></script> | ||
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" integrity="sha512-Wr9OKCTtq1anK0hq5bY3X/AvDI5EflDSAh0mE9gma+4hl+kXdTJPKZ3TwLMBcrgUeoY0s3dq9JjhCQc7vddtFg==" crossorigin="anonymous"></script> | ||
<link rel="stylesheet" href="https://unpkg.com/[email protected]/graphiql.min.css" /> | ||
</head> | ||
<body> | ||
<div id="graphiql">Loading...</div> | ||
<script src="https://unpkg.com/[email protected]/graphiql.min.js" type="application/javascript"></script> | ||
<script> | ||
ReactDOM.render( | ||
React.createElement(GraphiQL, { | ||
fetcher: GraphiQL.createFetcher({url: '/query'}), | ||
defaultEditorToolsVisibility: true, | ||
}), | ||
document.getElementById('graphiql'), | ||
); | ||
</script> | ||
</body> | ||
</html> |
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,57 @@ | ||
// Package main demonstrates a simple web app that uses type-safe enums in a GraphQL resolver. | ||
package main | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/graph-gophers/graphql-go" | ||
"github.com/graph-gophers/graphql-go/relay" | ||
) | ||
|
||
//go:embed index.html | ||
var page []byte | ||
|
||
//go:embed schema.graphql | ||
var sdl string | ||
|
||
type resolver struct { | ||
state State | ||
} | ||
|
||
func (r *resolver) Query() *queryResolver { | ||
return &queryResolver{state: &r.state} | ||
} | ||
|
||
func (r *resolver) Mutation() *mutationResolver { | ||
return &mutationResolver{state: &r.state} | ||
} | ||
|
||
type queryResolver struct { | ||
state *State | ||
} | ||
|
||
func (q *queryResolver) State(ctx context.Context) State { | ||
return *q.state | ||
} | ||
|
||
type mutationResolver struct { | ||
state *State | ||
} | ||
|
||
func (m *mutationResolver) State(ctx context.Context, args *struct{ State State }) State { | ||
*m.state = args.State | ||
return *m.state | ||
} | ||
|
||
func main() { | ||
opts := []graphql.SchemaOpt{graphql.UseStringDescriptions()} | ||
schema := graphql.MustParseSchema(sdl, &resolver{}, opts...) | ||
|
||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write(page) }) | ||
http.Handle("/query", &relay.Handler{Schema: schema}) | ||
|
||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} |
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,27 @@ | ||
type Query { | ||
""" | ||
Returns the current state. | ||
""" | ||
state: State! | ||
} | ||
|
||
type Mutation { | ||
""" | ||
Changes the state and returns the new state. | ||
""" | ||
state(state: State!): State! | ||
} | ||
|
||
""" | ||
State holds the possible task states. | ||
""" | ||
enum State { | ||
"Backlog indicates that a task is in the backlog and needs to be triaged." | ||
BACKLOG | ||
"Todo means that a task is ready to be worked on." | ||
TODO | ||
"INPROG means that a task is currently in progress." | ||
INPROG | ||
"Done means that a task is finished." | ||
DONE | ||
} |
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,52 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// State represents a type-safe enum in Go which corresponds to the State enum in the GraphQL schema. | ||
type State int | ||
|
||
const ( | ||
Backlog State = iota // default value | ||
TODO | ||
InProg | ||
Done | ||
) | ||
|
||
// the items in this array must have the exact same order as the corresponding constants above | ||
var states = [...]string{"BACKLOG", "TODO", "INPROG", "DONE"} | ||
|
||
func (s State) String() string { return states[s] } | ||
|
||
func (s *State) Deserialize(str string) { | ||
var found bool | ||
for i, st := range states { | ||
if st == str { | ||
found = true | ||
(*s) = State(i) | ||
} | ||
} | ||
if !found { | ||
panic("invalid value for enum State: " + str) | ||
} | ||
} | ||
|
||
// ImplementsGraphQLType instructs the GraphQL server that the State enum can be represented by the State type in Golang. | ||
// If this method is missing we would get a runtime error that the State type can't be assigned a string. However, since | ||
// this type implements the State enum, the server will try to call its [State.UnmarshalGraphQL] method to set a value. | ||
func (State) ImplementsGraphQLType(name string) bool { | ||
return name == "State" | ||
} | ||
|
||
// UnmarshalGraphQL tries to unmarshal a type from a given GraphQL value. | ||
func (s *State) UnmarshalGraphQL(input interface{}) error { | ||
var err error | ||
switch input := input.(type) { | ||
case string: | ||
s.Deserialize(input) | ||
default: | ||
err = fmt.Errorf("wrong type for State: %T", input) | ||
} | ||
return err | ||
} |
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
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