"message": "Cannot read property 'query' of undefined",

"message": "Cannot read property 'query' of undefined",

我的环境目前是带有 prisma 的 graphql-yoga。尝试 运行 查询时,我遇到了 "Cannot read property 'query' of undefined" 错误。我假设这是因为它无法从上下文中读取它

这个问题是在我重构代码和设置 Prisma 连接时出现的。我在启动服务器时在上下文中包含了 prisma,但它似乎在任何地方都不可用。

错误

[nodemon] starting `babel-node src/index.js`
The server is up!
TypeError: Cannot read property 'query' of undefined
    at Object.query (G:\laragon\www\engine-backend\src\resolvers/Query.js:23:23)
    at G:\laragon\www\engine-backend\node_modules\graphql-tools\src\schemaGenerator.ts:695:54
    at next (G:\laragon\www\engine-backend\src\directives/directives.js:21:12)
    at field.resolve (G:\laragon\www\engine-backend\node_modules\graphql-tools\src\schemaGenerator.ts:692:18)
    at resolveFieldValueOrError (G:\laragon\www\engine-backend\node_modules\graphql\execution\execute.js:467:18)
    at resolveField (G:\laragon\www\engine-backend\node_modules\graphql\execution\execute.js:434:16)
    at executeFields (G:\laragon\www\engine-backend\node_modules\graphql\execution\execute.js:275:18)
    at executeOperation (G:\laragon\www\engine-backend\node_modules\graphql\execution\execute.js:219:122)
    at executeImpl (G:\laragon\www\engine-backend\node_modules\graphql\execution\execute.js:104:14)
    at Object.execute (G:\laragon\www\engine-backend\node_modules\graphql\execution\execute.js:64:63)

我试过谷歌搜索和搜索,但似乎找不到任何答案。

这是代码

// index.js
const { GraphQLServer } = require("graphql-yoga")
require('dotenv').config()
const { checkJwt } = require("./middleware/jwt")
const { getUser } = require("./middleware/getUser")
const { importSchema } = require("graphql-import")
const { makeExecutableSchema } = require("graphql-tools")
const { directiveResolvers } = require("./directives/directives")
import { resolvers } from './resolvers/index'
import { prisma } from './prisma/prisma'


const schema = makeExecutableSchema({
  typeDefs: importSchema("./src/schema.graphql"),
  resolvers,
  directiveResolvers
})



const server = new GraphQLServer({
  schema,
  context: req => ({
    ...req,
    prisma
  })
})

server.express.post(
  server.options.endpoint,
  checkJwt,
  (err, req, res, next) => {
    if (err) return res.status(401).send(err.message)
    next()
  }
)
server.express.post(server.options.endpoint, (req, res, next) =>
  getUser(req, res, next, prisma)
)

server.start({ port: process.env.PORT || 3000 }, () => {
  console.log('The server is up!')
})
//prisma/prisma.js
import { Prisma } from 'prisma-binding'
require('dotenv').config()
import { fragmentReplacements } from '../resolvers/index'

const prisma = new Prisma({
//   fragmentReplacements: extractFragmentReplacements(resolvers),
  typeDefs: "src/generated/prisma.graphql",
  endpoint: process.env.PRISMA_ENDPOINT,
  secret: process.env.PRISMA_SECRET,
//   debug: true,
  fragmentReplacements
})

export { prisma as default }
// resolvers/Query.js
const Query = {
    users (parent, args, { prisma }, info ) {
      prisma.query.users(null, info)
    },
    async leadPhones (parent, args, { prisma }, info) {
      return prisma.query.leadPhones(null, info)
    },

}
export { Query as default}

您正在将 Prisma 实例导出为默认导出:

export { prisma as default }

因此您应该将其作为默认值导入

import prisma from './prisma/prisma'

将其作为命名导出导入会导致结果变量等于 undefined。请参阅 here 以了解命名导出和默认导出之间的区别。