1. Why we moving API from REST to Graphql?
2. What is Graphql?
3. Graphql in Golang (Why we choose Golang)
4. How to testing Graphql in Golang
5. Deploy Graphql application
ABOUT ME
Open source contributor
1. Gitea
2. Drone
3. Gin
appleboy @ GitHub
appleboy @ twitter
appleboy @ slideshare
appleboy46 @ facebook
2
AGENDA
▸ Why we moving API from REST to Graphql?
▸ What is Graphql?
▸ Graphql in Golang (Why we choose Golang)
▸ How to testing Graphql in Golang
▸ Deploy Graphql application
/**
* @api {get} /scene/:id Request Scene information
* @apiName GetScene
* @apiGroup Scene
*
* @apiParam {Number} id Scenes unique ID.
*
* @apiSuccess {String} title Title of the Scene.
* @apiSuccess {String} desc Description of the Scene.
*/
// Schema is the GraphQL schema served by the server.
var Schema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: rootQuery,
Mutation: rootMutation,
Types: types,
})
Query and Mutation
type Scene struct {
ID int64 `xorm:"pk autoincr" json:"id"`
Image string `json:"image,omitempty"`
CreatedAt time.Time `json:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty"`
DeletedAt time.Time `xorm:"deleted"`
// reference
Items []*SceneItem `xorm:"-" json:"items,omitempty"`
User *User `xorm:"-" json:"user,omitempty"`
Role *SceneAccess `xorm:"-" json:"role,omitempty"`
}
Data Model
user role permission
"user": &graphql.Field{
Type: userType,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
o, ok := p.Source.(*model.Shorten)
if !ok {
return nil, errMissingSource
}
if o.User != nil {
return o.User, nil
}
return getUserFromLoader(p.Context, o.UserID)
},
},
exist in model?
fetch from loader
GET DATA FROM DATABASE IF NOT EXIST
func userBatch(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
var results []*dataloader.Result
id, _ := helper.GetCacheID(keys[0].String())
user, err := model.GetUserByID(id.(int64))
results = append(results, &dataloader.Result{
Data: user,
Error: err,
})
return results
}
fetch from DB