1
- import {
2
- parse ,
3
- printSchema ,
4
- TypeNode ,
5
- ASTKindToNode ,
6
- ListTypeNode ,
7
- NamedTypeNode ,
8
- ObjectTypeDefinitionNode ,
9
- } from 'graphql' ;
1
+ import { parse , TypeNode , ASTKindToNode , ListTypeNode , NamedTypeNode , ObjectTypeDefinitionNode } from 'graphql' ;
10
2
import * as allFakerLocales from '@faker-js/faker' ;
11
3
import casual from 'casual' ;
12
4
import { oldVisit , PluginFunction , resolveExternalModuleAndFn } from '@graphql-codegen/plugin-helpers' ;
13
5
import { sentenceCase } from 'sentence-case' ;
14
6
import a from 'indefinite' ;
7
+ import { printSchemaWithDirectives } from '@graphql-tools/utils' ;
15
8
import { setupFunctionTokens , setupMockValueGenerator } from './mockValueGenerator' ;
16
9
17
10
type NamingConvention = 'change-case-all#pascalCase' | 'keep' | string ;
@@ -460,6 +453,7 @@ const getMockString = (
460
453
typesPrefix = '' ,
461
454
transformUnderscore : boolean ,
462
455
typeNamesMapping ?: Record < string , string > ,
456
+ hasOneOfDirective = false ,
463
457
) => {
464
458
const typeNameConverter = createNameConverter ( typeNamesConvention , transformUnderscore ) ;
465
459
const NewTypeName = typeNamesMapping [ typeName ] || typeName ;
@@ -468,6 +462,10 @@ const getMockString = (
468
462
const typename = addTypename ? `\n __typename: '${ typeName } ',` : '' ;
469
463
const typenameReturnType = addTypename ? `{ __typename: '${ typeName } ' } & ` : '' ;
470
464
465
+ const overridesArgumentString = ! hasOneOfDirective
466
+ ? `overrides?: Partial<${ casedNameWithPrefix } >`
467
+ : `override?: ${ casedNameWithPrefix } ` ;
468
+
471
469
if ( terminateCircularRelationships ) {
472
470
const relationshipsToOmitInit =
473
471
terminateCircularRelationships === 'immediate' ? '_relationshipsToOmit' : 'new Set(_relationshipsToOmit)' ;
@@ -476,7 +474,7 @@ export const ${toMockName(
476
474
typeName ,
477
475
casedName ,
478
476
prefix ,
479
- ) } = (overrides?: Partial< ${ casedNameWithPrefix } > , _relationshipsToOmit: Set<string> = new Set()): ${ typenameReturnType } ${ casedNameWithPrefix } => {
477
+ ) } = (${ overridesArgumentString } , _relationshipsToOmit: Set<string> = new Set()): ${ typenameReturnType } ${ casedNameWithPrefix } => {
480
478
const relationshipsToOmit: Set<string> = ${ relationshipsToOmitInit } ;
481
479
relationshipsToOmit.add('${ casedName } ');
482
480
return {${ typename }
@@ -489,7 +487,7 @@ export const ${toMockName(
489
487
typeName ,
490
488
casedName ,
491
489
prefix ,
492
- ) } = (overrides?: Partial< ${ casedNameWithPrefix } > ): ${ typenameReturnType } ${ casedNameWithPrefix } => {
490
+ ) } = (${ overridesArgumentString } ): ${ typenameReturnType } ${ casedNameWithPrefix } => {
493
491
return {${ typename }
494
492
${ fields }
495
493
};
@@ -624,7 +622,8 @@ type VisitorType = { [K in keyof ASTKindToNode]?: VisitFn<ASTKindToNode[keyof AS
624
622
// https://astexplorer.net
625
623
// Paste your graphql schema in it, and you'll be able to see what the `astNode` will look like
626
624
export const plugin : PluginFunction < TypescriptMocksPluginConfig > = ( schema , documents , config ) => {
627
- const printedSchema = printSchema ( schema ) ; // Returns a string representation of the schema
625
+ const printedSchema = printSchemaWithDirectives ( schema ) ; // Returns a string representation of the schema
626
+
628
627
const astNode = parse ( printedSchema ) ; // Transforms the string into ASTNode
629
628
630
629
if ( 'typenames' in config ) {
@@ -692,6 +691,30 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
692
691
}
693
692
} ,
694
693
} ;
694
+
695
+ const sharedGenerateMockOpts = {
696
+ customScalars : config . scalars ,
697
+ defaultNullableToNull,
698
+ dynamicValues,
699
+ enumsAsTypes,
700
+ enumsPrefix : config . enumsPrefix ,
701
+ enumValuesConvention,
702
+ fieldGeneration : config . fieldGeneration ,
703
+ generateLibrary,
704
+ generatorLocale,
705
+ listElementCount,
706
+ nonNull : false ,
707
+ prefix : config . prefix ,
708
+ terminateCircularRelationships : getTerminateCircularRelationshipsConfig ( config ) ,
709
+ transformUnderscore,
710
+ typeNamesConvention,
711
+ typeNamesMapping,
712
+ types,
713
+ typesPrefix : config . typesPrefix ,
714
+ useImplementingTypes,
715
+ useTypeImports,
716
+ } ;
717
+
695
718
const visitor : VisitorType = {
696
719
FieldDefinition : ( node ) => {
697
720
const fieldName = node . name . value ;
@@ -702,27 +725,8 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
702
725
const value = generateMockValue ( {
703
726
typeName,
704
727
fieldName,
705
- types,
706
- typeNamesConvention,
707
- enumValuesConvention,
708
- terminateCircularRelationships : getTerminateCircularRelationshipsConfig ( config ) ,
709
- prefix : config . prefix ,
710
- typesPrefix : config . typesPrefix ,
711
- enumsPrefix : config . enumsPrefix ,
712
728
currentType : node . type ,
713
- customScalars : config . scalars ,
714
- transformUnderscore,
715
- listElementCount,
716
- dynamicValues,
717
- generateLibrary,
718
- generatorLocale,
719
- fieldGeneration : config . fieldGeneration ,
720
- enumsAsTypes,
721
- useTypeImports,
722
- useImplementingTypes,
723
- defaultNullableToNull,
724
- nonNull : false ,
725
- typeNamesMapping : config . typeNamesMapping ,
729
+ ...sharedGenerateMockOpts ,
726
730
} ) ;
727
731
728
732
return ` ${ fieldName } : overrides && overrides.hasOwnProperty('${ fieldName } ') ? overrides.${ fieldName } ! : ${ value } ,` ;
@@ -735,50 +739,49 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
735
739
return {
736
740
typeName : fieldName ,
737
741
mockFn : ( ) => {
738
- const mockFields = node . fields
739
- ? node . fields
740
- . map ( ( field ) => {
741
- const value = generateMockValue ( {
742
- typeName : fieldName ,
743
- fieldName : field . name . value ,
744
- types,
745
- typeNamesConvention,
746
- enumValuesConvention,
747
- terminateCircularRelationships : getTerminateCircularRelationshipsConfig ( config ) ,
748
- prefix : config . prefix ,
749
- typesPrefix : config . typesPrefix ,
750
- enumsPrefix : config . enumsPrefix ,
751
- currentType : field . type ,
752
- customScalars : config . scalars ,
753
- transformUnderscore,
754
- listElementCount,
755
- dynamicValues,
756
- generateLibrary,
757
- generatorLocale,
758
- fieldGeneration : config . fieldGeneration ,
759
- enumsAsTypes,
760
- useTypeImports,
761
- useImplementingTypes,
762
- defaultNullableToNull,
763
- nonNull : false ,
764
- typeNamesMapping : config . typeNamesMapping ,
765
- } ) ;
766
-
767
- return ` ${ field . name . value } : overrides && overrides.hasOwnProperty('${ field . name . value } ') ? overrides.${ field . name . value } ! : ${ value } ,` ;
768
- } )
769
- . join ( '\n' )
770
- : '' ;
742
+ let mockFieldsString = '' ;
743
+
744
+ const { directives } = node ;
745
+ const hasOneOfDirective = directives . some ( ( directive ) => directive . name . value === 'oneOf' ) ;
746
+
747
+ if ( node . fields && node . fields . length > 0 && hasOneOfDirective ) {
748
+ const field = node . fields [ 0 ] ;
749
+ const value = generateMockValue ( {
750
+ typeName : fieldName ,
751
+ fieldName : field . name . value ,
752
+ currentType : field . type ,
753
+ ...sharedGenerateMockOpts ,
754
+ } ) ;
755
+
756
+ mockFieldsString = ` ...(override ? override : {${ field . name . value } : ${ value } }),` ;
757
+ } else if ( node . fields ) {
758
+ mockFieldsString = node . fields
759
+ . map ( ( field ) => {
760
+ const value = generateMockValue ( {
761
+ typeName : fieldName ,
762
+ fieldName : field . name . value ,
763
+ currentType : field . type ,
764
+ ...sharedGenerateMockOpts ,
765
+ } ) ;
766
+
767
+ const valueWithOverride = `overrides && overrides.hasOwnProperty('${ field . name . value } ') ? overrides.${ field . name . value } ! : ${ value } ` ;
768
+
769
+ return ` ${ field . name . value } : ${ valueWithOverride } ,` ;
770
+ } )
771
+ . join ( '\n' ) ;
772
+ }
771
773
772
774
return getMockString (
773
775
fieldName ,
774
- mockFields ,
776
+ mockFieldsString ,
775
777
typeNamesConvention ,
776
778
getTerminateCircularRelationshipsConfig ( config ) ,
777
779
false ,
778
780
config . prefix ,
779
781
config . typesPrefix ,
780
782
transformUnderscore ,
781
783
typeNamesMapping ,
784
+ hasOneOfDirective ,
782
785
) ;
783
786
} ,
784
787
} ;
0 commit comments