Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test with custom ApolloClientOptions? #1880

Open
Butterbluemchen opened this issue Jan 19, 2023 · 1 comment
Open

How to test with custom ApolloClientOptions? #1880

Butterbluemchen opened this issue Jan 19, 2023 · 1 comment

Comments

@Butterbluemchen
Copy link

I'm trying to write a test using the ApolloTestingModule and ApolloClientOptions.

I provide custom ApolloClientOptions with the APOLLO_OPTION injection token. In the options I set the "no-cache" fetch-policy for my queries.

In my Test I import the ApolloTestingModule. When I run my test, I get the following error: "Error: Apollo has been already created." I'm not sure which part already created the default client but the ApolloTestingModule also tries to create the default client with the fake backend and a cache (that might be provided).

I made a local copy of the ApolloTestingModuleCore and modified the code a bit:

export class ApolloTestingModuleCore {
  constructor(
    apollo: Apollo,
    backend: ApolloTestingBackend,
    @Optional()
    @Inject(APOLLO_TESTING_CLIENTS)
    namedClients?: string[],
    @Optional()
    @Inject(APOLLO_TESTING_CACHE)
    cache?: ApolloCache<any>,
    @Optional()
    @Inject(APOLLO_TESTING_NAMED_CACHE)
    namedCaches?: any, // FIX: using NamedCaches here makes ngc fail
    @Optional()
    @Inject(APOLLO_OPTIONS)
      options?: ApolloClientOptions<any>,
  ) {
    function createOptions(name: string, c?: ApolloCache<any> | null) {
      return {
        ...options,
        link: new ApolloLink((operation) =>
          backend.handle(addClient(name, operation)),
        ),
        cache:
          c ||
          new InMemoryCache({
            addTypename: false,
          }),
      };
    }
     
    apollo.removeClient('default');
    apollo.create(createOptions('default', cache));

    if (namedClients && namedClients.length) {
      namedClients.forEach((name) => {
        const caches =
          namedCaches && typeof namedCaches === 'object' ? namedCaches : {};

        apollo.createNamed(name, createOptions(name, caches[name]));
      });
    }
  }
}

When I use the modified version of the ApolloTestingModuleCore everything works as expected.

Is it possible to change this part or is there an intended way to test with ApolloClientOptions?

@PowerKiKi
Copy link
Collaborator

I assume you are providing Apollo somewhere else, where you shouldn't.

Can you please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue ?

@PowerKiKi PowerKiKi reopened this Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants