Skip to content

Commit ac22426

Browse files
hedgepigdanielDaniel Playfair Cal
andauthored
Fix type errors in generated code for nested input objects with @OneOf directive (#194)
Fixes #193 Skip `terminateCircularRelationships` logic for fields who's types are input objects with the `@oneOf` directive. This fixes the generation of incorrect code with type errors. --------- Co-authored-by: Daniel Playfair Cal <[email protected]>
1 parent a690ec4 commit ac22426

File tree

3 files changed

+414
-20
lines changed

3 files changed

+414
-20
lines changed

src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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) =>
@@ -409,7 +410,8 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
409410
throw `foundType is unknown: ${foundType.name}: ${foundType.type}`;
410411
}
411412
}
412-
if (opts.terminateCircularRelationships) {
413+
414+
if (opts.terminateCircularRelationships && !opts.inputOneOfTypes.has(opts.currentType.name.value)) {
413415
return handleValueGeneration(opts, null, () => {
414416
if (opts.typesPrefix) {
415417
const typeNameConverter = createNameConverter(
@@ -687,6 +689,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
687689

688690
// List of types that are enums
689691
const types: TypeItem[] = [];
692+
const inputOneOfTypes: Set<string> = new Set();
690693
const typeVisitor: VisitorType = {
691694
EnumTypeDefinition: (node) => {
692695
const name = node.name.value;
@@ -723,6 +726,11 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
723726
}
724727
}
725728
},
729+
InputObjectTypeDefinition: (node) => {
730+
if (node.directives.some((directive) => directive.name.value === 'oneOf')) {
731+
inputOneOfTypes.add(node.name.value);
732+
}
733+
},
726734
ScalarTypeDefinition: (node) => {
727735
const name = node.name.value;
728736
if (!types.find((scalarType) => scalarType.name === name)) {
@@ -755,6 +763,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
755763
typesPrefix: config.typesPrefix,
756764
useImplementingTypes,
757765
useTypeImports,
766+
inputOneOfTypes,
758767
};
759768

760769
const visitor: VisitorType = {

0 commit comments

Comments
 (0)