[Idea] Create Apollo on Dependency Injection level
See original GitHub issueTL;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 {}
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:
- Created 6 years ago
- Reactions:6
- Comments:16 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 underimports
.Edit: Ended up using the method described in https://alligator.io/angular/providers-shared-modules/
Here is an example repo for anyone wanting to see how this works.