"message": "无法读取未定义的属性 'query'",

Posted

技术标签:

【中文标题】"message": "无法读取未定义的属性 \'query\'",【英文标题】:"message": "Cannot read property 'query' of undefined","message": "无法读取未定义的属性 'query'", 【发布时间】:2020-01-03 06:30:57 【问题描述】:

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

这个问题是在我重构代码和设置 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

【问题讨论】:

【参考方案1】:

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

export  prisma as default 

所以你应该将它作为默认导入

import prisma from './prisma/prisma'

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

【讨论】:

谢谢,这确实消除了该错误。现在它似乎抛出了一个格式相当奇怪的错误 - paste.pics/d102aa54281cafec33fb4521bfd3d598 好的,这是我的 .env 中带有 Prisma url 的错误。一切都按预期工作。感谢您的帮助

以上是关于"message": "无法读取未定义的属性 'query'",的主要内容,如果未能解决你的问题,请参考以下文章

鼠标后的jquery滚动

记一次Maven发布Jar包中文乱码解决方法

java.security.AccessControlException:access denied("java.io.FilePermission" "文件路径&quo

[py]python3编码-接口返回乱码

Error in read.dcf(file.path(pkgname, "DESCRIPTION"), c("Package", "Type&quo

android gson怎么解析这样的json数据 "code":"00","result":"corpid":&quo