首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >阿波罗客户端的两个端点(角)

阿波罗客户端的两个端点(角)
EN

Stack Overflow用户
提问于 2020-12-04 23:54:01
回答 2查看 1.6K关注 0票数 0

贝格纳在这里。虽然使用单个端点使用在执行"ng添加阿波罗角“时使用的默认结构很好,但是当使用APOLLO_NAMED_OPTIONS添加第二个端点时,我会得到两个神秘的错误消息。会有什么问题吗?

这是我的graphql.module.ts文件

代码语言:javascript
复制
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,我在这里使用客户机

代码语言:javascript
复制
import { Apollo } from "apollo-angular";



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

最后,我的package.json

代码语言:javascript
复制
{
  "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时:

错误: Uncaught (承诺):不变违反:要初始化阿波罗客户端,必须在options对象中指定一个'cache‘属性。

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

错误错误:未知(承诺):错误: NG0200: GqlApolloInterfaceService错误检测到DI中的循环依赖关系: NG0200: GqlApolloInterfaceService检测到DI中的循环依赖关系

我刚刚从阿波罗客户端开始,所以这可能是非常简单的事情,然而,我得到的印象是文档非常模糊,在制作过程中没有付出太多的努力。什么会发生,最重要的是,我怎样才能让它发挥作用?

致以问候!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-12 01:10:37

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

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

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

代码语言:javascript
复制
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"}}
    } 
}

更多信息可以找到这里

我想这不是最复杂的解决方案,但奏效了。我很乐意知道是否有更干净的方法来做这件事。

致以问候!

票数 0
EN

Stack Overflow用户

发布于 2021-01-01 06:39:50

试着命名为阿波罗客户

代码语言:javascript
复制
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端点,而是使用阿波罗联合与阿波罗服务器配置多个graphql,这样UI将只有一个阿波罗客户端。任何新的api/graphql只在阿波罗服务器中更改。在UI https://www.apollographql.com/docs/federation/gateway/中不需要更改

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65152318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档