[英]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.