Skip to content

Fix type errors in generated code for nested input objects with @oneOf directive #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Options<T = TypeNode> = {
defaultNullableToNull: boolean;
nonNull: boolean;
typeNamesMapping?: Record<string, string>;
inputOneOfTypes: Set<string>;
};

const getTerminateCircularRelationshipsConfig = ({ terminateCircularRelationships }: TypescriptMocksPluginConfig) =>
Expand Down Expand Up @@ -409,7 +410,8 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
throw `foundType is unknown: ${foundType.name}: ${foundType.type}`;
}
}
if (opts.terminateCircularRelationships) {

if (opts.terminateCircularRelationships && !opts.inputOneOfTypes.has(opts.currentType.name.value)) {
return handleValueGeneration(opts, null, () => {
if (opts.typesPrefix) {
const typeNameConverter = createNameConverter(
Expand Down Expand Up @@ -687,6 +689,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu

// List of types that are enums
const types: TypeItem[] = [];
const inputOneOfTypes: Set<string> = new Set();
const typeVisitor: VisitorType = {
EnumTypeDefinition: (node) => {
const name = node.name.value;
Expand Down Expand Up @@ -723,6 +726,11 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
}
}
},
InputObjectTypeDefinition: (node) => {
if (node.directives.some((directive) => directive.name.value === 'oneOf')) {
inputOneOfTypes.add(node.name.value);
}
},
ScalarTypeDefinition: (node) => {
const name = node.name.value;
if (!types.find((scalarType) => scalarType.name === name)) {
Expand Down Expand Up @@ -755,6 +763,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
typesPrefix: config.typesPrefix,
useImplementingTypes,
useTypeImports,
inputOneOfTypes,
};

const visitor: VisitorType = {
Expand Down
Loading