6

下面是我的apollo angular设置代码:

providers: [
    {
      provide: APOLLO_OPTIONS,
      useFactory: (httpLink: HttpLink) => {
        return {
          cache: new InMemoryCache(),
          link: httpLink.create({
            uri: AppSettings.API_ENDPOINT

          })
        }
      },
      deps: [HttpLink]
    }
  ],

我想在下面使用:

const link = onError(({ graphQLErrors, networkError }) => {
  if (graphQLErrors)
    graphQLErrors.map(({ message, locations, path }) =>
      console.log(
        `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
      ),
    );

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

我怎样才能链接它,以便我能够在控制台中得到错误?

4

2 回答 2

4

以下是我如何使用“apollo-link-error”来处理 graphQL 错误和网络错误。我创建了一个名为apollo-clients-module.ts的单独模块并将其导入 app.module.ts 和我拥有的其他功能模块。

apollo-clients-module.ts 代码:

import { NgModule } from "@angular/core";
import { HttpLink } from "apollo-angular-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";
import { Apollo, ApolloModule } from "apollo-angular";
import { ApolloLink } from 'apollo-link';
import { onError } from "apollo-link-error";

@NgModule({
  declarations: [],
  imports: [ApolloModule]
})
export class ApolloClientsModule {

  constructor(private apollo: Apollo, private httpLink: HttpLink) {

    const link = onError(({ graphQLErrors, networkError }) => {
      if(graphQLErrors){
        graphQLErrors.map(({ message, locations, path }) => {
          // Here you may display a message to indicate graphql error
          // You may use 'sweetalert', 'ngx-toastr' or any of your preference
          console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
        })
      }
      if(networkError){
          // Here you may display a message to indicate network error
          console.log(`[Network error]: ${networkError}`);
      }
    });

    apollo.create(
      {
        link : ApolloLink.from([link, httpLink.create({ uri: "/end-point-uri-goes-here/graphql" })]) ,
        cache: new InMemoryCache(),
        defaultOptions : {
          watchQuery : {
            fetchPolicy : 'network-only',
            errorPolicy : 'all'
          }
        }
      },
      "default"
    );  
  }

}

记得'npm install apollo-link-error'

访问https://www.apollographql.com/docs/angular/features/error-handling/了解更多信息。

于 2019-09-03T08:35:07.183 回答
0

我昨天才开始工作,所以没有伟大的专家,但我认为它可以满足您的要求。我在网上也找不到很好的例子。

这将触发一个关于您的视图的对话框,其中包含您想要的任何消息。我没有包含对话代码,但它存在于我的 messagesService 中。

请注意,我将 InMemoryCache 留在那里作为诸如不需要它的响应的注释。它包含在 Boost 中并自动设置。我能够用 readQuery 阅读它。

我选择了 Boost,并将它放在导出类的 app.module 中,因为我无法让它在模块提供程序中工作。最初我像你一样设置,但到达 messagesService 没有成功。它也可以是一项服务,我可以将它移到那里。这是全球性的,您无需在组件中执行任何操作。非常好!

app.module.ts

export class AppModule {

// Create and setup the Apollo server with error catching globally.
  constructor(
      private messagesService: MessagesService,
      private apollo: ApolloBoost,
  ) {
    apollo.create({
      uri: 'http://localhost:3000/graphql',
      // cache: new InMemoryCache(),  // This is included by default.  Can be modified.
      onError: ({ graphQLErrors, networkError }) => {
        if (graphQLErrors) {
          graphQLErrors.map(({ message, locations, path }) =>
              console.log(
                  `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`),
              console.log('This is a graphQL error!'),
          );
          const msg1 = 'GraphQL error';
          const msg2 = 'Please contact support.';
          this.handleError(msg1, msg2)
        }
        if (networkError) {
          console.log('This is a Network Error!', networkError);
          console.log('Can be called from a query error in the browser code!');
          const msg1 = 'Network error';
          const msg2 = 'Please check your Internet connection.  If OK then contact support .';
          this.handleError(msg1, msg2)
        }
      }
    });
  }


  public handleError(msg1, msg2) {
    this.messagesService.openDialog(msg1, msg2);
  }

}
于 2019-08-17T15:55:59.727 回答