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

Two endpoints in Apollo Client (Angular)

这里是初学者。在使用执行“ng add apollo-angular”时创建的默认结构使用单个端点时工作正常,当使用 APOLLO_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 {}

这里是GqlApolloInterfaceService,我用的是客户端

import { Apollo } from "apollo-angular";



@Injectable({
  providedIn: 'root'
})
export class GqlApolloInterfaceService {
 
  constructor(private apollo:Apollo) { }
  
}

最后,我的package.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 时:

ERROR 错误:未捕获(承诺):不变违规:要初始化 Apollo Client,您必须在选项对象中指定 '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 服务器中进行。无需更改 UI https://www.apollographql.com/docs/federation/gateway/

我通过使用 createApollo 的 return 对象的 link 属性中的 uri() 函数解决了这个问题。我的情况是,一些 queries/mutations 必须对一个端点完成,而另一些必须对另一个端点完成。我所有的 queries/mutations 都是作为服务实现的 here

首先,我创建了一个函数 operationFilter,它通过检查操作名称充当过滤器。然后,uri函数在两个不同的端点

之间使用operationFilter到select

我的新 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"}}
    } 
}

可以找到更多信息here

我想这不是最复杂的解决方案,但有效。如果有更简洁的方法来执行此操作,我会很高兴。

此致!