graphql在node端使用的增删改操作
在这里所有的操作都在mutation里面实现,mutation与query同级!!!!!!
添加操作
let schema = new GraphQLSchema({
mutation:new GraphQLObjectType({
name:"graphqlMethod",
description:"graphql的方法",
fields:{
insert:{
type: new GraphQLObjectType({
name:"insertMovie",
description:"添加电影",
fields:{
title:{
type:GraphQLString
},
genres:{
type:GraphQLString
},
rating:{
type:GraphQLFloat
},
theater:{
type:GraphQLInt
}
}
}),
args:{
title:{
type:GraphQLNonNull(GraphQLString)
},
genres:{
type:GraphQLString
},
rating:{
type:GraphQLFloat
},
theater:{
type:GraphQLInt
}
},
resolve(obj, args) {
return axios.post("http://localhost:3000/subjects", { ...args })
.then((result) => {
return result.data
})
}
},
}
})
})
//语句
mutation insert{
insert(title:"英伦对决2",genres:"悬疑2",rating:8.8,theater:1){
title,
genres,
rating,
theater
}
}
修改①
let schema = new GraphQLSchema({
mutation:new GraphQLObjectType({
name:"graphqlMethod",
description:"graphql的方法",
fields:{
put:{
type: new GraphQLObjectType({
name:"puttMovie",
description:"更新电影",
fields:{
id:{
type:GraphQLID
},
title:{
type:GraphQLString
},
genres:{
type:GraphQLString
},
rating:{
type:GraphQLFloat
},
theater:{
type:GraphQLInt
}
}
}),
args:{
id:{
type:GraphQLNonNull(GraphQLInt)
},
title:{
type:GraphQLNonNull(GraphQLString)
},
genres:{
type:GraphQLString
},
rating:{
type:GraphQLFloat
},
theater:{
type:GraphQLInt
}
},
resolve(obj, args) {
return axios.put("http://localhost:3000/subjects/"+args.id, { ...args })
.then((result) => {
return result.data
})
}
},
}
})
})
//语句
mutation put{
put(id:4,title:"英伦对决",genres:"悬疑",rating:8.8,theater:1){
id,
title,
genres,
rating,
theater
}
}
修改②
let schema = new GraphQLSchema({
mutation:new GraphQLObjectType({
name:"graphqlMethod",
description:"graphql的方法",
fields:{
patch:{
type: new GraphQLObjectType({
name:"patchtMovie",
description:"更新电影",
fields:{
id:{
type:GraphQLID
},
title:{
type:GraphQLString
},
genres:{
type:GraphQLString
},
rating:{
type:GraphQLFloat
},
theater:{
type:GraphQLInt
}
}
}),
args:{
id:{
type:GraphQLNonNull(GraphQLInt)
},
title:{
type:GraphQLNonNull(GraphQLString)
},
genres:{
type:GraphQLString
},
rating:{
type:GraphQLFloat
},
theater:{
type:GraphQLInt
}
},
resolve(obj, args) {
console.log(args)
return axios.patch("http://localhost:3000/subjects/"+args.id, { ...args })
.then((result) => {
console.log(result)
return result.data
})
}
},
}
})
})
//语句
mutation patch{
put(id:4,title:"英伦对决2"){
id,
title,
genres,
rating,
theater
}
}
删除
let schema = new GraphQLSchema({
mutation:new GraphQLObjectType({
name:"graphqlMethod",
description:"graphql的方法",
fields:{
delete:{
type: new GraphQLObjectType({
name: 'MessageType',
fields:{
message:{
type:GraphQLString
}
}
}),
args: {
id: {
type: new GraphQLNonNull(GraphQLInt)
}
},
resolve(obj,args){
return axios.delete("http://localhost:3000/subjects/" + args.id)
.then((result) => {
return {
message: "数据修改成功."
}
})
}
},
}
})
})
//语句
mutation delete{
delete(id:3){
message
}
}