【问题标题】:Two endpoints in Apollo Client (Angular)Apollo Client (Angular) 中的两个端点
【发布时间】:2021-03-17 00:33:50
【问题描述】:

这里是初学者。虽然使用执行“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 客户端,您必须在选项对象中指定“缓存”属性。

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

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

我刚开始使用 Apollo Client,所以它可能非常简单,但是,我的印象是文档非常模糊,并且没有付出太多努力来制作它。会发生什么,最重要的是,我怎样才能让它发挥作用?

问候!

【问题讨论】:

    标签: angular apollo-client apollo-angular


    【解决方案1】:

    尝试命名为 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 federation 和 Apollo server 来配置多个 graphql,这样 UI 将只有一个 Apollo 客户端。任何新的 api/graphql 仅在 Apollo 服务器中更改。无需更改 UI https://www.apollographql.com/docs/federation/gateway/

    【讨论】:

    • 我不明白如何实现这一点,因为我将查询和突变用作服务。除了我在 graphql.module.ts 文件中执行的实现之外,我什至从未调用函数 apollo.create 。我错过了什么?问候!
    【解决方案2】:

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

    首先,我创建了一个函数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"}}
        } 
    }
    

    更多信息可以找到here

    我想这不是最复杂的解决方案,但确实有效。我很乐意了解是否有更清洁的方法来做到这一点。

    问候!

    【讨论】:

      猜你喜欢
      • 2020-09-03
      • 2021-01-15
      • 2018-01-20
      • 2018-09-18
      • 2021-11-14
      • 2021-12-06
      • 2013-07-30
      • 2022-08-24
      • 2020-02-12
      相关资源
      最近更新 更多