Apollo客户端(Angular)中的两个端点



此处为初学者。当使用使用在执行";ng加apollo角";工作正常,当使用APOLO_NAMED_OPTIONS添加第二个时,我会收到两条神秘的错误消息。可能出了什么问题?

这是我的graphql.module.ts文件

import {APOLLO_OPTIONS, APOLLO_NAMED_OPTIONS} from 'apollo-angular';
import { InMemoryCache, ApolloLink } from '@apollo/client/core';
import {HttpLink} from 'apollo-angular/http';
const urium = 'https://some.url.here';       //changed for this post
const urips = 'https://some.other.one.here'; //changed for this post

export function createApollo(httpLink: HttpLink) {
return {
link: ApolloLink.from([httpLink.create({ uri: urium })]),
cache: new InMemoryCache()
}
}

export function createProjectspecApollo(httpLink: HttpLink) {
return {
name:'projectspec',
link: ApolloLink.from([httpLink.create({ uri: urips })]),
cache: new InMemoryCache()
}
}
@NgModule({
providers: [
{
provide: APOLLO_OPTIONS,
useFactory: createApollo,
deps: [HttpLink]
},{
provide: APOLLO_NAMED_OPTIONS,
useFactory: createProjectspecApollo,
deps: [HttpLink]
}
]
})
export class GraphQLModule {}

这里是GqlApoloInterfaceService,我在这里使用客户端

import { Apollo } from "apollo-angular";

@Injectable({
providedIn: 'root'
})
export class GqlApolloInterfaceService {

constructor(private apollo:Apollo) { }

}

最后,我的包.json

{
"name": "frontend",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~11.0.0",
"@angular/cdk": "^11.0.1",
"@angular/common": "~11.0.0",
"@angular/compiler": "~11.0.0",
"@angular/core": "~11.0.0",
"@angular/flex-layout": "^11.0.0-beta.33",
"@angular/forms": "~11.0.0",
"@angular/material": "^11.0.1",
"@angular/platform-browser": "~11.0.0",
"@angular/platform-browser-dynamic": "~11.0.0",
"@angular/router": "~11.0.0",
"@apollo/client": "^3.0.0",
"apollo-angular": "^2.1.0",
"apollo-angular-link-http": "^1.11.0",
"apollo-link-context": "^1.0.20",
"graphql": "^15.0.0",
"rxjs": "~6.6.0",
"tslib": "^2.0.0",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.1100.1",
"@angular/cli": "~11.0.1",
"@angular/compiler-cli": "~11.0.0",
"@types/jasmine": "~3.6.0",
"@types/node": "^12.11.1",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.6.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.1.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "^1.5.0",
"protractor": "~7.0.0",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~4.0.2"
}
}

我得到两个错误:第一个错误只显示在第一个,当我在提供者列表中添加第二个pbject时:

错误错误:未捕获(承诺中(:不变冲突:若要初始化Apollo客户端,必须在选项对象中指定"cache"属性

然后,如果我刷新页面,我会得到一个不同的页面:

错误错误:未捕获(承诺中(:错误:NG0200:为GqlApolloInterfaceService检测到DI中的循环依赖项错误:NG0200:为GqlApolloInterfaceService检测到DI中的循环依赖关系

我刚开始使用Apollo Client,所以它可能是一件非常简单的事情,然而,我得到的印象是,文档非常模糊,而且没有付出太多努力。可能会发生什么,最重要的是,我如何使它发挥作用?

问候!

尝试命名apollo客户端

apollo.create(option1, "endpoint 1")
apollo.create(option2, "endpoint 2")
apollo.use('endpoint 1').watchQuery({...});

参考https://apollo-angular.com/docs/recipes/multiple-clients/

备选方案,只是一个想法

而不是在UI中配置多个graphql端点使用Apollo联盟和Apollo服务器来配置多个graphql,这样UI将只有一个Apollo客户端。任何新的api/graphql都只在Apollo服务器中更改。无需更改UIhttps://www.apollographql.com/docs/federation/gateway/

我通过使用createApollo返回对象的链接atribute中的uri((函数解决了这个问题。我的情况是,一些查询/突变必须对一个端点进行,而另一些则必须对不同的端点进行。我的所有查询/突变都是作为服务实现的,如所示

首先,我制作了一个函数operationFilter,它通过检查操作名称来充当过滤器。然后,uri函数使用operationFilter在两个不同的端点之间进行选择

我的新graphql.module.ts看起来像这个

import {NgModule} from '@angular/core';
import {APOLLO_OPTIONS } from 'apollo-angular';
import { InMemoryCache, ApolloLink } from '@apollo/client/core';
import {HttpLink} from 'apollo-angular/http';
import { setContext } from '@apollo/client/link/context';
const urium = 'https://some.url.here';       //changed for this post
const urips = 'https://some.other.one.here'; //changed for this post

function operationFilter(operationName:string):boolean{  
if(...) return true;    //queries that should go to one endpoint
else return false;      //and the others
}
export function createApollo(httpLink: HttpLink) {
const auth = setContext((operation, context) => {
...add some header stuff, like jwt...
});
return {
link: ApolloLink.from(
[
auth, 
httpLink.create({
uri(operation){ 
return operationFilter(operation.operationName)? urium : urips;
} 
})
]),
cache: new InMemoryCache(),
defaultOptions:{query:{errorPolicy: "all"}}
} 
}

更多信息可以在这里找到

我想这不是最复杂的解决方案,但奏效了。如果有更干净的方法,我会很高兴知道的。

问候!

最新更新