如何在 angular 中为 graphql 使用 apollo 客户端处理错误?
How to handle error using apollo client in angular for graphql?
下面是我的代码。我一直在尝试以某种方式 link 错误变量到 GraphQL Module 。请提供实现该目标的方法。
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 {}
我需要有关 linking GraphQL 模块错误的帮助。我怎样才能做到这一点?提前致谢。
Apollo links 是可组合单元。每个 link 都是一个函数,它将操作作为参数,returns 是一个可观察值。 Apollo links 将像中间件一样工作,可以转换请求及其结果。
您可以将错误 [=15=] 合并到主模块中作为
export function createApollo(httpLink: HttpLink) {
return {
link: error.concat(httpLink.create({ uri })),
cache: new InMemoryCache(),
};
}
下面是我的代码。我一直在尝试以某种方式 link 错误变量到 GraphQL Module 。请提供实现该目标的方法。
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 {}
我需要有关 linking GraphQL 模块错误的帮助。我怎样才能做到这一点?提前致谢。
Apollo links 是可组合单元。每个 link 都是一个函数,它将操作作为参数,returns 是一个可观察值。 Apollo links 将像中间件一样工作,可以转换请求及其结果。
您可以将错误 [=15=] 合并到主模块中作为
export function createApollo(httpLink: HttpLink) {
return {
link: error.concat(httpLink.create({ uri })),
cache: new InMemoryCache(),
};
}