大佬教程收集整理的这篇文章主要介绍了角度Apollo错误处理,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
基本上我所要做的就是当发生错误时提示我的用户有关问题.如果有人有一些替代npm包而不是apollo-link-error我都是耳朵.
尝试1:
export class AppModule { constructor (apollo: Apollo,httpLink: httpLink) { apollo.create({ link: httpLink.create({ uri: 'http://localhost:8080/graphql' }),cache: new InMemoryCache() }); const error = onError(({ networkError }) => { const networkErrorRef:httpErrorResponse = networkError as httpErrorResponse; if (networkErrorRef && networkErrorRef.status === 401) { console.log('Prompt User',error); } }); } }
尝试2:
export class AppModule { constructor (apollo: Apollo,cache: new InMemoryCache() }); const error = onError(({networkError}) => { if (networkError.status === 401) { console.log('Prompt User',error); } }); } }
尝试3:
export class AppModule { constructor (apollo: Apollo,httpLink: httpLink) { apollo.create({ link: httpLink.create({ uri: 'http://localhost:8080/graphql' }),cache: new InMemoryCache() }); 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}`); }); } }
这意味着您必须创建另一个结合了http和错误链接的apollo链接:
import { ApolloLink } from 'apollo-link'; import { httpLink } from 'apollo-link-http'; import { onError } from 'apollo-link-error'; ... const errorLink = onError(({ graphQLErrors,); if (networkError) console.log(`[Network error]: ${networkError}`); }); } } const httpLink = new httpLink({ uri: "http://localhost:8080/graphql" }); const httpLinkWithErrorHandling = ApolloLink.from([ errorLink,httpLink,]); apollo.create({ link: httpLinkWithErrorHandling,cache: new InMemoryCache() }); ...
以上是大佬教程为你收集整理的角度Apollo错误处理全部内容,希望文章能够帮你解决角度Apollo错误处理所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。