如何将客户端(window.location)传递给解析器

时间:2019-01-10 19:21:39

标签: javascript node.js react-apollo graphql-js

自从升级到React-Apollo V2 +以来,我一直难以访问客户端窗口位置。

在版本1上,该位置将在{ req }内部的服务器端解析器上可用。但是,由于升级到V2,{ req }仅包含/graphql端点的URL。

中间件的连接方式如下:

const server = new ApolloServer({
   schema,
   context: ({ req }) => ({ req }),
});

server.applyMiddleware({ app });

这是链接设置:

export default function getHttpLink(headers = {}, middleware = []) {

  return ApolloLink.from([...middleware, createHttpLink({
      uri: `http://localhost:${settings.getPort()}/graphql`,
      headers,
      cache: new InMemoryCache(),
    })]);

}

现在,在解析器中,我想使用客户端网址:


    export default {
       Query: {
           settings: (obj, args, { req }) => {
               if(req.path.includes('pie')) {
                   return fetch(pie)
               }
               return fetch(cake)
           }
       },
    };

我知道也可以通过查询args传递url值。但是,既然它在V1中可用,我想知道React-Apollo V2是否也支持这种功能?

从客户端URL读取GET参数并将其在解析器中使用也非常容易。有没有办法链接那些?

我尝试使用setContext,但这似乎对传入的解析程序请求没有任何作用。

0 个答案:

没有答案