Hi, im trying to do a delete with multiple paramet...
# orm-help
p
Hi, im trying to do a delete with multiple parameters. Seems kinda weird that the where field is only for unique columns. I have to do a query first to query the record. Then another query to delete the record itself? Is there a better eay to do this?
n
Hey @Pitakun šŸ‘‹ you're probably looking for
deleteMany
šŸ™‚ you can read more about using it in the docs here.
Let me know if that helps or if you have further questions, always happy to help šŸ™Œ
p
Hmm..feels weird to use that syntax when we have an id and another param...but the syntax is deleteMany
n
feels weird to use that syntax when we have an id and another param
Can you elaborate a bit on this use case? Why do you add another param if you already have an ID? (Since the ID already uniquely identifies a record, in my understanding there shouldn't be a need to add another param, but maybe I'm missing something? šŸ™‚)
p
Another question. id is an @id @unique When performing a mutation. Im passing a param { id: 2 }. However, on the mutation, the args.id gets it as a string. So a delete() or findUnique() needs to Number(args.id) everytime when using args.id.. Is there a way to avoid or simplify this?
n
I assume this question is in the context of a GraphQL server that you implement with Prisma? Can you maybe share the schema definition and the resolver implementation for the mutation that you are referring to? šŸ™‚ Are you working with JavaScript or TypeScript?
p
Copy code
///schema.prisma
model Link {
  id          Int      @id @default(autoincrement())
  createdAt   DateTime @default(now())
  description String
  url         String
  postedBy    User?    @relation(fields: [postedById], references: [id])
  postedById  Int?
}

model User {
  id       Int    @id @default(autoincrement())
  name     String
  email    String @unique
  password String
  links    Link[]
}

///schema.graphql
  type Mutation {
    post(url: String!, description: String!): Link!
    updateLink(id: ID!, url: String, description: String): Link
    deleteLink(id: ID!): Link
    signup(email: String!, password: String!, name: String!): AuthPayload
    login(email: String!, password: String!): AuthPayload
  }

///Mutation.js
updateLink = async (parent, args, context, info) => {
    const { userId } = context
    const link = await context.prisma.link.findFirst({ where: { AND: [{ id: Number(args.id) }, { postedBy: { id: userId } }] } })

    if (!link) {
        throw new Error('Cannot find link')
    }

    return await context.prisma.link.update({
        where: { id: Number(args.id) },
        data: {
            url: args.url || link.url,
            description: args.description || link.description
        }
    })
}
I think this code works for both questions. To clarify: • I just want the same user to be able to delete the Link
n
I'm not sure I can follow unfortunately. Do you mean the "only the user who created a link should be able to delete it" or what exactly do you mean by "I just want the same user to be able to delete the Link?" Also, is it intentional that you're showing the
updateLink
resolver but you're referring to deleting a link?
Were you able to figure it out @Pitakun? šŸ™‚
p
@nikolasburk yes i do want the user that created it to be allowed to delete.
Sry this is the snippet
Copy code
deleteLink = async (parent, args, context, info) => {
    const { userId } = context
    const link = await context.prisma.link.findFirst({ where: { AND: [{ id: Number(args.id) }, { postedBy: { id: userId } }] } })

    if (!link) {
        throw new Error('Cannot find link')
    }

    return await context.prisma.link.delete({ where: { id: Number(args.id) } })
}
n
I see, thanks for clarifying šŸ™‚ here's the resolver code that should work in this case:
Copy code
deleteLink = async (parent, args, context, info) => {
  const { userId } = context;
  const link = await context.prisma.link.findUnique({
    where: { id: Number(args.id) },
  });
  if (!link) {
    throw new Error("Cannot find link");
  }
  if (link.postedById !== userId) {
    throw new Error("Only the user who created a link can delete it.");
  }
  return await context.prisma.link.delete({ where: { id: Number(args.id) } });
};
šŸ‘ 1
As I mentioned, there's no need to include multiple params in the initial query if you already have the ID of the link, so I simplified the query to use
findUnique
instead of
findFirst
šŸ™‚
p
yeah this shld work too. tq
šŸ™Œ 1