Skip to content

Commit 6947123

Browse files
Merge branch 'main' into main
2 parents 4211203 + 1a04833 commit 6947123

File tree

7 files changed

+403
-96
lines changed

7 files changed

+403
-96
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# v4.2.0 (Thu Feb 06 2025)
2+
3+
#### 🚀 Enhancement
4+
5+
- Add support for oneOf directive [#180](https://github.com/ardeois/graphql-codegen-typescript-mock-data/pull/180) ([@thomasobrien99](https://github.com/thomasobrien99))
6+
7+
#### Authors: 1
8+
9+
- [@thomasobrien99](https://github.com/thomasobrien99)
10+
11+
---
12+
113
# v4.1.0 (Fri Sep 20 2024)
214

315
#### 🚀 Enhancement

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-codegen-typescript-mock-data",
3-
"version": "4.1.0",
3+
"version": "4.2.0",
44
"description": "GraphQL Codegen plugin for building mock data",
55
"main": "dist/commonjs/index.js",
66
"module": "dist/esnext/index.js",
@@ -24,6 +24,7 @@
2424
"dependencies": {
2525
"@faker-js/faker": "^8.4.1",
2626
"@graphql-codegen/plugin-helpers": "^5.0.4",
27+
"@graphql-tools/utils": "^10.7.2",
2728
"casual": "^1.6.2",
2829
"change-case-all": "^1.0.15",
2930
"indefinite": "^2.4.1",
@@ -39,6 +40,7 @@
3940
"@auto-it/conventional-commits": "^11.1.6",
4041
"@graphql-codegen/testing": "^3.0.3",
4142
"@graphql-codegen/typescript": "^4.0.7",
43+
"@types/indefinite": "^2.3.4",
4244
"@types/jest": "^27.0.2",
4345
"@typescript-eslint/eslint-plugin": "^5.1.0",
4446
"@typescript-eslint/parser": "^5.1.0",
@@ -57,7 +59,7 @@
5759
"prettier": "^2.4.1",
5860
"prettier-config-landr": "^0.2.0",
5961
"ts-jest": "^27.0.7",
60-
"typescript": "^4.4.4"
62+
"typescript": "^5.7.3"
6163
},
6264
"sideEffects": false,
6365
"scripts": {

src/index.ts

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
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';
102
import * as allFakerLocales from '@faker-js/faker';
113
import casual from 'casual';
124
import { oldVisit, PluginFunction, resolveExternalModuleAndFn } from '@graphql-codegen/plugin-helpers';
135
import { sentenceCase } from 'sentence-case';
146
import a from 'indefinite';
7+
import { printSchemaWithDirectives } from '@graphql-tools/utils';
158
import { setupFunctionTokens, setupMockValueGenerator } from './mockValueGenerator';
169

1710
type NamingConvention = 'change-case-all#pascalCase' | 'keep' | string;
@@ -460,6 +453,7 @@ const getMockString = (
460453
typesPrefix = '',
461454
transformUnderscore: boolean,
462455
typeNamesMapping?: Record<string, string>,
456+
hasOneOfDirective = false,
463457
) => {
464458
const typeNameConverter = createNameConverter(typeNamesConvention, transformUnderscore);
465459
const NewTypeName = typeNamesMapping[typeName] || typeName;
@@ -468,6 +462,10 @@ const getMockString = (
468462
const typename = addTypename ? `\n __typename: '${typeName}',` : '';
469463
const typenameReturnType = addTypename ? `{ __typename: '${typeName}' } & ` : '';
470464

465+
const overridesArgumentString = !hasOneOfDirective
466+
? `overrides?: Partial<${casedNameWithPrefix}>`
467+
: `override?: ${casedNameWithPrefix}`;
468+
471469
if (terminateCircularRelationships) {
472470
const relationshipsToOmitInit =
473471
terminateCircularRelationships === 'immediate' ? '_relationshipsToOmit' : 'new Set(_relationshipsToOmit)';
@@ -476,7 +474,7 @@ export const ${toMockName(
476474
typeName,
477475
casedName,
478476
prefix,
479-
)} = (overrides?: Partial<${casedNameWithPrefix}>, _relationshipsToOmit: Set<string> = new Set()): ${typenameReturnType}${casedNameWithPrefix} => {
477+
)} = (${overridesArgumentString}, _relationshipsToOmit: Set<string> = new Set()): ${typenameReturnType}${casedNameWithPrefix} => {
480478
const relationshipsToOmit: Set<string> = ${relationshipsToOmitInit};
481479
relationshipsToOmit.add('${casedName}');
482480
return {${typename}
@@ -489,7 +487,7 @@ export const ${toMockName(
489487
typeName,
490488
casedName,
491489
prefix,
492-
)} = (overrides?: Partial<${casedNameWithPrefix}>): ${typenameReturnType}${casedNameWithPrefix} => {
490+
)} = (${overridesArgumentString}): ${typenameReturnType}${casedNameWithPrefix} => {
493491
return {${typename}
494492
${fields}
495493
};
@@ -624,7 +622,8 @@ type VisitorType = { [K in keyof ASTKindToNode]?: VisitFn<ASTKindToNode[keyof AS
624622
// https://astexplorer.net
625623
// Paste your graphql schema in it, and you'll be able to see what the `astNode` will look like
626624
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+
628627
const astNode = parse(printedSchema); // Transforms the string into ASTNode
629628

630629
if ('typenames' in config) {
@@ -692,6 +691,30 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
692691
}
693692
},
694693
};
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+
695718
const visitor: VisitorType = {
696719
FieldDefinition: (node) => {
697720
const fieldName = node.name.value;
@@ -702,27 +725,8 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
702725
const value = generateMockValue({
703726
typeName,
704727
fieldName,
705-
types,
706-
typeNamesConvention,
707-
enumValuesConvention,
708-
terminateCircularRelationships: getTerminateCircularRelationshipsConfig(config),
709-
prefix: config.prefix,
710-
typesPrefix: config.typesPrefix,
711-
enumsPrefix: config.enumsPrefix,
712728
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,
726730
});
727731

728732
return ` ${fieldName}: overrides && overrides.hasOwnProperty('${fieldName}') ? overrides.${fieldName}! : ${value},`;
@@ -735,50 +739,49 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
735739
return {
736740
typeName: fieldName,
737741
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+
}
771773

772774
return getMockString(
773775
fieldName,
774-
mockFields,
776+
mockFieldsString,
775777
typeNamesConvention,
776778
getTerminateCircularRelationshipsConfig(config),
777779
false,
778780
config.prefix,
779781
config.typesPrefix,
780782
transformUnderscore,
781783
typeNamesMapping,
784+
hasOneOfDirective,
782785
);
783786
},
784787
};

0 commit comments

Comments
 (0)