@@ -21,6 +21,7 @@ export interface ReactApolloPluginConfig extends ClientSideBasePluginConfig {
2121 withHooks : boolean ;
2222 withMutationFn : boolean ;
2323 withRefetchFn : boolean ;
24+ withFragmentHooks ?: boolean ;
2425 apolloReactCommonImportFrom : string ;
2526 apolloReactComponentsImportFrom : string ;
2627 apolloReactHocImportFrom : string ;
@@ -62,6 +63,7 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor<
6263 withHooks : getConfigValue ( rawConfig . withHooks , true ) ,
6364 withMutationFn : getConfigValue ( rawConfig . withMutationFn , true ) ,
6465 withRefetchFn : getConfigValue ( rawConfig . withRefetchFn , false ) ,
66+ withFragmentHooks : getConfigValue ( rawConfig . withFragmentHooks , false ) ,
6567 apolloReactCommonImportFrom : getConfigValue (
6668 rawConfig . apolloReactCommonImportFrom ,
6769 rawConfig . reactApolloVersion === 2
@@ -192,10 +194,14 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor<
192194 const baseImports = super . getImports ( ) ;
193195 const hasOperations = this . _collectedOperations . length > 0 ;
194196
195- if ( ! hasOperations ) {
197+ if ( ! hasOperations && ! this . config . withFragmentHooks ) {
196198 return baseImports ;
197199 }
198200
201+ if ( this . config . withFragmentHooks ) {
202+ return [ ...baseImports , this . getApolloReactHooksImport ( false ) , ...Array . from ( this . imports ) ] ;
203+ }
204+
199205 return [ ...baseImports , ...Array . from ( this . imports ) ] ;
200206 }
201207
@@ -583,4 +589,58 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor<
583589 . filter ( a => a )
584590 . join ( '\n' ) ;
585591 }
592+
593+ public get fragments ( ) : string {
594+ const fragments = super . fragments ;
595+
596+ if ( this . _fragments . length === 0 || ! this . config . withFragmentHooks ) {
597+ return fragments ;
598+ }
599+
600+ const operationType = 'Fragment' ;
601+
602+ const hookFns : string [ ] = [ fragments ] ;
603+
604+ for ( const fragment of this . _fragments . values ( ) ) {
605+ if ( fragment . isExternal ) {
606+ continue ;
607+ }
608+
609+ const nodeName = fragment . name ?? '' ;
610+ const suffix = this . _getHookSuffix ( nodeName , operationType ) ;
611+ const fragmentName : string =
612+ this . convertName ( nodeName , {
613+ suffix,
614+ useTypesPrefix : false ,
615+ useTypesSuffix : false ,
616+ } ) + this . config . hooksSuffix ;
617+
618+ const operationTypeSuffix : string = this . getOperationSuffix ( fragmentName , operationType ) ;
619+
620+ const operationResultType : string = this . convertName ( nodeName , {
621+ suffix : operationTypeSuffix + this . _parsedConfig . operationResultSuffix ,
622+ } ) ;
623+
624+ const IDType = this . scalars . ID ?? 'string' ;
625+
626+ const hook = `export function use${ fragmentName } <F = { id: ${ IDType } }>(identifiers: F) {
627+ return ${ this . getApolloReactHooksIdentifier ( ) } .use${ operationType } <${ operationResultType } >({
628+ fragment: ${ nodeName } ${ this . config . fragmentVariableSuffix } ,
629+ fragmentName: "${ nodeName } ",
630+ from: {
631+ __typename: "${ fragment . onType } ",
632+ ...identifiers,
633+ },
634+ });
635+ }` ;
636+
637+ const hookResults = [
638+ `export type ${ fragmentName } HookResult = ReturnType<typeof use${ fragmentName } >;` ,
639+ ] ;
640+
641+ hookFns . push ( [ hook , hookResults ] . join ( '\n' ) ) ;
642+ }
643+
644+ return hookFns . join ( '\n' ) ;
645+ }
586646}
0 commit comments