graphql - URQL - GraphQL 如何获取响应标头
问题描述
当我对 GraphQL 服务器进行查询时,我想读取 http 响应标头中设置的信息。
当我使用 urql 客户端执行查询时,我只得到这些信息:
/** Resulting data from an [operation]{@link Operation}. */
export interface OperationResult<Data = any> {
/** The [operation]{@link Operation} which has been executed. */
operation: Operation;
/** The data returned from the Graphql server. */
data?: Data;
/** Any errors resulting from the operation. */
error?: CombinedError;
/** Optional extensions return by the Graphql server. */
extensions?: Record<string, any>;
/** Optional stale flag added by exchanges that return stale results. */
stale?: boolean;
}
有没有办法获取响应标头?
解决方案
您可以在提供的客户端(如果它对您的应用程序是全局的)中使用fetch
包装器,也可以在组件中的特定客户端中使用包装器,其fetch
选项如下:
const client = createClient({
url: `https://example.org/graphql`,
fetch: (...args) =>
fetch(...args).then((response) => {
const myHeader = response.headers.get('my-header');
if (myHeader) {
/* do stuff */
}
return response;
}),
fetchOptions: { credentials: 'include' },
…
});
推荐阅读
- firebase - 您可以等待 firebase cloud auth.user().OnCreate 函数吗?
- rust - 将 NixOS 升级到 20.03 后,具有柴油和 mysql 依赖项的板条箱不再针对 mariadb 进行编译
- apache-spark - 如何解决 Elephas 基本示例上的 java 错误?
- angular - 为什么 StackBlitz 中没有显示 mat-accordion?
- c++ - GCC 和 MSVC 中的 C++ utf-8 文字
- matlab - 如何使用 Matlab 分析和绘制二氧化碳排放数据
- gulp - 为什么 Gulp 以错误的顺序连接我的输出?
- c# - 如何将 Azure MSI AccessToken 添加到 EF6 DbContext 以连接 Azure SQL PaaS
- python-3.x - AWS Glue Python Shell 作业因 MemoryError 而失败
- vhdl - 在 VHDL 93 中对端口实体中的类型使用泛型