question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Idea] Create Apollo on Dependency Injection level

See original GitHub issue

TL;DR: An InjectionToken for options of the default ApolloClient as an alternative way to initialize Apollo, a replacement for Apollo.create (both will be supported).

An example:

import {NgModule} from '@angular/core';
import {HttpClientModule} from '@angular/common/http';
import {ApolloOptionsToken, ApolloModule} from 'apollo-angular';
import {HttpLink, HttpLinkModule} from 'apollo-angular-link-http';
import {InMemoryCache} from 'apollo-cache-inmemory';

@NgModule({
  imports: [HttpClientModule, ApolloModule, HttpLinkModule],
  providers: [
    {
      provide: ApolloOptionsToken,
      useFactory: httpLink => {
        return {
          link: httpLink.create({uri: 'https://api.example.com/graphql'}),
          cache: new InMemoryCache(),
        };
      },
      deps: [HttpLink],
    },
  ],
})
class AppModule {}

Details

Why?

Why we didn’t do it in a first place? For one reason, it’s way more complex and not all of Angular Developers are familiar with how Dependency Injection’s work and how to provide a service other than just put a class in an Array (NgModules.providers). Apollo.create is way simpler and easier to get started with Apollo for everyone and I think it’s the best approach.

Do you like it? I already prepared a branch with those changes.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
patricknazarcommented, Jan 26, 2018

I am still not sure how to use this module with feature modules. The Client has not been defined yet error comes up for me with lazy-loaded feature modules (via angular router) but fine when under imports.

Edit: Ended up using the method described in https://alligator.io/angular/providers-shared-modules/

2reactions
patricknazarcommented, Feb 1, 2018

Here is an example repo for anyone wanting to see how this works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Idea] Create Apollo on Dependency Injection level · Issue #460
TL;DR: An InjectionToken for options of the default ApolloClient as an alternative way to initialize Apollo, a replacement for Apollo.create ( ...
Read more >
Dependency Injection in GraphQL-Modules – The Guild
We created a solution where you won't need to use Dependency Injection when you start, but after you are getting to a certain...
Read more >
Dependency Injection - Hot Chocolate
Injecting dependencies at the method-level has a couple of benefits: The resolver can be optimized and the execution strategy can be adjusted ...
Read more >
Contexts and Dependency Injection (CDI) | IntelliJ IDEA ...
Jakarta Contexts and Dependency Injection (CDI) is a specification for declarative dependency injection and supporting services.
Read more >
Working with Apollo GraphQL in Android - ProAndroidDev
Step 6: Now create an instance of ApolloClient with okHttpClient builder in one class. Note: In this tutorial, I have used Dagger-Hilt for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found