Skip to content

Commit e0f3470

Browse files
authored
Merge pull request #25 from guendev/fix-use-fragment
Update `useFragment` to handle nullish values and example refinement
2 parents 73dd869 + df6ab74 commit e0f3470

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

.changeset/hot-adults-invite.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): update `useFragment` to handle nullish `from` values and refine strict fragment usage example

packages/core/src/composables/useFragment.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type UseFragmentOptions<TData = unknown, TVariables extends OperationVari
4141
* from: 'User:123'
4242
* ```
4343
*/
44-
from: MaybeRefOrGetter<FragmentType<NoInfer<TData>> | Reference | StoreObject | string>
44+
from?: MaybeRefOrGetter<FragmentType<NoInfer<TData>> | null | Reference | StoreObject | string | undefined>
4545

4646
/**
4747
* GraphQL fragment document.
@@ -165,6 +165,10 @@ export function useFragment<TData = unknown, TVariables extends OperationVariabl
165165
return fromValue
166166
}
167167

168+
if (!fromValue) {
169+
return fromValue
170+
}
171+
168172
return cache.value.identify(fromValue)
169173
})
170174

@@ -361,24 +365,20 @@ export function useFragment<TData = unknown, TVariables extends OperationVariabl
361365
* @example
362366
* Create a helper for complete data access:
363367
* ```ts
364-
* // composables/useCompleteFragment.ts
368+
* // composables/useStrictFragment.ts
365369
* import type { OperationVariables } from '@apollo/client'
366370
* import type { UseFragmentOptions } from '@vue3-apollo/core'
367371
*
368-
* export function useCompleteFragment<TData = unknown, TVariables extends OperationVariables = OperationVariables>(options: UseFragmentOptions<TData, TVariables>) {
372+
* export function useStrictFragment<TData = unknown, TVariables extends OperationVariables = OperationVariables>(options: UseFragmentOptions<TData, TVariables>) {
369373
* const fragment = useFragment(options)
370-
* const fullData = computed(() =>
371-
* fragment.result.value?.complete
372-
* ? fragment.result.value.data
373-
* : undefined
374-
* )
375-
* return { ...fragment, fullData }
374+
* const data = computed(() => fragment.result.value.data as TData)
375+
* return { ...fragment, data }
376376
* }
377377
*
378378
* // Usage
379-
* const { fullData } = useCompleteFragment(options)
380-
* if (fullData.value) {
381-
* console.log(fullData.value.name) // No optional chaining needed
379+
* const { data } = useStrictFragment(options)
380+
* if (data.value) {
381+
* console.log(data.value.name) // No optional chaining needed
382382
* }
383383
* ```
384384
*/

0 commit comments

Comments
 (0)