GraphQL中有三种操作类型,分别是query、mutation、subscription:
- query: 获取数据,对应CRUD里的R;
- mutation: 操作数据,对应CRUD里的CUD(创建,更新,删除);
- subscription: 消息订阅,数据更改时进行消息推送.
之前已经学习了query, 这一篇重点学习mutation.
首先,修改models/article.go文件,增加相应的CUD函数:
// Create a new article with the title and content provided
func CreateNewArticle(title, content string) (*Article, error) {
// Set the ID of a new article to one more than the number of articles
a := Article{ID: len(articleList) + 1, Title: title, Content: content}
// Add the article to the list of articles
articleList = append(articleList, a)
return &a, nil
}
// Update a article
func UpdateArticle(id int, title, content string) (*Article, error) {
a, err := GetArticleByID(id)
if err != nil {
return nil, err
}
if title != "" {
a.Title = title
}
if content != "" {
a.Content = content
}
return a, nil
}
// Delete a article
func DeleteArticle(id int) error {
for k, a := range articleList {
if a.ID == id {
articleList = append(articleList[:k], articleList[k+1:]...)
return nil
}
}
return errors.New("Article not found")
}
然后是修改schema/article.go,增加相应的mutation:
// 定义mutation,增删改操作
// add
var addArticle = graphql.Field{
Name: "新文章",
Description: "增加新文章",
Type: articleType,
Args: graphql.FieldConfigArgument{
"title": &graphql.ArgumentConfig{
Type: graphql.String,
},
"content": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: func(p graphql.ResolveParams) (result interface{}, err error) {
title, _ := p.Args["title"].(string)
content, _ := p.Args["content"].(string)
result, err = models.CreateNewArticle(title, content)
if err != nil {
return nil, err
}
return result, nil
},
}
// update
var updateArticle = graphql.Field{
Name: "编辑文章",
Description: "编辑文章",
Type: articleType,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.Int,
},
"title": &graphql.ArgumentConfig{
Type: graphql.String,
},
"content": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: func(p graphql.ResolveParams) (result interface{}, err error) {
id, _ := p.Args["id"].(int)
title, _ := p.Args["title"].(string)
content, _ := p.Args["content"].(string)
result, err = models.UpdateArticle(id, title, content)
if err != nil {
return nil, err
}
return result, nil
},
}
// delete
var deleteArticle = graphql.Field{
Name: "删除文章",
Description: "删除指定Id的文章",
Type: articleType,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.Int,
},
},
Resolve: func(p graphql.ResolveParams) (result interface{}, err error) {
id, _ := p.Args["id"].(int)
// 查找文章是否存在
result, err = models.GetArticleByID(id)
if err != nil {
return nil, err
}
if err = models.DeleteArticle(id); err != nil {
return nil, err
}
return result, nil
},
}
// 定义增删改方法
var mutationType = graphql.NewObject(graphql.ObjectConfig{
Name: "mutation",
Description: "增删改",
Fields: graphql.Fields{
"add": &addArticle,
"update": &updateArticle,
"delete": &deleteArticle,
},
})
// 定义Schema用于http handler处理
var Schema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: rootQuery,
Mutation: mutationType, // 这里不能是nil了,对应前面的定义
})
测试结果话不多说,上图:
新增文章:
编辑文章
删除文章
代码在github上的链接
传送门