0

初学者在这里。虽然使用执行“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:

错误错误:未捕获(承诺):不变违规:要初始化 Apollo 客户端,您必须在选项对象中指定“缓存”属性。

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

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

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

问候!

4

2 回答 2

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

于 2021-01-01T06:39:50.047 回答
0

我通过在 createApollo 的返回对象的链接属性中使用 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"}}
    } 
}

更多信息可以在这里找到

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

问候!

于 2021-01-12T01:10:37.157 回答