使用ExpressJS访问Apollo Server中的Request对象 [英] Accessing Request object in Apollo Server with ExpressJS

查看:65
本文介绍了使用ExpressJS访问Apollo Server中的Request对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以从Apollo Server中的基础Express应用程序访问请求对象

Is there any way of accessing the request object from the underlying express app in Apollo Server

推荐答案

上下文配置参数可以是对象,返回对象的函数或返回承诺返回对象的函数.该函数将HTTP请求作为参数,可以这样定义:

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

感谢 Eugene eugene1g

这篇关于使用ExpressJS访问Apollo Server中的Request对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆