如何将查询参数添加到Apollo-Server-express的上下文中

发布于 2025-01-22 10:55:18 字数 1746 浏览 1 评论 0 原文

我现在正在使用Apollo-Server-express:

import { ApolloServer } from 'apollo-server-express';
import { WebApp } from 'meteor/webapp';
import { getUser } from 'meteor/apollo';
import schema from './api';

const server = new ApolloServer({
  schema,
  context: async ({ req }) => {
    const user = await getUser(req.headers.authorization);

    return {
      user,
    };
  },
  playground: process.env.NODE_ENV === 'development',
  introspection: true,
  uploads: false,
});

server.applyMiddleware({
  app: WebApp.connectHandlers,
  path: '/graphql',
});

我想将另一个字段添加到上下文中,该字段具有我用户访问的URL的所有查询参数。

看来对象将上下文传递给 apollo-server-express 的功能是类型 req.query 对象。但是,当我尝试访问它时:

  context: async ({ req }) => {
    console.log('### query', req.query);
    console.log('### params', req.params);
    const user = await getUser(req.headers.authorization);

    return {
      user,
    };
  },

并通过http:// localhost访问我的应用程序:3000/u/u/3q2pcjrwyiqr2ywhm/bk2cig7fn3p7z5xvy?edittoken = qdj3rryjcumxfnrnz

我看到以下日志行:

I20220417-15:08:54.477(-5)? ### query {}
I20220417-15:08:54.478(-5)? ### params undefined
I20220417-15:08:54.570(-5)? ### query {}
I20220417-15:08:54.571(-5)? ### params undefine

在创建Apollo-Server-express上下文时,访问URL查询参数的正确方法是什么?我特别尝试将 deditkoken 添加到上下文中。

I'm using apollo-server-express as so right now:

import { ApolloServer } from 'apollo-server-express';
import { WebApp } from 'meteor/webapp';
import { getUser } from 'meteor/apollo';
import schema from './api';

const server = new ApolloServer({
  schema,
  context: async ({ req }) => {
    const user = await getUser(req.headers.authorization);

    return {
      user,
    };
  },
  playground: process.env.NODE_ENV === 'development',
  introspection: true,
  uploads: false,
});

server.applyMiddleware({
  app: WebApp.connectHandlers,
  path: '/graphql',
});

I'd like to add another field to the context which has all the query params from the URL my user is visiting.

It seems that the req object passed to the context function by apollo-server-express is of type express.Request which ought to have a req.query object. However, when I try to access that like so:

  context: async ({ req }) => {
    console.log('### query', req.query);
    console.log('### params', req.params);
    const user = await getUser(req.headers.authorization);

    return {
      user,
    };
  },

and visit my app at http://localhost:3000/u/3q2PcjRwyiqR2ywHM/BK2CiG7fN3P7Z5xvy?editToken=qdj3RRYjCuMxFNRnz (note the ?editToken=...)

I see the following log lines:

I20220417-15:08:54.477(-5)? ### query {}
I20220417-15:08:54.478(-5)? ### params undefined
I20220417-15:08:54.570(-5)? ### query {}
I20220417-15:08:54.571(-5)? ### params undefine

What is the correct way to access URL query params when creating the context for apollo-server-express? I'm specifically trying to add the editToken to the context.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瘫痪情歌 2025-01-29 10:55:18

也想知道。决定离开我的解决方案
在上下文中 - 不仅返回用户,还将用户obj添加到req(req.user =用户),然后返回req本身,在解析器中,您可以使用完整的req obj,

您也可以将edittoken添加到标题中,并以代码将其添加到代码中不太可见用户

,确保我们无法在您的Localhost上访问您的应用:)

Was wondering also. Decide to leave my solution
In context - return not only user, but add user obj to req (req.user = user) and return req itself and in resolvers you can use full req obj

Also you can add editToken to headers and get it in code not so visible for users

And sure we can't visit your app on your localhost :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文