Skip to content

Commit 73dd869

Browse files
authored
Merge pull request #24 from guendev/fix-20
docs(core): enhance `useFragment` JSDoc with examples for type-safe access
2 parents 789dcb6 + f2624f3 commit 73dd869

File tree

1 file changed

+74
-2
lines changed

1 file changed

+74
-2
lines changed

packages/core/src/composables/useFragment.ts

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ export type UseFragmentResult<TData>
117117
* fragment: gql`fragment UserFields on User { id name email }`,
118118
* from: { __typename: 'User', id: '123' }
119119
* })
120+
*
121+
* // Use optional chaining for partial data
122+
* console.log(data.value?.name)
123+
* ```
124+
*
125+
* // Type-safe access with result (recommended):
126+
* @example
127+
* ```ts
128+
* const { result } = useFragment({
129+
* fragment: USER_FRAGMENT,
130+
* from: { __typename: 'User', id: '123' }
131+
* })
132+
*
133+
* if (result.value?.complete) {
134+
* // No optional chaining needed
135+
* console.log(result.value.data.name)
136+
* console.log(result.value.data.email)
137+
* }
120138
* ```
121139
*/
122140
export function useFragment<TData = unknown, TVariables extends OperationVariables = OperationVariables>(
@@ -329,7 +347,40 @@ export function useFragment<TData = unknown, TVariables extends OperationVariabl
329347
/**
330348
* The fragment result data.
331349
* Can be undefined if no data has been loaded yet or if `from` is null.
332-
* For better type safety, use `result` with type narrowing.
350+
* May contain partial data when not all fields are available in cache.
351+
*
352+
* For type-safe access without optional chaining, use `result` with type narrowing.
353+
*
354+
* @example
355+
* Basic usage with optional chaining:
356+
* ```ts
357+
* const { data } = useFragment(options)
358+
* console.log(data.value?.name)
359+
* ```
360+
*
361+
* @example
362+
* Create a helper for complete data access:
363+
* ```ts
364+
* // composables/useCompleteFragment.ts
365+
* import type { OperationVariables } from '@apollo/client'
366+
* import type { UseFragmentOptions } from '@vue3-apollo/core'
367+
*
368+
* export function useCompleteFragment<TData = unknown, TVariables extends OperationVariables = OperationVariables>(options: UseFragmentOptions<TData, TVariables>) {
369+
* 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 }
376+
* }
377+
*
378+
* // Usage
379+
* const { fullData } = useCompleteFragment(options)
380+
* if (fullData.value) {
381+
* console.log(fullData.value.name) // No optional chaining needed
382+
* }
383+
* ```
333384
*/
334385
data: computed(() => result.value?.data),
335386

@@ -370,12 +421,33 @@ export function useFragment<TData = unknown, TVariables extends OperationVariabl
370421

371422
/**
372423
* The full fragment result including data, complete status, and missing info.
373-
* Use this for better TypeScript type narrowing.
424+
*
425+
* This is the recommended way to access fragment data with type safety.
426+
* TypeScript can narrow the type based on the `complete` flag, eliminating
427+
* the need for optional chaining.
374428
*
375429
* @example
430+
* Type-safe access with complete check:
376431
* ```ts
432+
* const { result } = useFragment(options)
433+
*
377434
* if (result.value?.complete) {
435+
* // TypeScript knows all fields are present
378436
* console.log(result.value.data.name) // No optional chaining needed
437+
* console.log(result.value.data.email)
438+
* } else if (result.value?.data) {
439+
* // Partial data - use optional chaining
440+
* console.log(result.value.data.name ?? 'Loading...')
441+
* }
442+
* ```
443+
*
444+
* @example
445+
* Handle missing fields:
446+
* ```ts
447+
* const { result, missing } = useFragment(options)
448+
*
449+
* if (!result.value?.complete && missing.value) {
450+
* console.log('Missing fields:', missing.value)
379451
* }
380452
* ```
381453
*/

0 commit comments

Comments
 (0)