its*_*old 8 node.js apollo reactjs graphql apollo-client
我有一个可以在过期时更新用户令牌的应用程序。我需要使用此令牌更新客户端以防止错误。我实际上无法将新令牌放入前端,因为似乎无法在响应中设置任何内容。我可以在后端使用Graphql和护照,如果有帮助的话。
我已经尝试过res.send,res.setheader等...
有谁知道如何从apollo服务器向apollo客户端发送新的标头条目?
我认为formatResponseApolloServer 构造函数的参数应该对您有帮助。这允许您在将来自服务器的响应发送到客户端应用程序之前对其进行处理。该函数接受GraphQLResponseandGraphQLRequestContext作为输入并返回 a GraphQLResponse。其界面GraphQLResponse如下:
export interface GraphQLResponse {
data?: Record<string, any> | null;
errors?: ReadonlyArray<GraphQLFormattedError>;
extensions?: Record<string, any>;
http?: Pick<Response, 'headers'> & Partial<Pick<Mutable<Response>, 'status'>>;
}
Run Code Online (Sandbox Code Playgroud)
这意味着您可以使用附加参数配置服务器以在将响应发送到客户端之前解析响应:
const myServer = new ApolloServer(
...,
formatResponse: (response, requestContext) => {
// then create a new response here with your additional headers and return it, for instance
const newResponse = { ...response, http: { response.http, { headers: ...response.http.headers, newHeader: newValue };
return newResponse;
}
Run Code Online (Sandbox Code Playgroud)
请注意,上面只是一个框架,您需要比这更彻底地检查响应对象,因为它的属性在很大程度上是可选的,但这应该给您要点。
有关更多详细信息,请参阅此处的文档
| 归档时间: |
|
| 查看次数: |
412 次 |
| 最近记录: |