graphql - 使用 apollo graphql 客户端记录请求时间的正确方法是什么?

标签 graphql apollo-client

我正在对 React Native 应用程序进行一些应用程序分析,我发现使用 apollo-link-logger (以及我自己滚动的链接)和 Android 分析器的网络 channel 。对于探查器中约 600 毫秒的请求,我发现使用 apollo 链接系统的中间件需要 2 秒以上的时间。

我自己的链接(如下)中没有太多内容

const metricsLink = new ApolloLink((operation, forward) => {

    const { operationName } = operation
    const startTime = new Date().getTime()
    const observable = forward(operation)
    observable.subscribe({
        complete: () => {
            const elapsed = new Date().getTime() - startTime
            console.warn(`[METRICS][${operationName}] (${elapsed}) complete`)
        }
    })

    return observable
})

看起来它最终也会考虑到 apollo 管理这个请求链所需的时间。我通过直接使用 fetch 从其他端点获取并将这些时间与探查器时间(匹配)进行比较,确认这不是应用程序的普遍问题。

我应该期望这实际上反射(reflect)请求时间吗? native 网络请求和我在 apollo 客户端中看到的时间之间的其余时间可能会去哪里?

最佳答案

来自 Apollo Link docs ,有一个使用链接组合来测量时间的示例

注意:我已根据 this other SO answer 修改了示例以使用 performance.now() 而不是 new Date()

const timeStartLink = new ApolloLink((operation, forward) => {
  operation.setContext({ start: performance.now() })
  return forward(operation)
})

const logTimeLink = new ApolloLink((operation, forward) => {
  return forward(operation).map(data => {
    // data from a previous link
    const time = performance.now() - operation.getContext().start
    console.log(`operation ${operation.operationName} took ${time} to complete`)
    return data
  })
})

const link = timeStartLink.concat(logTimeLink)

关于graphql - 使用 apollo graphql 客户端记录请求时间的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56502738/

相关文章:

GraphQL 动态查询构建

angular - 带有插值的 graphql-codegen 动态场

reactjs - 如何从 Apollo set Context Http Link 访问 React 上下文

graphql - 如何在一个请求中使用不同的参数多次运行一个突变?

react-native - Apollo 客户端延迟授权 header

graphql - Android apollo 订阅不起作用?

javascript - GraphQL( Apollo ): Can you access directives within resolve function?

intellij-idea - Graphql-config无法识别Apollo Graphql @client指令

websocket - 带有 subscriptions-transport-ws 的 Rollup 和 Apollo websocket

reactjs - 大型react-apollo应用程序中片段组合的组织结构