Search code examples
ecmascript-6graphqles6-promiseprismaprisma-graphql

TypeError: Cannot read property 'createProduct' of undefined ? (from graphql mutation.js file)


When I write the same queries for my public facing API (localhost) I get the error :

TypeError: Cannot read property 'createProduct' of undefined
    at createProduct (/Users/gavish/Desktop/Final Beta/sick-fits/backend/src/resolvers/Mutation.js:5:42)
    at field.resolve (/Users/gavish/Desktop/Final Beta/sick-fits/backend/node_modules/graphql-extensions/lib/index.js:119:77)
    at resolveFieldValueOrError (/Users/gavish/Desktop/Final Beta/sick-fits/backend/node_modules/graphql/execution/execute.js:531:18)
    at resolveField (/Users/gavish/Desktop/Final Beta/sick-fits/backend/node_modules/graphql/execution/execute.js:495:16)
    at /Users/gavish/Desktop/Final Beta/sick-fits/backend/node_modules/graphql/execution/execute.js:339:18
    at /Users/gavish/Desktop/Final Beta/sick-fits/backend/node_modules/graphql/jsutils/promiseReduce.js:25:10
    at Array.reduce (<anonymous>)
    at promiseReduce (/Users/gavish/Desktop/Final Beta/sick-fits/backend/node_modules/graphql/jsutils/promiseReduce.js:22:17)
    at executeFieldsSerially (/Users/gavish/Desktop/Final Beta/sick-fits/backend/node_modules/graphql/execution/execute.js:336:38)
    at executeOperation (/Users/gavish/Desktop/Final Beta/sick-fits/backend/node_modules/graphql/execution/execute.js:289:55)

As I am new to graphql, working with graphql and Prisma database, I am able to query and mutate objects when I write queries and mutations for demo-prisma server but not on localhost endpoint of my public facing api for my app!

below is my mutation file for which it is showing an error:

const Mutations = {
    async createProduct(parent, args, ctx, info) {
        console.log('mutation started!')
        const product = await ctx.db.mutations.createProduct({
            data: {
                ...args
            }
        }, info)
        console.log('mutation done!')
        return product
    }
};

module.exports = Mutations;

also this is my schema.graphql file

# import * from './generated/prisma.graphql'

type Mutation {
  createProduct(
  id:ID
  name: String
  description: String
  price: Int
  colors: String
  quantity: Int ): Product!
}

type Query {
products:[Product]!
}

I think there is something wrong with the syntax I am using. Also what is the ES6 format to write the mutation function !


Solution

  • It is ctx.db.mutation not ctx.db.mutations.

    You might wanna switch to prisma-client instead of prisma-binding as it is more type-safe so that you can avoid mistakes like this one.

    https://www.prisma.io/docs/prisma-client/