Skip to content

Commit a5eaefe

Browse files
author
Daniel Playfair Cal
committed
simplify implementation
1 parent 9b0d2aa commit a5eaefe

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

src/index.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parse, TypeNode, ASTKindToNode, ListTypeNode, NamedTypeNode, ObjectTypeDefinitionNode, Kind } from 'graphql';
1+
import { parse, TypeNode, ASTKindToNode, ListTypeNode, NamedTypeNode, ObjectTypeDefinitionNode } from 'graphql';
22
import * as allFakerLocales from '@faker-js/faker';
33
import casual from 'casual';
44
import { oldVisit, PluginFunction, resolveExternalModuleAndFn } from '@graphql-codegen/plugin-helpers';
@@ -34,6 +34,7 @@ type Options<T = TypeNode> = {
3434
defaultNullableToNull: boolean;
3535
nonNull: boolean;
3636
typeNamesMapping?: Record<string, string>;
37+
inputOneOfTypes: Set<string>;
3738
};
3839

3940
const getTerminateCircularRelationshipsConfig = ({ terminateCircularRelationships }: TypescriptMocksPluginConfig) =>
@@ -410,7 +411,7 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
410411
}
411412
}
412413

413-
if (opts.terminateCircularRelationships) {
414+
if (opts.terminateCircularRelationships && !opts.inputOneOfTypes.has(opts.currentType.name.value)) {
414415
return handleValueGeneration(opts, null, () => {
415416
if (opts.typesPrefix) {
416417
const typeNameConverter = createNameConverter(
@@ -688,6 +689,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
688689

689690
// List of types that are enums
690691
const types: TypeItem[] = [];
692+
const inputOneOfTypes: Set<string> = new Set();
691693
const typeVisitor: VisitorType = {
692694
EnumTypeDefinition: (node) => {
693695
const name = node.name.value;
@@ -724,6 +726,11 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
724726
}
725727
}
726728
},
729+
InputObjectTypeDefinition: (node) => {
730+
if (node.directives.some((directive) => directive.name.value === 'oneOf')) {
731+
inputOneOfTypes.add(node.name.value);
732+
}
733+
},
727734
ScalarTypeDefinition: (node) => {
728735
const name = node.name.value;
729736
if (!types.find((scalarType) => scalarType.name === name)) {
@@ -756,6 +763,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
756763
typesPrefix: config.typesPrefix,
757764
useImplementingTypes,
758765
useTypeImports,
766+
inputOneOfTypes,
759767
};
760768

761769
const visitor: VisitorType = {
@@ -802,28 +810,12 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
802810
} else if (node.fields) {
803811
mockFieldsString = node.fields
804812
.map((field) => {
805-
const typeName = field.type.kind === Kind.NAMED_TYPE ? field.type.name.value : null;
806-
const fieldIsOneOfType = Boolean(
807-
typeName &&
808-
astNode.definitions.find(
809-
(definition) =>
810-
definition.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION &&
811-
definition.name.value === typeName &&
812-
definition.directives.some(
813-
(directive) => directive.name.value === 'oneOf',
814-
),
815-
),
816-
);
817-
818813
const value = generateMockValue({
819814
typeName: fieldName,
820815
fieldName: field.name.value,
821816
currentType: field.type,
822817
generatorMode: 'input',
823818
...sharedGenerateMockOpts,
824-
terminateCircularRelationships: fieldIsOneOfType
825-
? false
826-
: sharedGenerateMockOpts.terminateCircularRelationships,
827819
});
828820

829821
const valueWithOverride = `overrides && overrides.hasOwnProperty('${field.name.value}') ? overrides.${field.name.value}! : ${value}`;

0 commit comments

Comments
 (0)