graphql-hive / graphql-hive/graphql-modules

Sharing a single provider instance between multiple Injection Tokens

Open
#2,470 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1.3k
Forks
113
Avg merge
3h 36m
Merged PRs (30d)
1

Description

When I rearchitected Accounts.js 1.0 I wanted to preserve the ability to use different databases to store the user data and the sessions (whenever the user decides to use sessions instead of JSON Web Tokens).
This is done by [passing a database type parameter](https://github.com/accounts-js/accounts/blob/master/modules/module-mongo/src/index.ts#L26) to the function in charge to create the module.
This way depending on the parameter the module will provide either `DatabaseInterfaceUserToken`, `DatabaseInterfaceSessionsToken` or both (not providing both of them means that another module will have to provide the other provider).
The problem is that when you want to provide both of them from the same module you cannot actually do so because `useClass` will instantiate two instances of the same provider.
I would like to implement the possibility of sharing the same instance of a class between multiple injectors:

```ts
{
provide: DatabaseInterfaceUserToken,
useClass: Mongo,
},
{
provide: DatabaseInterfaceSessionsToken,
useInstance: DatabaseInterfaceUserToken,
},
```

The current workaround is that whenever the same module wants to provide both of them we provide `DatabaseInterfaceSessionsToken` using `{ useValue: undefined }` and every time we need to access the `DatabaseInterfaceSessionsToken` we check if it is defined and we use `DatabaseInterfaceUserToken` instead whenever it is undefined. This is possible because `DatabaseInterfaceUserToken` must be always provided.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.