@@ -46,10 +46,14 @@ function makeFunctionTypeArguments(
4646 data : TypeAlias ,
4747 variables ?: TypeAlias | null | undefined
4848) : TypeParameterInstantiation {
49- const params = [ j . genericTypeAnnotation ( j . identifier ( data . id . name ) , null ) ]
50- if ( variables ) {
51- params . push ( j . genericTypeAnnotation ( j . identifier ( variables . id . name ) , null ) )
52- }
49+ const params : FlowTypeKind [ ] = [
50+ j . genericTypeAnnotation ( j . identifier ( data . id . name ) , null ) ,
51+ ]
52+ params . push (
53+ variables
54+ ? j . genericTypeAnnotation ( j . identifier ( variables . id . name ) , null )
55+ : j . objectTypeAnnotation ( [ ] )
56+ )
5357 return j . typeParameterInstantiation ( params )
5458}
5559
@@ -140,6 +144,22 @@ export default function graphqlTypegenCore(
140144 )
141145 }
142146
147+ const queryResultAnnotation = (
148+ data : TypeAlias ,
149+ variables ?: TypeAlias | null | undefined
150+ ) : TypeAnnotation =>
151+ j . typeAnnotation (
152+ j . genericTypeAnnotation (
153+ j . identifier ( addQueryResult ( ) ) ,
154+ j . typeParameterInstantiation ( [
155+ j . genericTypeAnnotation ( j . identifier ( data . id . name ) , null ) ,
156+ variables
157+ ? j . genericTypeAnnotation ( j . identifier ( variables . id . name ) , null )
158+ : j . objectTypeAnnotation ( [ ] ) ,
159+ ] )
160+ )
161+ )
162+
143163 const mutationResultAnnotation = ( data : TypeAlias ) : TypeAnnotation =>
144164 j . typeAnnotation (
145165 j . genericTypeAnnotation (
@@ -150,25 +170,31 @@ export default function graphqlTypegenCore(
150170 )
151171 )
152172
153- const subscriptionResultAnnotation = (
173+ const mutationFunctionAnnotation = (
154174 data : TypeAlias ,
155175 variables ?: TypeAlias | null | undefined
156- ) : TypeAnnotation => {
157- const parameters = [
158- j . genericTypeAnnotation ( j . identifier ( data . id . name ) , null ) ,
159- ]
160- if ( variables ) {
161- parameters . push (
162- j . genericTypeAnnotation ( j . identifier ( variables . id . name ) , null )
176+ ) : TypeAnnotation =>
177+ j . typeAnnotation (
178+ j . genericTypeAnnotation (
179+ j . identifier ( addMutationFunction ( ) ) ,
180+ j . typeParameterInstantiation ( [
181+ j . genericTypeAnnotation ( j . identifier ( data . id . name ) , null ) ,
182+ variables
183+ ? j . genericTypeAnnotation ( j . identifier ( variables . id . name ) , null )
184+ : j . objectTypeAnnotation ( [ ] ) ,
185+ ] )
163186 )
164- }
165- return j . typeAnnotation (
187+ )
188+
189+ const subscriptionResultAnnotation = ( data : TypeAlias ) : TypeAnnotation =>
190+ j . typeAnnotation (
166191 j . genericTypeAnnotation (
167192 j . identifier ( addSubscriptionResult ( ) ) ,
168- j . typeParameterInstantiation ( parameters )
193+ j . typeParameterInstantiation ( [
194+ j . genericTypeAnnotation ( j . identifier ( data . id . name ) , null ) ,
195+ ] )
169196 )
170197 )
171- }
172198
173199 const findQueryPaths = ( root : Collection < any > ) : ASTPath < any > [ ] => [
174200 ...root
@@ -217,6 +243,13 @@ export default function graphqlTypegenCore(
217243 statement ( [ `import {type QueryRenderProps} from '${ apolloPkg } '` ] )
218244 ) . QueryRenderProps
219245 )
246+ const addQueryResult = once (
247+ ( ) =>
248+ addImports (
249+ root ,
250+ statement ( [ `import {type QueryResult} from '${ apolloPkg } '` ] )
251+ ) . QueryResult
252+ )
220253 const addMutationFunction = once (
221254 ( ) =>
222255 addImports (
@@ -434,16 +467,18 @@ export default function graphqlTypegenCore(
434467 const childFunction = getChildFunction ( elementPath )
435468 if ( childFunction ) {
436469 const firstParam = childFunction . get ( 'params' , 0 )
437- const { mutationFunction } =
470+ const { data , variables , mutationFunction } =
438471 onlyValue ( generatedTypes . mutation ) || { }
439- if ( ! mutationFunction ) return
472+ if ( ! data ) return
440473 if ( firstParam && firstParam . node . type === 'Identifier' ) {
441- firstParam . node . typeAnnotation = j . typeAnnotation (
442- j . genericTypeAnnotation (
443- j . identifier ( mutationFunction . id . name ) ,
444- null
445- )
446- )
474+ firstParam . node . typeAnnotation = mutationFunction
475+ ? j . typeAnnotation (
476+ j . genericTypeAnnotation (
477+ j . identifier ( mutationFunction . id . name ) ,
478+ null
479+ )
480+ )
481+ : mutationFunctionAnnotation ( data , variables )
447482 }
448483 }
449484 } )
@@ -477,7 +512,7 @@ export default function graphqlTypegenCore(
477512 path . node . id . type === 'Identifier' ||
478513 path . node . id . type === 'ObjectPattern'
479514 ) {
480- path . node . id . typeAnnotation = queryRenderPropsAnnotation (
515+ path . node . id . typeAnnotation = queryResultAnnotation (
481516 data ,
482517 variables
483518 )
@@ -530,7 +565,7 @@ export default function graphqlTypegenCore(
530565 . forEach ( ( path : ASTPath < VariableDeclarator > ) : void => {
531566 const { data, variables, mutationFunction } =
532567 onlyValue ( generatedTypes . mutation ) || { }
533- if ( ! mutationFunction || ! data ) return
568+ if ( ! data ) return
534569 const {
535570 node : { id } ,
536571 } = path
@@ -540,6 +575,7 @@ export default function graphqlTypegenCore(
540575 variables
541576 )
542577 } else {
578+ if ( ! mutationFunction ) return
543579 if ( id . type !== 'ArrayPattern' && id . type !== 'Identifier' ) return
544580 const tupleTypes : FlowTypeKind [ ] = [
545581 j . genericTypeAnnotation (
@@ -586,10 +622,7 @@ export default function graphqlTypegenCore(
586622 path . node . id . type === 'Identifier' ||
587623 path . node . id . type === 'ObjectPattern'
588624 ) {
589- path . node . id . typeAnnotation = subscriptionResultAnnotation (
590- data ,
591- variables
592- )
625+ path . node . id . typeAnnotation = subscriptionResultAnnotation ( data )
593626 }
594627 if ( path . node . init ?. type !== 'CallExpression' ) return
595628 const options = path . node . init . arguments [ 1 ]
0 commit comments