0

schema.prisma file I need help how can I update a post based on 2 conditions? The user id that I get from userId which belongs to user tab and post id which belongs to on other post table

updatePost: async (parant, args, context) => {
      const { userId } = context;
      const postExist = await context.prisma.post.findUnique({ 
        where: {
          id: args.id,
          author:{
            id: userId
          }
        } 
      })
     if (!postExist) {
      throw new Error ('post not found')
     }
      const updatedPost = await context.prisma.post.update({
      
        data:{
          ...args.data
        }
      })
      return updatedPost
    },

error code:

"message": "\nInvalid `context.prisma.post.findUnique()` invocation 

Argument where of type PostWhereUniqueInput needs exactly one argument, but you provided id and author. Please choose one. Available args: ",
            "type PostWhereUniqueInput {",
            "  id?: Int",
           "Unknown arg `author` in where.author for type PostWhereUniqueInput. Available args:",
            "",
            "type PostWhereUniqueInput {",
            "  id?: Int",
            "}",

amgoun
  • 1
  • 3
  • Hey, could you share a snippet of your Prisma schema? Alternatively, you can filter your data by using the `findFirst` method. – Alex Ruheni Aug 01 '22 at 07:31
  • @AlexRuheni https://i.stack.imgur.com/Crq9Y.png – amgoun Aug 01 '22 at 12:55
  • `type Mutation { updatePost(data: UpdatePosteInput, id: ID!): Post } input UpdatePostInput { title: String! thumbnailUrl: String! description: String }` – amgoun Aug 01 '22 at 13:42
  • 1
    [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/3404097) [Why are images of text, code and mathematical expressions discouraged?](https://meta.stackexchange.com/q/320052/266284) PS Please clarify via edits, not comments. [ask] [Help] PS [mre] – philipxy Aug 01 '22 at 14:13
  • @philipxy the edit not accept more code i i get the error " mutch code add more details" so i use the image, the important is to solve the issue – amgoun Aug 01 '22 at 14:52
  • 1
    SO is a repository of good Q&A. That is what is important. Not all questions are appropriate. A dump of a lot of wrong code does not make a good question. Debug questions requires a [mre]. Explain more in your post, inadequate explanations is one motivation for having that message. Please move clarifications from your comments into your post.l [ask] [Help] There is no problem having code this size. The message may be because you didn't format it correctly. – philipxy Aug 01 '22 at 20:27

1 Answers1

0

You should use context.prisma.post.findFirst()

context.prisma.post.findUnique will only accept fields with @unique field in your schema file. In this case, that is only your Post id.

SlothOverlord
  • 1,655
  • 1
  • 6
  • 16