0

当我为面向公众的 API (localhost) 编写相同的查询时,我收到错误:

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 服务器编写查询和变异时,我能够查询和变异对象,但不能在我的应用程序面向公众的 api 的 localhost 端点上!

下面是我的突变文件,它显示错误:

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格式来写变异函数!

4

1 回答 1

2

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

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

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

于 2019-04-08T16:18:03.487 回答