【问题标题】:Accessing Request object in Apollo Server with ExpressJS使用 ExpressJS 访问 Apollo Server 中的 Request 对象
【发布时间】:2019-10-15 23:10:13
【问题描述】:

有什么方法可以从 Apollo Server 中的底层 express 应用访问请求对象

【问题讨论】:

    标签: express apollo apollo-server


    【解决方案1】:

    上下文配置参数可以是对象、返回对象的函数或返回返回对象的promise的函数。该函数将 HTTP 请求作为参数获取,可以这样定义:

    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`)
    })
    

    感谢Eugene eugene1g

    【讨论】:

      猜你喜欢
      • 2018-02-18
      • 2021-01-01
      • 2016-02-28
      • 2016-09-23
      • 2020-10-13
      • 2021-02-05
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多