javascriptnode.jsgraphqlprismaprisma-graphql

Prisma throws an error "TypeError: cannot read property findmany of undefined"


I wanted to make a chat app to practice working with graphql and node, for database I used prisma. I was doing everything like in this tutorial.

https://www.howtographql.com/graphql-js/0-introduction/

I just changed variable names.

so I have this code

const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()


const resolvers = {
  Query: {
    history: async (parent, args, context) => {
      return context.prisma.Messages.findMany()
    },
  },
  Mutation: {
    post: (parent, args, context) => {
      const newMessage = context.prisma.Messages.create({
        data: {
          username: args.username,
          message: args.message,
        },
      })
      return newMessage
    },
  },
}

const server = new GraphQLServer({
  typeDefs: './src/schema.graphql',
  resolvers,
  context: {
    prisma,
  }
})
server.start(() => console.log(`Server is running on http://localhost:4000`))

as my index.js

this is my schema.prisma

  provider = "sqlite"
  url      = "file:./dev.db"
}


generator client {
  provider = "prisma-client-js"
}


model Message {
  id       Int      @id @default(autoincrement())
  sendedAt DateTime @default(now())
  message  String
  username String
}

script.js

const { PrismaClient } = require("@prisma/client")


const prisma = new PrismaClient()


async function main() {
  const newMessage = await prisma.Messages.create({
    data: {
      message: 'Fullstack tutorial for GraphQL',
      username: 'www.howtographql.com',
    },
  })
  const allMessages = await prisma.Messages.findMany()
  console.log(allMessages)
}


main()
  .catch(e => {
    throw e
  })
  // 5
  .finally(async () => {
    await prisma.disconnect()
  })

and schema.graphql

type Query {
  history: [Message!]!
}

type Mutation {
  post(username: String!, message: String!): Message!
}

type Message {
  id: ID!
  message: String!
  username: String!
}

and that is what i got in my playground

  "data": null,
  "errors": [
    {
      "message": "Cannot read property 'findMany' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "history"
      ]
    }
  ]
}

please help


Solution

  • It should be noted it is not actually lowercase but camel case.

    Example:

    Model name: Message -> Prisma client name: message

    Model name: MessagePerUser -> Prisma client name: messagePerUser