ApolloServer.applyMiddleware({ express }) getting UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'use of undefined
The example in the docs is:
const app = express();server.applyMiddleware({ app });
This means you are passing an object to the applyMiddleware
. In the example, the object we pass in is initialized using shorthand property name notation, which was introduced with ES2015. The above is equivalent to:
server.applyMiddleware({ app: app });
Our object has a property named app, the value of which equals a variable that happens to also be called app. If you did this:
const myApp = express()server.applyMiddleware({ myApp });
That would mean you were passing in an object with a myApp
property, and more importantly, missing the app
property the applyMiddleware
function expects. So... your code needs to look like this:
apoloSrv.applyMiddleware({ app: server});
const express = require("express");const router = express.Router();const { ApolloServer, gql } = require('apollo-server-express');const server = new ApolloServer({ schema: schema, introspection: true}); server.applyMiddleware({ app:router });module.exports = router;