I am trying to figure out how to make a delete mutation in my app.
I have an Issue Service, Issue Resolver, and Issue Form.
The service has:
import { prisma } from "../../lib/prisma"
import { Service } from "typedi"
import { IssueInput } from "./inputs/create.input"
import { Resolver } from "type-graphql"
import { Issue } from "./issue.model"
@Service()
@Resolver(() => Issue)
export class IssueService {
async createIssue(data: IssueInput) {
return await prisma.issue.create({ data })
}
async getAllIssues() {
return await prisma.issue.findMany()
}
async getIssue(id: string) {
return await prisma.issue.findUnique({ where: { id } })
}
async deleteIssue(id: string) {
return await prisma.issue.delete({ where: { id } })
}
}
The resolver has:
import { Arg, Mutation, Query, Resolver } from "type-graphql"
import { Issue } from "./issue.model"
import { IssueService } from "./issue.service"
import { IssueInput } from "./inputs/create.input"
import { Inject, Service } from "typedi"
@Service()
@Resolver(() => Issue)
export default class IssueResolver {
@Inject(() => IssueService)
issueService: IssueService
@Query(() => [Issue])
async allIssues() {
return await this.issueService.getAllIssues()
}
@Query(() => Issue)
async issue(@Arg("id") id: string) {
return await this.issueService.getIssue(id)
}
@Mutation(() => Issue)
async createIssue(@Arg("data") data: IssueInput) {
return await this.issueService.createIssue(data)
}
@Mutation(() => Issue)
async deleteIssue(@Arg("id") id: string) {
return await this.issueService.deleteIssue(id)
}
}
My lib/graphql sort of recognises that I tried to make the mutations. It has entries as follows:
export type Mutation = {
__typename?: 'Mutation';
createIssue: Issue;
deleteIssue: Issue;
export type MutationDeleteIssueArgs = {
id: Scalars['String'];
};
Apart from the above two references, the lib/graphql does not have something that looks like the other mutations and queries.
In the issue form, I want to try and use the delete mutation, so I tried:
import {
IssueInput,
useAllIssuesQuery,
useCreateIssueMutation,
useDeleteIssueMutation, ## this is not recognised as an exported member of lib/graphql
} from "lib/graphql"
How can I add it to the lib/graphql. Whatever the process for generating the mutation was has obviously run, but I don't understand why it didn't make something I can use in the form.
My goal is to try and figure out how to write something along the following lines to try and delete an entry.
{ onDelete: async (id) => {
await fetch("api/issue", { method: "delete" }),
toast({
title: "Issue deleted",
})
refetch()
refetchClimate()
}
}