当我向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;
}有没有办法获取响应头?
发布于 2020-05-20 07:57:43
简短的回答是,不,urql在其默认行为中不会公开请求的响应头,如下所示:https://github.com/FormidableLabs/urql/blob/master/packages/core/src/internal/fetchSource.ts#L23-L29它只是被吞噬了。
长篇大论的答案是,如果您用自定义实现替换fetchExchange,则可以提取所需的任何信息。不幸的是,这显然是自定义的和一些工作,但另一方面,urql允许您深入其内部并交换这种行为。
发布于 2021-02-19 20:51:02
您可以在提供的客户端中使用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' },
…
});https://stackoverflow.com/questions/61388188
复制相似问题