@@ -86,8 +86,6 @@ type FieldDefinitionPrintFn = (
86
86
parentName : string ,
87
87
avoidResolverOptionals : boolean
88
88
) => { value : string | null ; meta : { federation ?: { isResolveReference : boolean } } } ;
89
- export type FieldDefinitionResult = [ { node : FieldDefinitionNode } , FieldDefinitionPrintFn ] ;
90
-
91
89
export interface RootResolver {
92
90
content : string ;
93
91
generatedResolverTypes : {
@@ -1521,132 +1519,128 @@ export class BaseResolversVisitor<
1521
1519
return `ParentType extends ${ parentType } = ${ parentType } ` ;
1522
1520
}
1523
1521
1524
- FieldDefinition ( node : FieldDefinitionNode , key : string | number , parent : any ) : FieldDefinitionResult {
1522
+ FieldDefinition ( node : FieldDefinitionNode , key : string | number , parent : any ) : FieldDefinitionPrintFn {
1525
1523
const hasArguments = node . arguments && node . arguments . length > 0 ;
1526
1524
const declarationKind = 'type' ;
1527
1525
1528
- return [
1529
- { node } ,
1530
- ( parentName , avoidResolverOptionals ) => {
1531
- const original : FieldDefinitionNode = parent [ key ] ;
1532
- const parentType = this . schema . getType ( parentName ) ;
1533
- const meta : ReturnType < FieldDefinitionPrintFn > [ 'meta' ] = { } ;
1534
- const typeName = node . name as unknown as string ;
1535
-
1536
- const fieldsToGenerate = this . _federation . findFieldNodesToGenerate ( { type : parentType } ) ; // FIXME: for every field in a object, we are looping through every field of said object. This could be a bottleneck
1537
- const shouldGenerateField =
1538
- fieldsToGenerate . some ( field => field . name . value === typeName ) ||
1539
- this . _federation . isResolveReferenceField ( node ) ;
1540
-
1541
- if ( ! shouldGenerateField ) {
1542
- return { value : null , meta } ;
1543
- }
1526
+ return ( parentName , avoidResolverOptionals ) => {
1527
+ const original : FieldDefinitionNode = parent [ key ] ;
1528
+ const parentType = this . schema . getType ( parentName ) ;
1529
+ const meta : ReturnType < FieldDefinitionPrintFn > [ 'meta' ] = { } ;
1530
+ const typeName = node . name as unknown as string ;
1544
1531
1545
- const contextType = this . getContextType ( parentName , node ) ;
1546
-
1547
- let argsType = hasArguments
1548
- ? this . convertName (
1549
- parentName +
1550
- ( this . config . addUnderscoreToArgsType ? '_' : '' ) +
1551
- this . convertName ( typeName , {
1552
- useTypesPrefix : false ,
1553
- useTypesSuffix : false ,
1554
- } ) +
1555
- 'Args' ,
1556
- {
1557
- useTypesPrefix : true ,
1558
- } ,
1559
- true
1560
- )
1561
- : null ;
1562
-
1563
- const avoidInputsOptionals = this . config . avoidOptionals . inputValue ;
1564
-
1565
- if ( argsType !== null ) {
1566
- const argsToForceRequire = original . arguments . filter (
1567
- arg => ! ! arg . defaultValue || arg . type . kind === 'NonNullType'
1568
- ) ;
1569
-
1570
- if ( argsToForceRequire . length > 0 ) {
1571
- argsType = this . applyRequireFields ( argsType , argsToForceRequire ) ;
1572
- } else if ( original . arguments . length > 0 && avoidInputsOptionals !== true ) {
1573
- argsType = this . applyOptionalFields ( argsType , original . arguments ) ;
1574
- }
1575
- }
1532
+ const fieldsToGenerate = this . _federation . findFieldNodesToGenerate ( { type : parentType } ) ; // FIXME: for every field in a object, we are looping through every field of said object. This could be a bottleneck
1533
+ const shouldGenerateField =
1534
+ fieldsToGenerate . some ( field => field . name . value === typeName ) || this . _federation . isResolveReferenceField ( node ) ;
1576
1535
1577
- const parentTypeSignature = this . _federation . transformFieldParentType ( {
1578
- fieldNode : original ,
1579
- parentType,
1580
- parentTypeSignature : this . getParentTypeForSignature ( node ) ,
1581
- federationTypeSignature : 'FederationType' ,
1582
- } ) ;
1536
+ if ( ! shouldGenerateField ) {
1537
+ return { value : null , meta } ;
1538
+ }
1583
1539
1584
- const { mappedTypeKey, resolverType } = ( ( ) : { mappedTypeKey : string ; resolverType : string } => {
1585
- const baseType = getBaseTypeNode ( original . type ) ;
1586
- const realType = baseType . name . value ;
1587
- const typeToUse = this . getTypeToUse ( realType ) ;
1588
- /**
1589
- * Turns GraphQL type to TypeScript types (`mappedType`) e.g.
1590
- * - String! -> ResolversTypes['String']>
1591
- * - String -> Maybe<ResolversTypes['String']>
1592
- * - [String] -> Maybe<Array<Maybe<ResolversTypes['String']>>>
1593
- * - [String!]! -> Array<ResolversTypes['String']>
1594
- */
1595
- const mappedType = this . _variablesTransformer . wrapAstTypeWithModifiers ( typeToUse , original . type ) ;
1596
-
1597
- const subscriptionType = this . _schema . getSubscriptionType ( ) ;
1598
- const isSubscriptionType = subscriptionType && subscriptionType . name === parentName ;
1599
-
1600
- if ( isSubscriptionType ) {
1601
- return {
1602
- mappedTypeKey : `${ mappedType } , "${ typeName } "` ,
1603
- resolverType : 'SubscriptionResolver' ,
1604
- } ;
1605
- }
1540
+ const contextType = this . getContextType ( parentName , node ) ;
1541
+
1542
+ let argsType = hasArguments
1543
+ ? this . convertName (
1544
+ parentName +
1545
+ ( this . config . addUnderscoreToArgsType ? '_' : '' ) +
1546
+ this . convertName ( typeName , {
1547
+ useTypesPrefix : false ,
1548
+ useTypesSuffix : false ,
1549
+ } ) +
1550
+ 'Args' ,
1551
+ {
1552
+ useTypesPrefix : true ,
1553
+ } ,
1554
+ true
1555
+ )
1556
+ : null ;
1557
+
1558
+ const avoidInputsOptionals = this . config . avoidOptionals . inputValue ;
1559
+
1560
+ if ( argsType !== null ) {
1561
+ const argsToForceRequire = original . arguments . filter (
1562
+ arg => ! ! arg . defaultValue || arg . type . kind === 'NonNullType'
1563
+ ) ;
1606
1564
1607
- const directiveMappings =
1608
- node . directives
1609
- ?. map ( directive => this . _directiveResolverMappings [ directive . name as any ] )
1610
- . filter ( Boolean )
1611
- . reverse ( ) ?? [ ] ;
1565
+ if ( argsToForceRequire . length > 0 ) {
1566
+ argsType = this . applyRequireFields ( argsType , argsToForceRequire ) ;
1567
+ } else if ( original . arguments . length > 0 && avoidInputsOptionals !== true ) {
1568
+ argsType = this . applyOptionalFields ( argsType , original . arguments ) ;
1569
+ }
1570
+ }
1612
1571
1572
+ const parentTypeSignature = this . _federation . transformFieldParentType ( {
1573
+ fieldNode : original ,
1574
+ parentType,
1575
+ parentTypeSignature : this . getParentTypeForSignature ( node ) ,
1576
+ federationTypeSignature : 'FederationType' ,
1577
+ } ) ;
1578
+
1579
+ const { mappedTypeKey, resolverType } = ( ( ) : { mappedTypeKey : string ; resolverType : string } => {
1580
+ const baseType = getBaseTypeNode ( original . type ) ;
1581
+ const realType = baseType . name . value ;
1582
+ const typeToUse = this . getTypeToUse ( realType ) ;
1583
+ /**
1584
+ * Turns GraphQL type to TypeScript types (`mappedType`) e.g.
1585
+ * - String! -> ResolversTypes['String']>
1586
+ * - String -> Maybe<ResolversTypes['String']>
1587
+ * - [String] -> Maybe<Array<Maybe<ResolversTypes['String']>>>
1588
+ * - [String!]! -> Array<ResolversTypes['String']>
1589
+ */
1590
+ const mappedType = this . _variablesTransformer . wrapAstTypeWithModifiers ( typeToUse , original . type ) ;
1591
+
1592
+ const subscriptionType = this . _schema . getSubscriptionType ( ) ;
1593
+ const isSubscriptionType = subscriptionType && subscriptionType . name === parentName ;
1594
+
1595
+ if ( isSubscriptionType ) {
1613
1596
return {
1614
- mappedTypeKey : mappedType ,
1615
- resolverType : directiveMappings [ 0 ] ?? 'Resolver ',
1597
+ mappedTypeKey : ` ${ mappedType } , " ${ typeName } "` ,
1598
+ resolverType : 'SubscriptionResolver ',
1616
1599
} ;
1617
- } ) ( ) ;
1618
-
1619
- const signature : {
1620
- name : string ;
1621
- modifier : string ;
1622
- type : string ;
1623
- genericTypes : string [ ] ;
1624
- } = {
1625
- name : typeName ,
1626
- modifier : avoidResolverOptionals ? '' : '?' ,
1627
- type : resolverType ,
1628
- genericTypes : [ mappedTypeKey , parentTypeSignature , contextType , argsType ] . filter ( f => f ) ,
1629
- } ;
1630
-
1631
- if ( this . _federation . isResolveReferenceField ( node ) ) {
1632
- if ( ! this . _federation . getMeta ( ) [ parentType . name ] . hasResolveReference ) {
1633
- return { value : '' , meta } ;
1634
- }
1635
- signature . type = 'ReferenceResolver' ;
1636
- signature . genericTypes = [ mappedTypeKey , parentTypeSignature , contextType ] ;
1637
- meta . federation = { isResolveReference : true } ;
1638
1600
}
1639
1601
1602
+ const directiveMappings =
1603
+ node . directives
1604
+ ?. map ( directive => this . _directiveResolverMappings [ directive . name as any ] )
1605
+ . filter ( Boolean )
1606
+ . reverse ( ) ?? [ ] ;
1607
+
1640
1608
return {
1641
- value : indent (
1642
- `${ signature . name } ${ signature . modifier } : ${ signature . type } <${ signature . genericTypes . join (
1643
- ', '
1644
- ) } >${ this . getPunctuation ( declarationKind ) } `
1645
- ) ,
1646
- meta,
1609
+ mappedTypeKey : mappedType ,
1610
+ resolverType : directiveMappings [ 0 ] ?? 'Resolver' ,
1647
1611
} ;
1648
- } ,
1649
- ] ;
1612
+ } ) ( ) ;
1613
+
1614
+ const signature : {
1615
+ name : string ;
1616
+ modifier : string ;
1617
+ type : string ;
1618
+ genericTypes : string [ ] ;
1619
+ } = {
1620
+ name : typeName ,
1621
+ modifier : avoidResolverOptionals ? '' : '?' ,
1622
+ type : resolverType ,
1623
+ genericTypes : [ mappedTypeKey , parentTypeSignature , contextType , argsType ] . filter ( f => f ) ,
1624
+ } ;
1625
+
1626
+ if ( this . _federation . isResolveReferenceField ( node ) ) {
1627
+ if ( ! this . _federation . getMeta ( ) [ parentType . name ] . hasResolveReference ) {
1628
+ return { value : '' , meta } ;
1629
+ }
1630
+ signature . type = 'ReferenceResolver' ;
1631
+ signature . genericTypes = [ mappedTypeKey , parentTypeSignature , contextType ] ;
1632
+ meta . federation = { isResolveReference : true } ;
1633
+ }
1634
+
1635
+ return {
1636
+ value : indent (
1637
+ `${ signature . name } ${ signature . modifier } : ${ signature . type } <${ signature . genericTypes . join (
1638
+ ', '
1639
+ ) } >${ this . getPunctuation ( declarationKind ) } `
1640
+ ) ,
1641
+ meta,
1642
+ } ;
1643
+ } ;
1650
1644
}
1651
1645
1652
1646
private getFieldContextType ( parentName : string , node : FieldDefinitionNode ) : string {
@@ -1746,8 +1740,8 @@ export class BaseResolversVisitor<
1746
1740
return false ;
1747
1741
} ) ( ) ;
1748
1742
1749
- const fieldsContent = ( node . fields as unknown as FieldDefinitionResult [ ] )
1750
- . map ( ( [ _ , f ] ) => {
1743
+ const fieldsContent = ( node . fields as unknown as FieldDefinitionPrintFn [ ] )
1744
+ . map ( f => {
1751
1745
return f (
1752
1746
typeName ,
1753
1747
( rootType === 'query' && this . config . avoidOptionals . query ) ||
@@ -1982,7 +1976,7 @@ export class BaseResolversVisitor<
1982
1976
1983
1977
// An Interface in Federation may have the additional __resolveReference resolver, if resolvable.
1984
1978
// So, we filter out the normal fields declared on the Interface and add the __resolveReference resolver.
1985
- const fields = ( node . fields as unknown as FieldDefinitionResult [ ] ) . map ( ( [ _ , f ] ) =>
1979
+ const fields = ( node . fields as unknown as FieldDefinitionPrintFn [ ] ) . map ( f =>
1986
1980
f ( typeName , this . config . avoidOptionals . resolvers )
1987
1981
) ;
1988
1982
for ( const field of fields ) {
0 commit comments