Connect an Angular app to multiple Apollo clients
Asked Answered
F

2

8

Following the Apollo Angular docs, I applied this configuration to connect to a given graphql endpoint:

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 {}

Is it possible to use this same configuration to connect to another graphql endpoint?

There is this section in the docs that shows how to use multiple clients, but I don't see how I can apply it with apollo-angular-link-http

Thanks.

Fernandofernas answered 19/5, 2019 at 22:25 Comment(0)
F
12

I have a working solution by changing the initial configuration and following the second link about implementing multiple clients:

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()
    });
  }
}

The second client can then be used like this:

apollo.use('endpoint2').watchQuery({...});
Fernandofernas answered 20/5, 2019 at 3:11 Comment(0)
R
11

If you want to use providers with useFactory

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 {}

Usage:

apollo.use('second').watchQuery({...} });
Rocca answered 5/8, 2020 at 10:14 Comment(5)
Yooo thank you so much, I don't even know how you got this info !Adenoidectomy
@Adenoidectomy I'd love to know how you solved the karma testing. I'm struggling with this for a week now. Thanks !Glenoid
Hello Sherbi, I am sorry but I can't find back the dev I made as we discarded this evolution of having 2 graphQL endpoints... I'll keep searching if I can find itAdenoidectomy
Deleted the comment, note for later : always complete answer when hot ! SOrry Sherbi :(Adenoidectomy
@Adenoidectomy damn..:( well thanks for taking the time to search for it. I tried searching the web but I cant seem to find any resources about testing this multi client stuff, and the documentation they provide + examples don't seem to cut it.Glenoid

© 2022 - 2024 — McMap. All rights reserved.