Skip to content

Commit 3403562

Browse files
authored
Merge pull request #8 from guendev/fix-missing-context
feat(core): add HookContext to event hooks
2 parents a26ac21 + 9f48c1f commit 3403562

37 files changed

+504
-327
lines changed

.changeset/chubby-mice-repair.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@vue3-apollo/core": minor
3+
"@vue3-apollo/nuxt": minor
4+
---
5+
6+
feat(core): add `HookContext` to event hooks

.changeset/pretty-spoons-move.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue3-apollo/docs": minor
3+
---
4+
5+
docs: update migration guide

.github/workflows/ci.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ on:
66
- main
77
paths:
88
- 'packages/**'
9-
- 'package.json'
10-
- 'pnpm-lock.yaml'
11-
- '.github/workflows/ci.yml'
9+
- package.json
10+
- pnpm-lock.yaml
11+
- .github/workflows/ci.yml
1212
pull_request:
1313
branches:
1414
- main
@@ -31,7 +31,7 @@ jobs:
3131
uses: actions/setup-node@v5
3232
with:
3333
node-version: 20
34-
cache: 'pnpm'
34+
cache: pnpm
3535

3636
- name: Install dependencies
3737
run: pnpm install
@@ -53,7 +53,7 @@ jobs:
5353
uses: actions/setup-node@v5
5454
with:
5555
node-version: 20
56-
cache: 'pnpm'
56+
cache: pnpm
5757

5858
- name: Install dependencies
5959
run: pnpm install
@@ -74,7 +74,7 @@ jobs:
7474
uses: actions/setup-node@v5
7575
with:
7676
node-version: 20
77-
cache: 'pnpm'
77+
cache: pnpm
7878

7979
- name: Install dependencies
8080
run: pnpm install

.github/workflows/deploy-docs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
paths:
88
- 'packages/docs/**'
99
- 'packages/core/**'
10-
- '.github/workflows/deploy-docs.yml'
10+
- .github/workflows/deploy-docs.yml
1111
workflow_dispatch:
1212

1313
concurrency:
@@ -33,7 +33,7 @@ jobs:
3333
uses: actions/setup-node@v5
3434
with:
3535
node-version: 20
36-
cache: 'pnpm'
36+
cache: pnpm
3737

3838
- name: Install dependencies
3939
run: pnpm install --frozen-lockfile

.github/workflows/release.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ on:
66
- main
77
paths:
88
- 'packages/**'
9-
- 'package.json'
10-
- 'pnpm-lock.yaml'
11-
- 'pnpm-workspace.yaml'
9+
- package.json
10+
- pnpm-lock.yaml
11+
- pnpm-workspace.yaml
1212
- '.changeset/**'
1313

1414
concurrency:
@@ -36,7 +36,7 @@ jobs:
3636
uses: actions/setup-node@v5
3737
with:
3838
node-version: 20
39-
cache: 'pnpm'
39+
cache: pnpm
4040
registry-url: 'https://registry.npmjs.org'
4141

4242
- name: Install dependencies

eslint.config.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export default antfu(
55
ignores: [
66
'src/**/*.generated.*',
77
'.nuxt',
8-
'.junie/*'
8+
'.junie/*',
9+
'**/*/*.md'
910
],
1011
stylistic: {
1112
overrides: {

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "root",
33
"type": "module",
44
"version": "1.0.0",
5+
"packageManager": "[email protected]",
56
"scripts": {
67
"build": "pnpm run build:core && pnpm run build:nuxt && pnpm run build:docs",
78
"build:core": "pnpm --filter @vue3-apollo/core run build",
@@ -30,6 +31,5 @@
3031
"eslint-plugin-perfectionist": "^4.15.1",
3132
"typescript": "^5.9.3",
3233
"unbuild": "^3.6.1"
33-
},
34-
"packageManager": "[email protected]"
35-
}
34+
}
35+
}

packages/core/README.md

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,32 @@ npm install @vue3-apollo/core @apollo/client graphql
2626
### 1. Create an Apollo Client
2727

2828
```ts
29-
import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client/core'
29+
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client/core'
3030

3131
export const defaultClient = new ApolloClient({
32-
cache: new InMemoryCache(),
33-
link: new HttpLink({
32+
cache: new InMemoryCache(),
33+
link: new HttpLink({
3434
// Example public GraphQL API
35-
uri: 'https://graphqlplaceholder.vercel.app/graphql',
36-
}),
35+
uri: 'https://graphqlplaceholder.vercel.app/graphql',
36+
}),
3737
})
3838
```
3939

4040
### 2. Register the Plugin
4141

4242
```ts
43-
import { createApp } from 'vue'
4443
import { apolloPlugin } from '@vue3-apollo/core'
45-
import { defaultClient, analyticsClient } from './apollo-clients'
44+
import { createApp } from 'vue'
45+
46+
import { analyticsClient, defaultClient } from './apollo-clients'
4647

4748
const app = createApp(App)
4849

4950
app.use(apolloPlugin, {
50-
clients: {
51-
default: defaultClient,
52-
analytics: analyticsClient,
53-
},
51+
clients: {
52+
analytics: analyticsClient,
53+
default: defaultClient,
54+
},
5455
})
5556
```
5657

@@ -73,12 +74,16 @@ const GET_POSTS = gql`
7374
}
7475
`
7576
76-
const { result, loading, error } = useQuery(GET_POSTS)
77+
const { error, loading, result } = useQuery(GET_POSTS)
7778
</script>
7879
7980
<template>
80-
<div v-if="loading">Loading...</div>
81-
<div v-else-if="error">{{ error.message }}</div>
81+
<div v-if="loading">
82+
Loading...
83+
</div>
84+
<div v-else-if="error">
85+
{{ error.message }}
86+
</div>
8287
<ul v-else>
8388
<li v-for="post in result.posts" :key="post.id">
8489
<strong>{{ post.title }}</strong> — {{ post.body }}
@@ -108,7 +113,7 @@ You can register and switch between multiple clients:
108113

109114
```ts
110115
const { result: analyticsData } = useQuery(ANALYTICS_QUERY, null, {
111-
clientId: 'analytics',
116+
clientId: 'analytics',
112117
})
113118
```
114119

@@ -117,7 +122,7 @@ const { result: analyticsData } = useQuery(ANALYTICS_QUERY, null, {
117122
All composables are fully typed, providing autocompletion and inference for query variables and responses.
118123

119124
```ts
120-
const { result } = useQuery<{ posts: { id: string; title: string }[] }>(GET_POSTS)
125+
const { result } = useQuery<{ posts: { id: string, title: string }[] }>(GET_POSTS)
121126
```
122127

123128
## 🧑‍💻 Contributing
@@ -141,4 +146,4 @@ pnpm dev:docs
141146
- 🌐 [Documentation](https://vue3-apollo.guen.dev/)
142147
- 💾 [GitHub Repository](https://github.com/guendev/vue3-apollo)
143148
- 📦 [npm - @vue3-apollo/core](https://www.npmjs.com/package/@vue3-apollo/core)
144-
- 🧱 [Nuxt Integration - @vue3-apollo/nuxt](https://www.npmjs.com/package/@vue3-apollo/nuxt)
149+
- 🧱 [Nuxt Integration - @vue3-apollo/nuxt](https://www.npmjs.com/package/@vue3-apollo/nuxt)

packages/core/package.json

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
{
22
"name": "@vue3-apollo/core",
3+
"type": "module",
34
"version": "1.1.0",
5+
"packageManager": "[email protected]",
46
"description": "Composable Apollo Client utilities for Vue 3.",
5-
"keywords": [
6-
"vue",
7-
"vue3",
8-
"apollo",
9-
"graphql",
10-
"composables",
11-
"client"
12-
],
13-
"license": "MIT",
147
"author": "Guen <[email protected]>",
8+
"license": "MIT",
159
"homepage": "https://github.com/guendev/vue3-apollo#readme",
1610
"repository": {
1711
"type": "git",
@@ -20,10 +14,15 @@
2014
"bugs": {
2115
"url": "https://github.com/guendev/vue3-apollo/issues"
2216
},
23-
"type": "module",
24-
"main": "./dist/index.cjs",
25-
"module": "./dist/index.js",
26-
"types": "./dist/index.d.ts",
17+
"keywords": [
18+
"vue",
19+
"vue3",
20+
"apollo",
21+
"graphql",
22+
"composables",
23+
"client"
24+
],
25+
"sideEffects": false,
2726
"exports": {
2827
".": {
2928
"types": "./dist/index.d.ts",
@@ -33,6 +32,9 @@
3332
},
3433
"./package.json": "./package.json"
3534
},
35+
"main": "./dist/index.cjs",
36+
"module": "./dist/index.js",
37+
"types": "./dist/index.d.ts",
3638
"typesVersions": {
3739
"*": {
3840
"*": [
@@ -42,10 +44,12 @@
4244
}
4345
},
4446
"files": [
45-
"dist",
46-
"README.md"
47+
"README.md",
48+
"dist"
4749
],
48-
"sideEffects": false,
50+
"engines": {
51+
"node": ">=18.0.0"
52+
},
4953
"scripts": {
5054
"dev": "tsup --config tsup.config.ts --watch",
5155
"build": "tsup --config tsup.config.ts",
@@ -67,13 +71,9 @@
6771
"tsup": "^8.5.0",
6872
"typescript": "^5.9.3"
6973
},
70-
"packageManager": "[email protected]",
71-
"engines": {
72-
"node": ">=18.0.0"
73-
},
7474
"publishConfig": {
7575
"access": "public",
7676
"registry": "https://registry.npmjs.org/",
7777
"provenance": true
7878
}
79-
}
79+
}

packages/core/src/composables/useMutation.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { DocumentNode } from 'graphql'
44
import { createEventHook } from '@vueuse/core'
55
import { nextTick, ref, shallowRef } from 'vue'
66

7-
import type { UseBaseOption } from '../utils/type'
7+
import type { HookContext, UseBaseOption } from '../utils/type'
88

99
import { useApolloTracking } from '../helpers/useApolloTracking'
1010
import { isDefined } from '../utils/isDefined'
@@ -91,8 +91,8 @@ export function useMutation<
9191
const error = ref<ErrorLike>()
9292
const called = ref(false)
9393

94-
const onDone = createEventHook<TData>()
95-
const onError = createEventHook<ErrorLike>()
94+
const onDone = createEventHook<[TData, HookContext]>()
95+
const onError = createEventHook<[ErrorLike, HookContext]>()
9696

9797
useApolloTracking({
9898
state: loading,
@@ -119,14 +119,12 @@ export function useMutation<
119119

120120
if (isDefined(result.data)) {
121121
await nextTick()
122-
// eslint-disable-next-line ts/ban-ts-comment
123-
// @ts-expect-error
124-
void onDone.trigger(result.data)
122+
void onDone.trigger(result.data, { client })
125123
}
126124

127125
if (result.error) {
128126
error.value = result.error
129-
void onError.trigger(result.error)
127+
void onError.trigger(result.error, { client })
130128
}
131129

132130
return result
@@ -136,7 +134,7 @@ export function useMutation<
136134
error.value = mutationErrorValue
137135

138136
await nextTick()
139-
void onError.trigger(mutationErrorValue)
137+
void onError.trigger(mutationErrorValue, { client })
140138
if (options?.throws === 'always') {
141139
throw e
142140
}
@@ -205,9 +203,11 @@ export function useMutation<
205203
*
206204
* @example
207205
* ```ts
208-
* onDone((data) => {
206+
* onDone((data, context) => {
209207
* toast.success('User created!')
210208
* router.push(`/users/${data.createUser.id}`)
209+
* // Access Apollo client for manual cache updates
210+
* context.client.cache.evict({ fieldName: 'users' })
211211
* })
212212
* ```
213213
*/
@@ -219,9 +219,11 @@ export function useMutation<
219219
*
220220
* @example
221221
* ```ts
222-
* onError((error) => {
222+
* onError((error, context) => {
223223
* toast.error(error.message)
224224
* console.error('Mutation failed:', error)
225+
* // Access Apollo client for error handling
226+
* context.client.clearStore()
225227
* })
226228
* ```
227229
*/

0 commit comments

Comments
 (0)