2828import { TypeScriptOperationVariablesToObject } from "@graphql-codegen/typescript" ;
2929import type {
3030 DeclarationKind ,
31+ FieldDefinitionResult ,
3132 ParsedMapper ,
3233 ParsedResolversConfig ,
3334 ResolverTypes ,
@@ -147,6 +148,17 @@ export class LocalStateVisitor extends BaseResolversVisitor<
147148 getTypeToUse,
148149 currentType,
149150 shouldInclude,
151+ onNotMappedObjectType : ( { typeName, initialType } ) => {
152+ let result = initialType ;
153+ if (
154+ this . _federation . getMeta ( ) [ typeName ] ?. referenceSelectionSetsString
155+ ) {
156+ result += ` | ${ this . convertName (
157+ "FederationReferenceTypes"
158+ ) } ['${ typeName } ']`;
159+ }
160+ return result ;
161+ } ,
150162 } ) ;
151163 }
152164
@@ -374,7 +386,7 @@ export class LocalStateVisitor extends BaseResolversVisitor<
374386 }
375387
376388 EnumTypeDefinition ( node : EnumTypeDefinitionNode ) : string {
377- const rawTypeName = node . name as any ;
389+ const rawTypeName = node . name . value ;
378390
379391 // If we have enumValues set, and it's point to an external enum - we need to allow internal values resolvers
380392 // In case we have enumValues set but as explicit values, no need to to do mapping since it's already
@@ -418,7 +430,7 @@ export class LocalStateVisitor extends BaseResolversVisitor<
418430 const name = this . convertName ( node , {
419431 suffix : this . config . resolverTypeSuffix ,
420432 } ) ;
421- const typeName = node . name as any as string ;
433+ const typeName = node . name . value ;
422434
423435 const rootType = ( ( ) : false | "query" | "mutation" | "subscription" => {
424436 if ( this . schema . getQueryType ( ) ?. name === typeName ) {
@@ -433,26 +445,26 @@ export class LocalStateVisitor extends BaseResolversVisitor<
433445 return false ;
434446 } ) ( ) ;
435447
436- const fieldsContent = (
437- node . fields as unknown as FieldDefinitionPrintFn [ ]
438- ) . map ( ( f ) => {
439- return f (
440- typeName ,
441- ( rootType === "query " && this . config . avoidOptionals . query ) ||
442- ( rootType === "mutation " && this . config . avoidOptionals . mutation ) ||
443- ( rootType === " subscription" &&
444- this . config . avoidOptionals . subscription ) ||
445- ( rootType === false && this . config . avoidOptionals . resolvers )
446- ) ;
447- } ) ;
448+ const fieldsContent = ( node . fields as unknown as FieldDefinitionResult [ ] )
449+ . map ( ( f ) => {
450+ return f . printContent (
451+ node ,
452+ ( rootType === "query" && this . config . avoidOptionals . query ) ||
453+ ( rootType === "mutation " && this . config . avoidOptionals . mutation ) ||
454+ ( rootType === "subscription " &&
455+ this . config . avoidOptionals . subscription ) ||
456+ ( rootType === false && this . config . avoidOptionals . resolvers )
457+ ) . value ;
458+ } )
459+ . filter ( ( v ) => v ) ;
448460
449461 const block = new DeclarationBlock ( this . _declarationBlockConfig )
450462 . export ( )
451463 . asKind ( declarationKind )
452464 . withName ( name )
453465 . withBlock ( fieldsContent . join ( "\n" ) ) ;
454466
455- this . _collectedResolvers [ node . name as any ] = {
467+ this . _collectedResolvers [ typeName ] = {
456468 typename : name ,
457469 baseGeneratedTypename : name ,
458470 } ;
@@ -472,91 +484,103 @@ export class LocalStateVisitor extends BaseResolversVisitor<
472484 node : FieldDefinitionNode ,
473485 key : string | number ,
474486 parent : any
475- ) : FieldDefinitionPrintFn {
487+ ) : FieldDefinitionResult {
476488 const hasArguments = node . arguments && node . arguments . length > 0 ;
477489 const declarationKind = "type" ;
478490
479- return ( parentName , avoidResolverOptionals ) => {
480- const original : FieldDefinitionNode = parent [ key ] ;
481-
482- let argsType =
483- hasArguments ?
484- this . convertName (
485- parentName +
486- ( this . config . addUnderscoreToArgsType ? "_" : "" ) +
487- this . convertName ( node . name , {
488- useTypesPrefix : false ,
489- useTypesSuffix : false ,
490- } ) +
491- "Args" ,
492- {
493- useTypesPrefix : true ,
494- } ,
495- true
496- )
497- : null ;
491+ const original : FieldDefinitionNode = parent [ key ] ;
498492
499- if ( argsType !== null ) {
500- const argsToForceRequire = original . arguments ! . filter (
501- ( arg ) => ! ! arg . defaultValue || arg . type . kind === "NonNullType"
502- ) ;
493+ return {
494+ node : original ,
495+ printContent : (
496+ parentNode : ObjectTypeDefinitionNode | InterfaceTypeDefinitionNode ,
497+ avoidResolverOptionals
498+ ) => {
499+ const parentName = parentNode . name . value ;
500+
501+ let argsType =
502+ hasArguments ?
503+ this . convertName (
504+ parentName +
505+ ( this . config . addUnderscoreToArgsType ? "_" : "" ) +
506+ this . convertName ( node . name , {
507+ useTypesPrefix : false ,
508+ useTypesSuffix : false ,
509+ } ) +
510+ "Args" ,
511+ {
512+ useTypesPrefix : true ,
513+ } ,
514+ true
515+ )
516+ : null ;
517+
518+ if ( argsType !== null ) {
519+ const argsToForceRequire = original . arguments ! . filter (
520+ ( arg ) => ! ! arg . defaultValue || arg . type . kind === "NonNullType"
521+ ) ;
503522
504- if ( argsToForceRequire . length > 0 ) {
505- argsType = this . applyRequireFields ( argsType , argsToForceRequire ) ;
506- } else if ( original . arguments ! . length > 0 ) {
507- argsType = this . applyOptionalFields ( argsType , original . arguments ! ) ;
523+ if ( argsToForceRequire . length > 0 ) {
524+ argsType = this . applyRequireFields ( argsType , argsToForceRequire ) ;
525+ } else if ( original . arguments ! . length > 0 ) {
526+ argsType = this . applyOptionalFields ( argsType , original . arguments ! ) ;
527+ }
508528 }
509- }
510- const { mappedTypeKey, resolverType } = ( ( ) : {
511- mappedTypeKey : string ;
512- resolverType : string ;
513- } => {
514- const baseType = getBaseTypeNode ( original . type ) ;
515- const realType = baseType . name . value ;
516- const typeToUse = this . getTypeToUse ( realType ) ;
517- /**
518- * Turns GraphQL type to TypeScript types (`mappedType`) e.g.
519- *
520- * - String! -> ResolversTypes['String']>
521- * - String -> Maybe<ResolversTypes['String']>
522- * - [String] -> Maybe<Array<Maybe<ResolversTypes['String']>>>
523- * - [String!]! -> Array<ResolversTypes['String']>
524- */
525- const mappedType = this . _variablesTransformer . wrapAstTypeWithModifiers (
526- typeToUse ,
527- original . type
528- ) ;
529+ const { mappedTypeKey, resolverType } = ( ( ) : {
530+ mappedTypeKey : string ;
531+ resolverType : string ;
532+ } => {
533+ const baseType = getBaseTypeNode ( original . type ) ;
534+ const realType = baseType . name . value ;
535+ const typeToUse = this . getTypeToUse ( realType ) ;
536+ /**
537+ * Turns GraphQL type to TypeScript types (`mappedType`) e.g.
538+ *
539+ * - String! -> ResolversTypes['String']>
540+ * - String -> Maybe<ResolversTypes['String']>
541+ * - [String] -> Maybe<Array<Maybe<ResolversTypes['String']>>>
542+ * - [String!]! -> Array<ResolversTypes['String']>
543+ */
544+ const mappedType =
545+ this . _variablesTransformer . wrapAstTypeWithModifiers (
546+ typeToUse ,
547+ original . type
548+ ) ;
549+
550+ return {
551+ mappedTypeKey : mappedType ,
552+ resolverType : "LocalState.Resolver" ,
553+ } ;
554+ } ) ( ) ;
555+
556+ const signature : {
557+ name : string ;
558+ modifier : string ;
559+ type : string ;
560+ genericTypes : string [ ] ;
561+ } = {
562+ name : node . name . value ,
563+ modifier : avoidResolverOptionals ? "" : "?" ,
564+ type : resolverType ,
565+ genericTypes : [
566+ mappedTypeKey ,
567+ this . getParentTypeToUse ( parentName ) ,
568+ this . config . contextType . type ,
569+ argsType ! ,
570+ ] . filter ( ( f ) => f ) ,
571+ } ;
529572
530573 return {
531- mappedTypeKey : mappedType ,
532- resolverType : "LocalState.Resolver" ,
574+ value : indent (
575+ `${ signature . name } ${ signature . modifier } : ${
576+ signature . type
577+ } <${ signature . genericTypes . join ( ", " ) } >${ this . getPunctuation (
578+ declarationKind
579+ ) } `
580+ ) ,
581+ meta : { } ,
533582 } ;
534- } ) ( ) ;
535-
536- const signature : {
537- name : string ;
538- modifier : string ;
539- type : string ;
540- genericTypes : string [ ] ;
541- } = {
542- name : node . name as any ,
543- modifier : avoidResolverOptionals ? "" : "?" ,
544- type : resolverType ,
545- genericTypes : [
546- mappedTypeKey ,
547- this . getParentTypeToUse ( parentName ) ,
548- this . config . contextType . type ,
549- argsType ! ,
550- ] . filter ( ( f ) => f ) ,
551- } ;
552-
553- return indent (
554- `${ signature . name } ${ signature . modifier } : ${
555- signature . type
556- } <${ signature . genericTypes . join ( ", " ) } >${ this . getPunctuation (
557- declarationKind
558- ) } `
559- ) ;
583+ } ,
560584 } ;
561585 }
562586
@@ -583,6 +607,9 @@ export class LocalStateVisitor extends BaseResolversVisitor<
583607 if ( resolverType . baseGeneratedTypename ) {
584608 userDefinedTypes [ schemaTypeName ] = {
585609 name : resolverType . baseGeneratedTypename ,
610+ hasIsTypeOf :
611+ this . _parsedSchemaMeta . typesWithIsTypeOf [ schemaTypeName ] ||
612+ false ,
586613 } ;
587614 }
588615
@@ -644,7 +671,7 @@ export class LocalStateVisitor extends BaseResolversVisitor<
644671 const valuesMap = `{ ${ ( node . values || [ ] )
645672 . map (
646673 ( v ) =>
647- `${ v . name as any as string } ${
674+ `${ v . name . value } ${
648675 this . config . avoidOptionals . resolvers ? "" : "?"
649676 } : any`
650677 )
@@ -661,7 +688,7 @@ export class LocalStateVisitor extends BaseResolversVisitor<
661688 ) : string {
662689 return `{ ${ ( node . values || [ ] )
663690 . map ( ( v ) => {
664- const valueName = v . name as any as string ;
691+ const valueName = v . name . value ;
665692 const mappedValue = valuesMapping [ valueName ] ;
666693
667694 return `${ valueName } : ${
0 commit comments