@@ -33,6 +33,7 @@ import {
33
33
ConvertOptions ,
34
34
DeclarationKind ,
35
35
EnumValuesMap ,
36
+ NormalizedAvoidOptionalsConfig ,
36
37
NormalizedScalarsMap ,
37
38
ParsedEnumValuesMap ,
38
39
ResolversNonOptionalTypenameConfig ,
@@ -50,6 +51,7 @@ import {
50
51
wrapTypeWithModifiers ,
51
52
} from './utils.js' ;
52
53
import { OperationVariablesToObject } from './variables-to-object.js' ;
54
+ import { normalizeAvoidOptionals } from './avoid-optionals.js' ;
53
55
54
56
export interface ParsedResolversConfig extends ParsedConfig {
55
57
contextType : ParsedMapper ;
@@ -60,7 +62,7 @@ export interface ParsedResolversConfig extends ParsedConfig {
60
62
[ typeName : string ] : ParsedMapper ;
61
63
} ;
62
64
defaultMapper : ParsedMapper | null ;
63
- avoidOptionals : AvoidOptionalsConfig | boolean ;
65
+ avoidOptionals : NormalizedAvoidOptionalsConfig ;
64
66
addUnderscoreToArgsType : boolean ;
65
67
enumValues : ParsedEnumValuesMap ;
66
68
resolverTypeWrapperSignature : string ;
@@ -78,6 +80,8 @@ export interface ParsedResolversConfig extends ParsedConfig {
78
80
resolversNonOptionalTypename : ResolversNonOptionalTypenameConfig ;
79
81
}
80
82
83
+ type FieldDefinitionPrintFn = ( parentName : string , avoidResolverOptionals : boolean ) => string | null ;
84
+
81
85
export interface RawResolversConfig extends RawConfig {
82
86
/**
83
87
* @description Adds `_` to generated `Args` types in order to avoid duplicate identifiers.
@@ -429,6 +433,9 @@ export interface RawResolversConfig extends RawConfig {
429
433
* inputValue: true,
430
434
* object: true,
431
435
* defaultValue: true,
436
+ * query: true,
437
+ * mutation: true,
438
+ * subscription: true,
432
439
* }
433
440
* },
434
441
* },
@@ -682,7 +689,7 @@ export class BaseResolversVisitor<
682
689
allResolversTypeName : getConfigValue ( rawConfig . allResolversTypeName , 'Resolvers' ) ,
683
690
rootValueType : parseMapper ( rawConfig . rootValueType || '{}' , 'RootValueType' ) ,
684
691
namespacedImportName : getConfigValue ( rawConfig . namespacedImportName , '' ) ,
685
- avoidOptionals : getConfigValue ( rawConfig . avoidOptionals , false ) ,
692
+ avoidOptionals : normalizeAvoidOptionals ( rawConfig . avoidOptionals ) ,
686
693
defaultMapper : rawConfig . defaultMapper
687
694
? parseMapper ( rawConfig . defaultMapper || 'any' , 'DefaultMapperType' )
688
695
: null ,
@@ -1307,7 +1314,7 @@ export class BaseResolversVisitor<
1307
1314
}
1308
1315
1309
1316
protected formatRootResolver ( schemaTypeName : string , resolverType : string , declarationKind : DeclarationKind ) : string {
1310
- return `${ schemaTypeName } ${ this . config . avoidOptionals ? '' : '?' } : ${ resolverType } ${ this . getPunctuation (
1317
+ return `${ schemaTypeName } ${ this . config . avoidOptionals . resolvers ? '' : '?' } : ${ resolverType } ${ this . getPunctuation (
1311
1318
declarationKind
1312
1319
) } `;
1313
1320
}
@@ -1394,11 +1401,11 @@ export class BaseResolversVisitor<
1394
1401
return `ParentType extends ${ parentType } = ${ parentType } ` ;
1395
1402
}
1396
1403
1397
- FieldDefinition ( node : FieldDefinitionNode , key : string | number , parent : any ) : ( parentName : string ) => string | null {
1404
+ FieldDefinition ( node : FieldDefinitionNode , key : string | number , parent : any ) : FieldDefinitionPrintFn {
1398
1405
const hasArguments = node . arguments && node . arguments . length > 0 ;
1399
1406
const declarationKind = 'type' ;
1400
1407
1401
- return ( parentName : string ) => {
1408
+ return ( parentName , avoidResolverOptionals ) => {
1402
1409
const original : FieldDefinitionNode = parent [ key ] ;
1403
1410
const baseType = getBaseTypeNode ( original . type ) ;
1404
1411
const realType = baseType . name . value ;
@@ -1431,10 +1438,7 @@ export class BaseResolversVisitor<
1431
1438
)
1432
1439
: null ;
1433
1440
1434
- const avoidInputsOptionals =
1435
- typeof this . config . avoidOptionals === 'object'
1436
- ? this . config . avoidOptionals ?. inputValue
1437
- : this . config . avoidOptionals === true ;
1441
+ const avoidInputsOptionals = this . config . avoidOptionals . inputValue ;
1438
1442
1439
1443
if ( argsType !== null ) {
1440
1444
const argsToForceRequire = original . arguments . filter (
@@ -1463,10 +1467,6 @@ export class BaseResolversVisitor<
1463
1467
1464
1468
const resolverType = isSubscriptionType ? 'SubscriptionResolver' : directiveMappings [ 0 ] ?? 'Resolver' ;
1465
1469
1466
- const avoidResolverOptionals =
1467
- typeof this . config . avoidOptionals === 'object'
1468
- ? this . config . avoidOptionals ?. resolvers
1469
- : this . config . avoidOptionals === true ;
1470
1470
const signature : {
1471
1471
name : string ;
1472
1472
modifier : string ;
@@ -1532,15 +1532,31 @@ export class BaseResolversVisitor<
1532
1532
} ) ;
1533
1533
const typeName = node . name as any as string ;
1534
1534
const parentType = this . getParentTypeToUse ( typeName ) ;
1535
- const isRootType = [
1536
- this . schema . getQueryType ( ) ?. name ,
1537
- this . schema . getMutationType ( ) ?. name ,
1538
- this . schema . getSubscriptionType ( ) ?. name ,
1539
- ] . includes ( typeName ) ;
1540
1535
1541
- const fieldsContent = node . fields . map ( ( f : any ) => f ( node . name ) ) ;
1536
+ const rootType = ( ( ) : false | 'query' | 'mutation' | 'subscription' => {
1537
+ if ( this . schema . getQueryType ( ) ?. name === typeName ) {
1538
+ return 'query' ;
1539
+ }
1540
+ if ( this . schema . getMutationType ( ) ?. name === typeName ) {
1541
+ return 'mutation' ;
1542
+ }
1543
+ if ( this . schema . getSubscriptionType ( ) ?. name === typeName ) {
1544
+ return 'subscription' ;
1545
+ }
1546
+ return false ;
1547
+ } ) ( ) ;
1548
+
1549
+ const fieldsContent = ( node . fields as unknown as FieldDefinitionPrintFn [ ] ) . map ( f => {
1550
+ return f (
1551
+ typeName ,
1552
+ ( rootType === 'query' && this . config . avoidOptionals . query ) ||
1553
+ ( rootType === 'mutation' && this . config . avoidOptionals . mutation ) ||
1554
+ ( rootType === 'subscription' && this . config . avoidOptionals . subscription ) ||
1555
+ ( rootType === false && this . config . avoidOptionals . resolvers )
1556
+ ) ;
1557
+ } ) ;
1542
1558
1543
- if ( ! isRootType ) {
1559
+ if ( ! rootType ) {
1544
1560
fieldsContent . push (
1545
1561
indent (
1546
1562
`${
@@ -1720,21 +1736,23 @@ export class BaseResolversVisitor<
1720
1736
const allTypesMap = this . _schema . getTypeMap ( ) ;
1721
1737
const implementingTypes : string [ ] = [ ] ;
1722
1738
1723
- this . _collectedResolvers [ node . name as any ] = {
1739
+ const typeName = node . name as any as string ;
1740
+
1741
+ this . _collectedResolvers [ typeName ] = {
1724
1742
typename : name + '<ContextType>' ,
1725
1743
baseGeneratedTypename : name ,
1726
1744
} ;
1727
1745
1728
1746
for ( const graphqlType of Object . values ( allTypesMap ) ) {
1729
1747
if ( graphqlType instanceof GraphQLObjectType ) {
1730
1748
const allInterfaces = graphqlType . getInterfaces ( ) ;
1731
- if ( allInterfaces . find ( int => int . name === ( node . name as any as string ) ) ) {
1749
+ if ( allInterfaces . find ( int => int . name === typeName ) ) {
1732
1750
implementingTypes . push ( graphqlType . name ) ;
1733
1751
}
1734
1752
}
1735
1753
}
1736
1754
1737
- const parentType = this . getParentTypeToUse ( node . name as any as string ) ;
1755
+ const parentType = this . getParentTypeToUse ( typeName ) ;
1738
1756
const possibleTypes = implementingTypes . map ( name => `'${ name } '` ) . join ( ' | ' ) || 'null' ;
1739
1757
const fields = this . config . onlyResolveTypeForInterfaces ? [ ] : node . fields || [ ] ;
1740
1758
@@ -1749,7 +1767,9 @@ export class BaseResolversVisitor<
1749
1767
this . config . optionalResolveType ? '?' : ''
1750
1768
} : TypeResolveFn<${ possibleTypes } , ParentType, ContextType>${ this . getPunctuation ( declarationKind ) } `
1751
1769
) ,
1752
- ...fields . map ( ( f : any ) => f ( node . name ) ) ,
1770
+ ...( fields as unknown as FieldDefinitionPrintFn [ ] ) . map ( f =>
1771
+ f ( typeName , this . config . avoidOptionals . resolvers )
1772
+ ) ,
1753
1773
] . join ( '\n' )
1754
1774
) . string ;
1755
1775
}
@@ -1809,7 +1829,7 @@ export class BaseResolversVisitor<
1809
1829
return null ;
1810
1830
}
1811
1831
1812
- const addOptionalSign = ! this . config . avoidOptionals && ! isNonNullType ( field . type ) ;
1832
+ const addOptionalSign = ! this . config . avoidOptionals . resolvers && ! isNonNullType ( field . type ) ;
1813
1833
1814
1834
return {
1815
1835
addOptionalSign,
0 commit comments