简体   繁体   English

TypeError:无法读取未定义的属性'createProduct'? (来自graphqlmutation.js文件)

[英]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 : 当我为面向公众的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)

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! 由于我是graphql的新手,可以使用graphql和Prisma数据库,因此我可以为demo-prisma服务器编写查询和突变而查询和突变对象,但不能在面向我的应用程序的公开API的localhost端点上查询和突变对象!

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 这也是我的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]!
}

I think there is something wrong with the syntax I am using. 我认为我使用的语法有问题。 Also what is the ES6 format to write the mutation function ! 还有什么写突变功能的ES6格式!

It is ctx.db.mutation not ctx.db.mutations . 它是ctx.db.mutation而不是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. 您可能想要切换到prisma-client而不是prisma-binding,因为它更加类型安全,因此可以避免此类错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM