My environment is currently graphql-yoga with prisma. When attempting to run queries I am faced with "Cannot read property 'query' of undefined" error. I am assuming this is because it cannot read it from the context
This issue came about when I was refactoring the code and setting up the Prisma connection. I have included prisma in the context when starting the server, but it does not seem to be available anywhere.
error
[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)
I have tried googling and searching around but it seems that I cannot find any answer.
Here is the code
// 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}