首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将一个Angular应用程序连接到多个Apollo客户端

将一个Angular应用程序连接到多个Apollo客户端
EN

Stack Overflow用户
提问于 2019-05-20 06:25:29
回答 2查看 2.2K关注 0票数 7

Apollo Angular docs之后,我应用此配置来连接到给定的graphql端点:

代码语言:javascript
复制
import { HttpClientModule } from "@angular/common/http";
import { ApolloModule, APOLLO_OPTIONS } from "apollo-angular";
import { HttpLinkModule, HttpLink } from "apollo-angular-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";

@NgModule({
  imports: [
    BrowserModule,
    HttpClientModule,
    ApolloModule,
    HttpLinkModule
  ],
  providers: [{
    provide: APOLLO_OPTIONS,
    useFactory: (httpLink: HttpLink) => {
      return {
        cache: new InMemoryCache(),
        link: httpLink.create({
          uri: "https://my.endpoint.io/graphql"
        })
      }
    },
    deps: [HttpLink]
  }],
})
export class AppModule {}

是否可以使用相同的配置连接到另一个graphql终结点?

文档中有说明如何使用多个客户机的this section,但我不知道如何在apollo-angular-link-http中应用它

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-20 11:11:05

我有一个有效的解决方案,通过更改初始配置并遵循the second link来实现多个客户端:

代码语言:javascript
复制
import { NgModule } from '@angular/core';
import { HttpClientModule, HttpHeaders } from '@angular/common/http';
import { Apollo, ApolloModule } from 'apollo-angular';
import { HttpLinkModule, HttpLink } from 'apollo-angular-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';

@NgModule({
  imports: [
    HttpClientModule,
    ApolloModule,
    HttpLinkModule
  ]
})
export class GraphQLModule {

  private readonly URI1: string = 'http://first.endpoint.io/graphql';
  private readonly URI2: string = 'http://second.endpoint.io/graphql';

  constructor(
    apollo: Apollo,
    httpLink: HttpLink
  ) {
    const options1: any = { uri: this.URI1 };
    apollo.createDefault({
      link: httpLink.create(options1),
      cache: new InMemoryCache()
    });

    const options2: any = { uri: this.URI2 };
    apollo.createNamed('endpoint2', {
      link: httpLink.create(options2),
      cache: new InMemoryCache()
    });
  }
}

然后可以像这样使用第二个客户端:

代码语言:javascript
复制
apollo.use('endpoint2').watchQuery({...});
票数 9
EN

Stack Overflow用户

发布于 2020-08-05 18:14:56

如果您想使用providers with useFactory

代码语言:javascript
复制
import { NgModule } from '@angular/core';
import { ApolloModule, APOLLO_OPTIONS, APOLLO_NAMED_OPTIONS } from 'apollo-angular';
import { HttpLinkModule, HttpLink } from 'apollo-angular-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloClientOptions } from 'apollo-client';

const defaultUri= 'http://default.endpoint.io/graphql';
const secondUri = 'http://second.endpoint.io/graphql';
const thirdUri = 'http://third.endpoint.io/graphql';

export function createDefaultApollo(httpLink: HttpLink): ApolloClientOptions<any> {
    return {
        link: httpLink.create({ uri: defaultUri }),
        cache: new InMemoryCache()
    };
}

export function createNamedApollo(httpLink: HttpLink): Record<string, ApolloClientOptions<any>> {
    return {
        second: {
            name: 'second',
            link: httpLink.create({ uri: secondUri }),
            cache: new InMemoryCache()
        },
        third: {
            name: 'third',
            link: httpLink.create({ uri: thirdUri }),
            cache: new InMemoryCache()
        }
    };
}

@NgModule({
    exports: [ApolloModule, HttpLinkModule],
    providers: [
        {
            provide: APOLLO_OPTIONS,
            deps: [HttpLink],
            useFactory: createDefaultApollo
        },
        {
            provide: APOLLO_NAMED_OPTIONS,
            deps: [HttpLink],
            useFactory: createNamedApollo
        }
    ]
})
export class GraphQLModule {}

用法:

代码语言:javascript
复制
apollo.use('second').watchQuery({...} });
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56212486

复制
相关文章

相似问题

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