Is it possible to inject a service into apollo-client's middleware?

Viewed 375
2 Answers

I would use one of two options:

Option 1:

  1. Have an Auth service that manages login/tokens etc.
  2. Inject the Auth service into the service where you construct Apollo.
  3. Inside the middleware that adds your token, retrieve the token from the injected Auth service.

Option 2:

Have the service that constructs your Apollo client also manage Auth state. Inject it into the page where you log in to handle authentication and store the token after.

I know it's very old, but just in case, the injection is made into deps:

    providers: [
        {
          provide: APOLLO_NAMED_OPTIONS,
          deps: [HttpLink, AuthService],
          useFactory: createApollo,
        },
      ],

then you just add a parameter into your createApollo(httpLink: HttpLink, authService: AuthService) factory function.

Related