因此,我要迁移到apollo-server-express 2.3.3(我使用的是1.3.6),我遵循了一些指南,进行了必要的调整,但陷入了CORS问题。
根据文档,您必须使用applyMiddleware函数通过express连接apollo服务器。
我目前正在执行以下操作:
const app = express();
// CORS configuration
const corsOptions = {
origin: 'http://localhost:3000',
credentials: true
}
app.use(cors(corsOptions))
// Setup JWT authentication middleware
app.use(async (req, res, next) => {
const token = req.headers['authorization'];
if(token !== "null"){
try {
const currentUser = await jwt.verify(token, process.env.SECRET)
req.currentUser = currentUser
} catch(e) {
console.error(e);
}
}
next();
});
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => ({ Property, User, currentUser: req.currentUser })
});
server.applyMiddleware({ …Run Code Online (Sandbox Code Playgroud)