Skip to content

Commit 2e9b5fa

Browse files
authored
Merge pull request #37 from guendev/feat/use-apollo-clients
feat(core): add `useApolloClients` composable for managing multiple Apollo clients
2 parents 3172b3d + 72da2d6 commit 2e9b5fa

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

packages/core/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ const { error, loading, result } = useQuery(GET_POSTS)
9494

9595
## 🧠 Composables Overview
9696

97-
| Composable | Description |
98-
|-------------|--------------|
99-
| `useQuery` | Reactive GraphQL query |
100-
| `useLazyQuery` | Run query on demand |
101-
| `useMutation` | Execute GraphQL mutations |
102-
| `useSubscription` | Subscribe to GraphQL streams |
103-
| `useApolloClient` | Access current Apollo client |
104-
| `provideApolloClients` / `useApolloClients` | Manage multiple clients |
97+
| Composable | Description |
98+
|--------------------|------------------------------------------------|
99+
| `useQuery` | Reactive GraphQL query |
100+
| `useMutation` | Execute GraphQL mutations |
101+
| `useSubscription` | Subscribe to GraphQL streams |
102+
| `useFragment` | Retrieve and manage normalized cache fragments |
103+
| `useApolloClient` | Access current Apollo client |
104+
| `useApolloClients` | Access Apollo clients |
105105

106106
See the [full API reference](https://vue3-apollo.guen.dev/core/composables/use-query) for details.
107107

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { inject } from 'vue'
2+
3+
import { APOLLO_CLIENTS_KEY } from '../constants/apollo'
4+
5+
/**
6+
* Get all Apollo client instances
7+
*
8+
* @returns Object containing all registered Apollo clients
9+
*
10+
* @example
11+
* ```ts
12+
* // Get all clients
13+
* const clients = useApolloClients()
14+
*
15+
* // Access specific client
16+
* const defaultClient = clients.default
17+
* const analyticsClient = clients.analytics
18+
*
19+
* // Iterate over all clients
20+
* Object.entries(clients).forEach(([id, client]) => {
21+
* console.log(`Client ${id}:`, client)
22+
* })
23+
* ```
24+
*/
25+
export function useApolloClients() {
26+
const apolloClients = inject(APOLLO_CLIENTS_KEY)
27+
28+
if (!apolloClients) {
29+
throw new Error(
30+
'[useApolloClients] Apollo clients registry not found. Did you forget to install ApolloPlugin?'
31+
)
32+
}
33+
34+
return apolloClients
35+
}

packages/core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export * from './composables/useApolloClient'
2+
export * from './composables/useApolloClients'
3+
24
export * from './composables/useFragment'
35
export * from './composables/useMutation'
46
export * from './composables/useQuery'

packages/docs/composables/useApolloClient.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const result = await client.query({
1414
query: GET_USERS,
1515
})
1616
```
17+
> Use `useApolloClients()` to get all clients.
1718
1819
## API
1920

@@ -22,4 +23,4 @@ const result = await client.query({
2223
- If omitted, returns the first available client (usually the `default` one).
2324

2425
### Returns
25-
- **`ApolloClient`** – the Apollo client instance.
26+
- **`ApolloClient`** – the Apollo client instance.

packages/nuxt/src/module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default defineNuxtModule<ApolloModuleOptions>({
3636
from: '@vue3-apollo/core',
3737
imports: [
3838
// composables
39+
'useApolloClients',
3940
'useApolloClient',
4041
'useFragment',
4142
'useMutation',

0 commit comments

Comments
 (0)