|
1 |
| -import { parse, TypeNode, ASTKindToNode, ListTypeNode, NamedTypeNode, ObjectTypeDefinitionNode, Kind } from 'graphql'; |
| 1 | +import { parse, TypeNode, ASTKindToNode, ListTypeNode, NamedTypeNode, ObjectTypeDefinitionNode } from 'graphql'; |
2 | 2 | import * as allFakerLocales from '@faker-js/faker';
|
3 | 3 | import casual from 'casual';
|
4 | 4 | import { oldVisit, PluginFunction, resolveExternalModuleAndFn } from '@graphql-codegen/plugin-helpers';
|
@@ -34,6 +34,7 @@ type Options<T = TypeNode> = {
|
34 | 34 | defaultNullableToNull: boolean;
|
35 | 35 | nonNull: boolean;
|
36 | 36 | typeNamesMapping?: Record<string, string>;
|
| 37 | + inputOneOfTypes: Set<string>; |
37 | 38 | };
|
38 | 39 |
|
39 | 40 | const getTerminateCircularRelationshipsConfig = ({ terminateCircularRelationships }: TypescriptMocksPluginConfig) =>
|
@@ -410,7 +411,7 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
|
410 | 411 | }
|
411 | 412 | }
|
412 | 413 |
|
413 |
| - if (opts.terminateCircularRelationships) { |
| 414 | + if (opts.terminateCircularRelationships && !opts.inputOneOfTypes.has(opts.currentType.name.value)) { |
414 | 415 | return handleValueGeneration(opts, null, () => {
|
415 | 416 | if (opts.typesPrefix) {
|
416 | 417 | const typeNameConverter = createNameConverter(
|
@@ -688,6 +689,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
|
688 | 689 |
|
689 | 690 | // List of types that are enums
|
690 | 691 | const types: TypeItem[] = [];
|
| 692 | + const inputOneOfTypes: Set<string> = new Set(); |
691 | 693 | const typeVisitor: VisitorType = {
|
692 | 694 | EnumTypeDefinition: (node) => {
|
693 | 695 | const name = node.name.value;
|
@@ -724,6 +726,11 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
|
724 | 726 | }
|
725 | 727 | }
|
726 | 728 | },
|
| 729 | + InputObjectTypeDefinition: (node) => { |
| 730 | + if (node.directives.some((directive) => directive.name.value === 'oneOf')) { |
| 731 | + inputOneOfTypes.add(node.name.value); |
| 732 | + } |
| 733 | + }, |
727 | 734 | ScalarTypeDefinition: (node) => {
|
728 | 735 | const name = node.name.value;
|
729 | 736 | if (!types.find((scalarType) => scalarType.name === name)) {
|
@@ -756,6 +763,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
|
756 | 763 | typesPrefix: config.typesPrefix,
|
757 | 764 | useImplementingTypes,
|
758 | 765 | useTypeImports,
|
| 766 | + inputOneOfTypes, |
759 | 767 | };
|
760 | 768 |
|
761 | 769 | const visitor: VisitorType = {
|
@@ -802,28 +810,12 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
|
802 | 810 | } else if (node.fields) {
|
803 | 811 | mockFieldsString = node.fields
|
804 | 812 | .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 |
| - |
818 | 813 | const value = generateMockValue({
|
819 | 814 | typeName: fieldName,
|
820 | 815 | fieldName: field.name.value,
|
821 | 816 | currentType: field.type,
|
822 | 817 | generatorMode: 'input',
|
823 | 818 | ...sharedGenerateMockOpts,
|
824 |
| - terminateCircularRelationships: fieldIsOneOfType |
825 |
| - ? false |
826 |
| - : sharedGenerateMockOpts.terminateCircularRelationships, |
827 | 819 | });
|
828 | 820 |
|
829 | 821 | const valueWithOverride = `overrides && overrides.hasOwnProperty('${field.name.value}') ? overrides.${field.name.value}! : ${value}`;
|
|
0 commit comments