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
};
@@ -622,7 +620,8 @@ type VisitorType = { [K in keyof ASTKindToNode]?: VisitFn<ASTKindToNode[keyof AS
622
620
// https://astexplorer.net
623
621
// Paste your graphql schema in it, and you'll be able to see what the `astNode` will look like
624
622
export const plugin : PluginFunction < TypescriptMocksPluginConfig > = ( schema , documents , config ) => {
625
- const printedSchema = printSchema ( schema ) ; // Returns a string representation of the schema
623
+ const printedSchema = printSchemaWithDirectives ( schema ) ; // Returns a string representation of the schema
624
+
626
625
const astNode = parse ( printedSchema ) ; // Transforms the string into ASTNode
627
626
628
627
if ( 'typenames' in config ) {
@@ -690,6 +689,30 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
690
689
}
691
690
} ,
692
691
} ;
692
+
693
+ const sharedGenerateMockOpts = {
694
+ customScalars : config . scalars ,
695
+ defaultNullableToNull,
696
+ dynamicValues,
697
+ enumsAsTypes,
698
+ enumsPrefix : config . enumsPrefix ,
699
+ enumValuesConvention,
700
+ fieldGeneration : config . fieldGeneration ,
701
+ generateLibrary,
702
+ generatorLocale,
703
+ listElementCount,
704
+ nonNull : false ,
705
+ prefix : config . prefix ,
706
+ terminateCircularRelationships : getTerminateCircularRelationshipsConfig ( config ) ,
707
+ transformUnderscore,
708
+ typeNamesConvention,
709
+ typeNamesMapping,
710
+ types,
711
+ typesPrefix : config . typesPrefix ,
712
+ useImplementingTypes,
713
+ useTypeImports,
714
+ } ;
715
+
693
716
const visitor : VisitorType = {
694
717
FieldDefinition : ( node ) => {
695
718
const fieldName = node . name . value ;
@@ -700,27 +723,8 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
700
723
const value = generateMockValue ( {
701
724
typeName,
702
725
fieldName,
703
- types,
704
- typeNamesConvention,
705
- enumValuesConvention,
706
- terminateCircularRelationships : getTerminateCircularRelationshipsConfig ( config ) ,
707
- prefix : config . prefix ,
708
- typesPrefix : config . typesPrefix ,
709
- enumsPrefix : config . enumsPrefix ,
710
726
currentType : node . type ,
711
- customScalars : config . scalars ,
712
- transformUnderscore,
713
- listElementCount,
714
- dynamicValues,
715
- generateLibrary,
716
- generatorLocale,
717
- fieldGeneration : config . fieldGeneration ,
718
- enumsAsTypes,
719
- useTypeImports,
720
- useImplementingTypes,
721
- defaultNullableToNull,
722
- nonNull : false ,
723
- typeNamesMapping : config . typeNamesMapping ,
727
+ ...sharedGenerateMockOpts ,
724
728
} ) ;
725
729
726
730
return ` ${ fieldName } : overrides && overrides.hasOwnProperty('${ fieldName } ') ? overrides.${ fieldName } ! : ${ value } ,` ;
@@ -733,50 +737,49 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
733
737
return {
734
738
typeName : fieldName ,
735
739
mockFn : ( ) => {
736
- const mockFields = node . fields
737
- ? node . fields
738
- . map ( ( field ) => {
739
- const value = generateMockValue ( {
740
- typeName : fieldName ,
741
- fieldName : field . name . value ,
742
- types,
743
- typeNamesConvention,
744
- enumValuesConvention,
745
- terminateCircularRelationships : getTerminateCircularRelationshipsConfig ( config ) ,
746
- prefix : config . prefix ,
747
- typesPrefix : config . typesPrefix ,
748
- enumsPrefix : config . enumsPrefix ,
749
- currentType : field . type ,
750
- customScalars : config . scalars ,
751
- transformUnderscore,
752
- listElementCount,
753
- dynamicValues,
754
- generateLibrary,
755
- generatorLocale,
756
- fieldGeneration : config . fieldGeneration ,
757
- enumsAsTypes,
758
- useTypeImports,
759
- useImplementingTypes,
760
- defaultNullableToNull,
761
- nonNull : false ,
762
- typeNamesMapping : config . typeNamesMapping ,
763
- } ) ;
764
-
765
- return ` ${ field . name . value } : overrides && overrides.hasOwnProperty('${ field . name . value } ') ? overrides.${ field . name . value } ! : ${ value } ,` ;
766
- } )
767
- . join ( '\n' )
768
- : '' ;
740
+ let mockFieldsString = '' ;
741
+
742
+ const { directives } = node ;
743
+ const hasOneOfDirective = directives . some ( ( directive ) => directive . name . value === 'oneOf' ) ;
744
+
745
+ if ( node . fields && node . fields . length > 0 && hasOneOfDirective ) {
746
+ const field = node . fields [ 0 ] ;
747
+ const value = generateMockValue ( {
748
+ typeName : fieldName ,
749
+ fieldName : field . name . value ,
750
+ currentType : field . type ,
751
+ ...sharedGenerateMockOpts ,
752
+ } ) ;
753
+
754
+ mockFieldsString = ` ...(override ? override : {${ field . name . value } : ${ value } }),` ;
755
+ } else if ( node . fields ) {
756
+ mockFieldsString = node . fields
757
+ . map ( ( field ) => {
758
+ const value = generateMockValue ( {
759
+ typeName : fieldName ,
760
+ fieldName : field . name . value ,
761
+ currentType : field . type ,
762
+ ...sharedGenerateMockOpts ,
763
+ } ) ;
764
+
765
+ const valueWithOverride = `overrides && overrides.hasOwnProperty('${ field . name . value } ') ? overrides.${ field . name . value } ! : ${ value } ` ;
766
+
767
+ return ` ${ field . name . value } : ${ valueWithOverride } ,` ;
768
+ } )
769
+ . join ( '\n' ) ;
770
+ }
769
771
770
772
return getMockString (
771
773
fieldName ,
772
- mockFields ,
774
+ mockFieldsString ,
773
775
typeNamesConvention ,
774
776
getTerminateCircularRelationshipsConfig ( config ) ,
775
777
false ,
776
778
config . prefix ,
777
779
config . typesPrefix ,
778
780
transformUnderscore ,
779
781
typeNamesMapping ,
782
+ hasOneOfDirective ,
780
783
) ;
781
784
} ,
782
785
} ;
0 commit comments