Accessing Request object in Apollo Server with ExpressJS

蓝咒 提交于 2019-12-02 09:37:26

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!