Is there any way of accessing the request object from the underlying express app in Apollo Server
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
The context configuration parameter can be either an object, a function that returns the object, or a function that returns a promise to return the object. This function would get the HTTP request as a parameter, and could be defined like so:
const apolloServer = new ApolloServer({
schema,
context: async ({ req }) => {
const something = getSomething(req)
return { something }
},
})
apolloServer.applyMiddleware({ app, path: '/graphql' })
const { appPort } = serviceFunc.getAccessData()
app.listen({ port: appPort }, () => {
console.log(`Express+Apollo Server on http://localhost:${appPort}/graphql`)
})
Thanks to Eugene eugene1g