Skip to content

Commit c9b54ed

Browse files
committed
docs(core): enhance useFragment JSDoc with examples for type-safe and complete data usage
1 parent d53a206 commit c9b54ed

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

packages/core/src/composables/useFragment.ts

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,30 @@ export type UseFragmentResult<TData>
112112
* @returns Object containing fragment state and control methods
113113
*
114114
* @example
115+
* Basic usage:
115116
* ```ts
116117
* const { data, complete } = useFragment({
117118
* fragment: gql`fragment UserFields on User { id name email }`,
118119
* from: { __typename: 'User', id: '123' }
119120
* })
121+
*
122+
* // Use optional chaining for partial data
123+
* console.log(data.value?.name)
124+
* ```
125+
*
126+
* @example
127+
* Type-safe access with result (recommended):
128+
* ```ts
129+
* const { result } = useFragment({
130+
* fragment: USER_FRAGMENT,
131+
* from: { __typename: 'User', id: '123' }
132+
* })
133+
*
134+
* if (result.value?.complete) {
135+
* // No optional chaining needed
136+
* console.log(result.value.data.name)
137+
* console.log(result.value.data.email)
138+
* }
120139
* ```
121140
*/
122141
export function useFragment<TData = unknown, TVariables extends OperationVariables = OperationVariables>(
@@ -329,7 +348,37 @@ export function useFragment<TData = unknown, TVariables extends OperationVariabl
329348
/**
330349
* The fragment result data.
331350
* 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.
351+
* May contain partial data when not all fields are available in cache.
352+
*
353+
* For type-safe access without optional chaining, use `result` with type narrowing.
354+
*
355+
* @example
356+
* Basic usage with optional chaining:
357+
* ```ts
358+
* const { data } = useFragment(options)
359+
* console.log(data.value?.name)
360+
* ```
361+
*
362+
* @example
363+
* Create a helper for complete data access:
364+
* ```ts
365+
* // composables/useCompleteFragment.ts
366+
* export function useCompleteFragment(options) {
367+
* const fragment = useFragment(options)
368+
* const fullData = computed(() =>
369+
* fragment.result.value?.complete
370+
* ? fragment.result.value.data
371+
* : undefined
372+
* )
373+
* return { ...fragment, fullData }
374+
* }
375+
*
376+
* // Usage
377+
* const { fullData } = useCompleteFragment(options)
378+
* if (fullData.value) {
379+
* console.log(fullData.value.name) // No optional chaining needed
380+
* }
381+
* ```
333382
*/
334383
data: computed(() => result.value?.data),
335384

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

371420
/**
372421
* The full fragment result including data, complete status, and missing info.
373-
* Use this for better TypeScript type narrowing.
422+
*
423+
* This is the recommended way to access fragment data with type safety.
424+
* TypeScript can narrow the type based on the `complete` flag, eliminating
425+
* the need for optional chaining.
374426
*
375427
* @example
428+
* Type-safe access with complete check:
376429
* ```ts
430+
* const { result } = useFragment(options)
431+
*
377432
* if (result.value?.complete) {
433+
* // TypeScript knows all fields are present
378434
* console.log(result.value.data.name) // No optional chaining needed
435+
* console.log(result.value.data.email)
436+
* } else if (result.value?.data) {
437+
* // Partial data - use optional chaining
438+
* console.log(result.value.data.name ?? 'Loading...')
439+
* }
440+
* ```
441+
*
442+
* @example
443+
* Handle missing fields:
444+
* ```ts
445+
* const { result, missing } = useFragment(options)
446+
*
447+
* if (!result.value?.complete && missing.value) {
448+
* console.log('Missing fields:', missing.value)
379449
* }
380450
* ```
381451
*/

0 commit comments

Comments
 (0)