“ message”:“无法读取未定义的属性”查询“,

时间:2019-08-30 01:16:08

标签: node.js express graphql prisma prisma-graphql

我的环境当前是带有方棱镜的graphql-yoga。尝试运行查询时,我遇到“无法读取未定义的属性'query'”错误。我认为这是因为它无法从上下文中读取它

当我重构代码并设置Prisma连接时,就会出现此问题。启动服务器时,我已经在上下文中包含了pyramida,但是似乎在任何地方都不可用。

错误

[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)

我已经尝试使用Google搜索和四处搜索,但似乎找不到任何答案。

这是代码

// 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}

1 个答案:

答案 0 :(得分:0)

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

export { prisma as default }

所以您应该将其导入为默认值

import prisma from './prisma/prisma'

将其作为命名导出导入将导致结果变量等于undefined。有关命名导出和默认导出之间的区别,请参见here