-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #282 from salman-ahmad/master
Use Struct Field Resolver instead of Method
- Loading branch information
Showing
8 changed files
with
438 additions
and
81 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,9 @@ | ||
### Social App | ||
|
||
A simple example of how to use struct fields as resolvers instead of methods. | ||
|
||
To run this server | ||
|
||
`go run ./example/field-resolvers/server/server.go` | ||
|
||
and go to localhost:9011 to interact |
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,62 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/graph-gophers/graphql-go" | ||
"github.com/graph-gophers/graphql-go/example/social" | ||
"github.com/graph-gophers/graphql-go/relay" | ||
) | ||
|
||
func main() { | ||
opts := []graphql.SchemaOpt{graphql.UseFieldResolvers(), graphql.MaxParallelism(20)} | ||
schema := graphql.MustParseSchema(social.Schema, &social.Resolver{}, opts...) | ||
|
||
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Write(page) | ||
})) | ||
|
||
http.Handle("/query", &relay.Handler{Schema: schema}) | ||
|
||
log.Fatal(http.ListenAndServe(":9011", nil)) | ||
} | ||
|
||
var page = []byte(` | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<link href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.11.11/graphiql.min.css" rel="stylesheet" /> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/4.1.1/es6-promise.auto.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.production.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.production.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.11.11/graphiql.min.js"></script> | ||
</head> | ||
<body style="width: 100%; height: 100%; margin: 0; overflow: hidden;"> | ||
<div id="graphiql" style="height: 100vh;">Loading...</div> | ||
<script> | ||
function graphQLFetcher(graphQLParams) { | ||
return fetch("/query", { | ||
method: "post", | ||
body: JSON.stringify(graphQLParams), | ||
credentials: "include", | ||
}).then(function (response) { | ||
return response.text(); | ||
}).then(function (responseBody) { | ||
try { | ||
return JSON.parse(responseBody); | ||
} catch (error) { | ||
return responseBody; | ||
} | ||
}); | ||
} | ||
ReactDOM.render( | ||
React.createElement(GraphiQL, {fetcher: graphQLFetcher}), | ||
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,206 @@ | ||
package social | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/graph-gophers/graphql-go" | ||
) | ||
|
||
const Schema = ` | ||
schema { | ||
query: Query | ||
} | ||
type Query { | ||
admin(id: ID!, role: Role = ADMIN): Admin! | ||
user(id: ID!): User! | ||
search(text: String!): [SearchResult]! | ||
} | ||
interface Admin { | ||
id: ID! | ||
name: String! | ||
role: Role! | ||
} | ||
scalar Time | ||
type User implements Admin { | ||
id: ID! | ||
name: String! | ||
email: String! | ||
role: Role! | ||
phone: String! | ||
address: [String!] | ||
friends(page: Pagination): [User] | ||
createdAt: Time! | ||
} | ||
input Pagination { | ||
first: Int | ||
last: Int | ||
} | ||
enum Role { | ||
ADMIN | ||
USER | ||
} | ||
union SearchResult = User | ||
` | ||
|
||
type page struct { | ||
First *float64 | ||
Last *float64 | ||
} | ||
|
||
type admin interface { | ||
ID() graphql.ID | ||
Name() string | ||
Role() string | ||
} | ||
|
||
type searchResult struct { | ||
result interface{} | ||
} | ||
|
||
func (r *searchResult) ToUser() (*user, bool) { | ||
res, ok := r.result.(*user) | ||
return res, ok | ||
} | ||
|
||
type user struct { | ||
IDField string | ||
NameField string | ||
RoleField string | ||
Email string | ||
Phone string | ||
Address *[]string | ||
Friends *[]*user | ||
CreatedAt graphql.Time | ||
} | ||
|
||
func (u user) ID() graphql.ID { | ||
return graphql.ID(u.IDField) | ||
} | ||
|
||
func (u user) Name() string { | ||
return u.NameField | ||
} | ||
|
||
func (u user) Role() string { | ||
return u.RoleField | ||
} | ||
|
||
func (u user) FriendsResolver(args struct{ Page *page }) (*[]*user, error) { | ||
var from int | ||
numFriends := len(*u.Friends) | ||
to := numFriends | ||
|
||
if args.Page != nil { | ||
if args.Page.First != nil { | ||
from = int(*args.Page.First) | ||
if from > numFriends { | ||
return nil, errors.New("not enough users") | ||
} | ||
} | ||
if args.Page.Last != nil { | ||
to = int(*args.Page.Last) | ||
if to == 0 || to > numFriends { | ||
to = numFriends | ||
} | ||
} | ||
} | ||
|
||
friends := (*u.Friends)[from:to] | ||
|
||
return &friends, nil | ||
} | ||
|
||
var users = []*user{ | ||
{ | ||
IDField: "0x01", | ||
NameField: "Albus Dumbledore", | ||
RoleField: "ADMIN", | ||
Email: "[email protected]", | ||
Phone: "000-000-0000", | ||
Address: &[]string{"Office @ Hogwarts", "where Horcruxes are"}, | ||
CreatedAt: graphql.Time{Time: time.Now()}, | ||
}, | ||
{ | ||
IDField: "0x02", | ||
NameField: "Harry Potter", | ||
RoleField: "USER", | ||
Email: "[email protected]", | ||
Phone: "000-000-0001", | ||
Address: &[]string{"123 dorm room @ Hogwarts", "456 random place"}, | ||
CreatedAt: graphql.Time{Time: time.Now()}, | ||
}, | ||
{ | ||
IDField: "0x03", | ||
NameField: "Hermione Granger", | ||
RoleField: "USER", | ||
Email: "[email protected]", | ||
Phone: "000-000-0011", | ||
Address: &[]string{"233 dorm room @ Hogwarts", "786 @ random place"}, | ||
CreatedAt: graphql.Time{Time: time.Now()}, | ||
}, | ||
{ | ||
IDField: "0x04", | ||
NameField: "Ronald Weasley", | ||
RoleField: "USER", | ||
Email: "[email protected]", | ||
Phone: "000-000-0111", | ||
Address: &[]string{"411 dorm room @ Hogwarts", "981 @ random place"}, | ||
CreatedAt: graphql.Time{Time: time.Now()}, | ||
}, | ||
} | ||
|
||
var usersMap = make(map[string]*user) | ||
|
||
func init() { | ||
users[0].Friends = &[]*user{users[1]} | ||
users[1].Friends = &[]*user{users[0], users[2], users[3]} | ||
users[2].Friends = &[]*user{users[1], users[3]} | ||
users[3].Friends = &[]*user{users[1], users[2]} | ||
for _, usr := range users { | ||
usersMap[usr.IDField] = usr | ||
} | ||
} | ||
|
||
type Resolver struct{} | ||
|
||
func (r *Resolver) Admin(ctx context.Context, args struct { | ||
ID string | ||
Role string | ||
}) (admin, error) { | ||
if usr, ok := usersMap[args.ID]; ok { | ||
if usr.RoleField == args.Role { | ||
return *usr, nil | ||
} | ||
} | ||
err := fmt.Errorf("user with id=%s and role=%s does not exist", args.ID, args.Role) | ||
return user{}, err | ||
} | ||
|
||
func (r *Resolver) User(ctx context.Context, args struct{ Id string }) (user, error) { | ||
if usr, ok := usersMap[args.Id]; ok { | ||
return *usr, nil | ||
} | ||
err := fmt.Errorf("user with id=%s does not exist", args.Id) | ||
return user{}, err | ||
} | ||
|
||
func (r *Resolver) Search(ctx context.Context, args struct{ Text string }) ([]*searchResult, error) { | ||
var result []*searchResult | ||
for _, usr := range users { | ||
if strings.Contains(usr.NameField, args.Text) { | ||
result = append(result, &searchResult{usr}) | ||
} | ||
} | ||
return result, 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
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
Oops, something went wrong.