【发布时间】:2022-12-29 08:38:36
【问题描述】:
我试图在我的角度应用程序上使用两个不同的 apollo 客户端,但出现以下错误:
Error: Uncaught (in promise): Error: Client has not been defined yet
我的graphql.模块.ts设置为按名称处理两个不同的客户端(“auth”和“default”):
const authUri = 'http://localhost:4000/graphql/auth';
const defaultUri = 'http://localhost:4000/graphql';
export function createDefaultApollo(httpLink: HttpLink): NamedOptions {
return {
default: {
// name: 'default',
link: httpLink.create({ uri: defaultUri }),
cache: new InMemoryCache({
typePolicies: {
Quotes: {
keyFields: ['_id'],
fields: {
allQuotes: {
merge: true,
},
},
},
},
}),
},
auth: {
// name: 'auth',
link: httpLink.create({ uri: authUri }),
cache: new InMemoryCache(),
},
};
}
@NgModule({
exports: [ApolloModule],
providers: [
{
provide: APOLLO_NAMED_OPTIONS,
useFactory: createDefaultApollo,
deps: [HttpLink],
},
],
})
export class GraphQLModule {}
然后,我导入GraphQL模块在 AppModule 上。我猜这是与延迟加载相关的某种问题,因为第二个客户端(称为“auth”)工作正常(它是应用程序上加载的第一个模块)。但是,第一个客户端接下来加载了其他模块,我收到了错误。
注意:为了处理我服务的客户,我使用:
return this._apollo.use('auth')
.watchQuery<LoginApiResponse>({
query,
variables,
})
【问题讨论】:
标签: angular graphql lazy-loading apollo-client