Skip to content

Commit fe3854c

Browse files
authored
Merge pull request #32 from guendev/fix/27-3
fix(core): support reactive GraphQL documents in `useFragment`
2 parents 296a2ab + 2ac58e8 commit fe3854c

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

.changeset/new-clowns-wink.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@vue3-apollo/core": patch
3+
"@vue3-apollo/nuxt": patch
4+
---
5+
6+
fix(core): support reactive GraphQL documents in `useFragment`

packages/core/src/composables/useFragment.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ export type UseFragmentOptions<TData = unknown, TVariables extends OperationVari
4646
/**
4747
* GraphQL fragment document.
4848
*/
49-
fragment: DocumentNode | TypedDocumentNode<TData, TVariables>
49+
fragment: MaybeRefOrGetter<DocumentNode | TypedDocumentNode<TData, TVariables>>
5050

5151
/**
5252
* Fragment name (required if document has multiple fragments).
5353
*/
54-
fragmentName?: string
54+
fragmentName?: MaybeRefOrGetter<string>
5555

5656
/**
5757
* Whether the fragment should be watched.
@@ -157,6 +157,8 @@ export function useFragment<TData = unknown, TVariables extends OperationVariabl
157157

158158
// Handle reactive variables
159159
const reactiveVariables = computed(() => toValue(options.variables))
160+
const reactiveFragment = computed(() => toValue(options.fragment))
161+
const reactiveFragmentName = computed(() => toValue(options.fragmentName))
160162

161163
// Calculate cache id
162164
const cacheId = computed(() => {
@@ -188,7 +190,8 @@ export function useFragment<TData = unknown, TVariables extends OperationVariabl
188190

189191
const diff = (): UseFragmentResult<TData> => {
190192
const id = cacheId.value
191-
const { fragment, fragmentName } = options
193+
const fragment = reactiveFragment.value
194+
const fragmentName = reactiveFragmentName.value
192195

193196
if (!id) {
194197
return diffToResult({
@@ -247,13 +250,6 @@ export function useFragment<TData = unknown, TVariables extends OperationVariabl
247250

248251
const subscription = shallowRef<ReturnType<ReturnType<ApolloClient['watchFragment']>['subscribe']>>()
249252

250-
const stop = () => {
251-
if (subscription.value) {
252-
subscription.value.unsubscribe()
253-
subscription.value = undefined
254-
}
255-
}
256-
257253
const start = () => {
258254
if (!enabled.value) {
259255
return
@@ -279,8 +275,8 @@ export function useFragment<TData = unknown, TVariables extends OperationVariabl
279275
// Watch for changes
280276
try {
281277
const observable = client.watchFragment({
282-
fragment: options.fragment,
283-
fragmentName: options.fragmentName,
278+
fragment: reactiveFragment.value,
279+
fragmentName: reactiveFragmentName.value,
284280
from: id,
285281
optimistic,
286282
variables: reactiveVariables.value
@@ -307,6 +303,20 @@ export function useFragment<TData = unknown, TVariables extends OperationVariabl
307303
}
308304
}
309305

306+
const stop = () => {
307+
if (subscription.value) {
308+
subscription.value.unsubscribe()
309+
subscription.value = undefined
310+
}
311+
}
312+
313+
const restart = () => {
314+
if (enabled.value) {
315+
stop()
316+
start()
317+
}
318+
}
319+
310320
// Only start on the client-side, server data is handled via onServerPrefetch
311321
if (!isServer()) {
312322
start()
@@ -322,15 +332,16 @@ export function useFragment<TData = unknown, TVariables extends OperationVariabl
322332

323333
// Watch for changes in from, variables, or fragment options
324334
watch(
325-
[cacheId, reactiveVariables],
335+
[reactiveFragmentName, cacheId, reactiveVariables],
326336
() => {
327-
if (enabled.value) {
328-
stop()
329-
start()
330-
}
337+
restart()
331338
},
332-
{ deep: true }
339+
{ deep: true, flush: 'post' }
333340
)
341+
342+
watch(reactiveFragment, () => {
343+
restart()
344+
})
334345
}
335346

336347
if (getCurrentScope()) {

0 commit comments

Comments
 (0)