繁体   English   中英

angular 中的apollo client for graphql 如何处理报错?

[英]How to handle error using apollo client in angular for graphql?

下面是我的代码。 我一直在尝试以某种方式将错误变量链接到 GraphQL 模块。 请提供实现该目标的方法。

import { ApolloModule, APOLLO_OPTIONS } from 'apollo-angular';
import { HttpLinkModule, HttpLink } from 'apollo-angular-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { onError } from 'apollo-link-error';

const uri = 'http://localhost:3000/graphql'; 

const error = onError(({ graphQLErrors, networkError }) => { // need help on linking this with graphql module
  if (graphQLErrors)
    graphQLErrors.map(({ message, locations, path }) =>
      console.log(
        `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
      ),
    );

  if (networkError) console.log(`[Network error]: ${networkError}`);
});

// <-- add the URL of the GraphQL server here
export function createApollo(httpLink: HttpLink) {
  return {
    link: httpLink.create({ uri }),
    cache: new InMemoryCache(),
  };
}

@NgModule({
  exports: [ApolloModule, HttpLinkModule],
  providers: [
    {
      provide: APOLLO_OPTIONS,
      useFactory: createApollo,
      deps: [HttpLink],
    },
  ],
})
export class GraphQLModule {}

我需要有关 GraphQL 模块链接错误的帮助。 我怎样才能做到这一点? 提前致谢。

Apollo 链接是可组合的单元。 每个链接都是一个 function,它将操作作为参数并返回一个可观察对象。 Apollo 链接将像中间件一样工作,可以转换请求及其结果。

您可以将错误链接组合到主模块作为

export function createApollo(httpLink: HttpLink) {
  return {
    link: error.concat(httpLink.create({ uri })),
    cache: new InMemoryCache(),
  };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM