简体   繁体   English

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

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

Below is my code.下面是我的代码。 I have been trying to somehow link the error variable to GraphQL Module.我一直在尝试以某种方式将错误变量链接到 GraphQL 模块。 Kindly provide a way to achieve that.请提供实现该目标的方法。

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 {}

I need help on linking error with GraphQL Module.我需要有关 GraphQL 模块链接错误的帮助。 How can I do that?我怎样才能做到这一点? Thanks in advance.提前致谢。

Apollo links are composable units. Apollo 链接是可组合的单元。 Each link is a function that takes the operation as an argument and returns an observable.每个链接都是一个 function,它将操作作为参数并返回一个可观察对象。 Apollo links will work just like a middleware that might transform a request and its result. Apollo 链接将像中间件一样工作,可以转换请求及其结果。

You can combine the error link to the main module as您可以将错误链接组合到主模块作为

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

暂无
暂无

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

相关问题 如何使用Apollo客户端库创建带有angular参数的graphql查询 - How to create a graphql query with arguments in angular using apollo client library 使用 graphql 和 apollo 客户端刷新 angular 的令牌 - refresh token for angular using graphql and apollo client 如何使用graphql为angular2 + apollo-client创建服务 - How to create service for angular2 + apollo-client with graphql 测试Angular Effect + graphql apollo客户端 - Testing Angular Effect + graphql apollo client Graphql - 使用 Apollo 客户端 Angular 的突变操作不起作用 - Graphql - Mutation operation with Apollo Client Angular not working 使用 GraphQl 的 Apollo 客户端时如何在 watchQuery 中使用加载属性 - How to use the loading property in a watchQuery when using the Apollo client for GraphQl 如何在Angular 5中使用GET方法使用Apollo graphql查询 - how to use apollo graphql query using GET method in angular 5 在客户端解析器中导入类型时,如何避免使用 Apollo 客户端和 `graphql-codegen` 的角度项目中的循环依赖? - How to avoid circular dependency in angular project with Apollo client and `graphql-codegen` when importing types in client resolver? GraphQL angular 阿波罗客户端。 从缓存中读取抛出错误,但对服务器的相同查询工作正常 - GraphQL angular apollo-client. Read from cache throw error but the same query to the server works fine Apollo Angular Graphql,如何使用多个端点? - Apollo Angular Graphql, How to use multiple enpoints?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM