无法通过 graphql 突变从 sqlite 中删除项目
Cant delete item from sqlite by qraphql mutation
我正在按照 graphql 教程学习编写 apollo 服务器 https://www.howtographql.com/graphql-js/3-a-simple-mutation/ 但是当我尝试编写 delete mutation 并在 GraphQL Playground 中使用它时,执行后我得到 null,如果我检查Prisma studio 中的数据没有变化
mutation delete and server response
我确定还有很多要删除的项目以及我用于删除的当前 ID
这是我的代码
prisma studio data
const {ApolloServer} = require('apollo-server');
const fs = require('fs');
const path = require('path');
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
const resolvers = {
Query : {
info: () => 'Info text',
feed: async (parent, args, context, info) => {
return context.prisma.link.findMany()
},
},
Mutation : {
post: (parent, args, context, info) => {
const newLink = context.prisma.link.create({
data: {
url: args.url,
description: args.description,
},
})
return newLink
},
delete: (parent, args, context, info) => {
const deleteLink = context.prisma.link.delete({
data: {
id: args.id
}
})
return deleteLink
}
}
}
const server = new ApolloServer({
typeDefs: fs.readFileSync(
path.join(__dirname, 'schema.graphql'),
'utf8'
),
resolvers,
context: {
prisma,
}
})
server
.listen()
.then(({url})=>
console.log(`Server is running on ${url}`)
);
我明白了。相反 data
使用 where
。 Prisma 支持使用 where 查询选项进行过滤。
async function deleteLink(parent, args, context, info){
const deleteLink = context.prisma.link.delete({
where: {
id: +args.id,
}
})
}
我正在按照 graphql 教程学习编写 apollo 服务器 https://www.howtographql.com/graphql-js/3-a-simple-mutation/ 但是当我尝试编写 delete mutation 并在 GraphQL Playground 中使用它时,执行后我得到 null,如果我检查Prisma studio 中的数据没有变化
mutation delete and server response
我确定还有很多要删除的项目以及我用于删除的当前 ID 这是我的代码
prisma studio data
const {ApolloServer} = require('apollo-server');
const fs = require('fs');
const path = require('path');
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
const resolvers = {
Query : {
info: () => 'Info text',
feed: async (parent, args, context, info) => {
return context.prisma.link.findMany()
},
},
Mutation : {
post: (parent, args, context, info) => {
const newLink = context.prisma.link.create({
data: {
url: args.url,
description: args.description,
},
})
return newLink
},
delete: (parent, args, context, info) => {
const deleteLink = context.prisma.link.delete({
data: {
id: args.id
}
})
return deleteLink
}
}
}
const server = new ApolloServer({
typeDefs: fs.readFileSync(
path.join(__dirname, 'schema.graphql'),
'utf8'
),
resolvers,
context: {
prisma,
}
})
server
.listen()
.then(({url})=>
console.log(`Server is running on ${url}`)
);
我明白了。相反 data
使用 where
。 Prisma 支持使用 where 查询选项进行过滤。
async function deleteLink(parent, args, context, info){
const deleteLink = context.prisma.link.delete({
where: {
id: +args.id,
}
})
}