Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
URQL-GraphQL如何获取响应头_Graphql_Urql - Fatal编程技术网

URQL-GraphQL如何获取响应头

URQL-GraphQL如何获取响应头,graphql,urql,Graphql,Urql,我想在查询GraphQL服务器时读取http响应头中的信息集 当我使用urql客户端执行查询时,我只得到以下信息: /** Resulting data from an [operation]{@link Operation}. */ export interface OperationResult<Data = any> { /** The [operation]{@link Operation} which has been executed. */ operat

我想在查询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;
}
/**来自[operation]{@link operation}的结果数据*/
导出接口操作结果{
/**已执行的[operation]{@link operation}*/
操作:操作;
/**从Graphql服务器返回的数据*/
数据?:数据;
/**操作导致的任何错误*/
错误?:组合错误;
/**Graphql服务器返回可选扩展*/
扩展?:记录;
/**由返回陈旧结果的交换添加的可选陈旧标志*/
陈旧?:布尔型;
}

有没有办法获取响应头?

简短的回答是,没有,
urql
在其默认行为中不公开请求的响应头,如图所示: 它简直被吞下了


答案很长,如果用自定义实现替换
fetchExchange
,则可以提取所需的任何信息。不幸的是,这显然是定制的,并且需要一些工作,但另一方面,
urql
允许您深入其内部并交换此行为。

简短的回答是,不,
urql
在其默认行为中不公开请求的响应头,如图所示: 它简直被吞下了


答案很长,如果用自定义实现替换
fetchExchange
,则可以提取所需的任何信息。不幸的是,这显然是定制的,需要做一些工作,但另一方面,
urql
允许您深入其内部,并将此行为交换出去。

您可以在提供的客户端中使用
获取
包装器(如果它对您的应用程序是全局的)或者在组件中的特定客户端上,使用如下
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' },
    …
  });


您可以在提供的客户端中使用
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' },
    …
  });