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

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

当我为面向 API 的 public(本地主机)编写相同的查询时 我收到错误:

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)

由于我是 graphql 的新手,使用 graphql 和 Prisma 数据库, 当我为 demo-prisma 服务器编写查询和更改时,我能够查询和更改对象,但不能在我的 public 面向 api 的本地主机端点上为我的应用程序查询和更改对象!

下面是我的变异文件,它显示了一个错误:

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;

这也是我的 schema.graphql 文件

# 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]!
}

我认为我使用的语法有问题。 还有用什么ES6格式来写变异函数!

ctx.db.mutation不是ctx.db.mutations

您可能想切换到 prisma-client 而不是 prisma-binding,因为它类型更安全,因此您可以避免像这样的错误。

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