Skip to content

Commit

Permalink
Fix: Generate nested CustomTypes in schema output
Browse files Browse the repository at this point in the history
  • Loading branch information
vaisshnavi7 committed Jan 15, 2025
1 parent 2e81a9d commit c54ed9b
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions packages/data-schema/src/SchemaProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ function generateInputTypes(

implicitTypes.forEach(([typeName, typeDef]) => {
if (isCustomType(typeDef)) {
const { gqlFields } = processFields(
const { gqlFields, implicitTypes: nestedTypes } = processFields(
typeName,
typeDef.data.fields,
{},
Expand All @@ -1416,6 +1416,20 @@ function generateInputTypes(
const typeKeyword = generateInputType ? 'input' : 'type';
const customType = `${typeKeyword} ${typeName}${authString ? ` ${authString}` : ''}\n{\n ${gqlFields.join('\n ')}\n}`;
generatedTypes.add(customType);

// Process nested types
if (nestedTypes.length > 0) {
const nestedGeneratedTypes = generateInputTypes(
nestedTypes,
generateInputType,
getRefType,
authRules,
true,
);
nestedGeneratedTypes.forEach((type) => {
generatedTypes.add(type);
});
}
} else if (typeDef.type === 'enum') {
const enumDefinition = `enum ${typeName} {\n ${typeDef.values.join('\n ')}\n}`;
generatedTypes.add(enumDefinition);
Expand All @@ -1441,7 +1455,7 @@ const schemaPreprocessor = (
} => {
const enumTypes = new Set<string>();
const gqlModels: string[] = [];
const inputTypes: string[] = [];
const inputTypes = new Map<string, string>();

const customQueries = [];
const customMutations = [];
Expand Down Expand Up @@ -1578,7 +1592,10 @@ const schemaPreprocessor = (
databaseType,
getRefType,
);
inputTypes.push(...operationInputTypes);
operationInputTypes.forEach((type) => {
const typeName = type.split(' ')[1];
inputTypes.set(typeName, type);
});
gqlModels.push(...operationReturnTypes);

/**
Expand All @@ -1601,13 +1618,16 @@ const schemaPreprocessor = (
const shouldGenerateInputType = !operationReturnTypes.some(
(returnType) => returnType.includes(typeName),
);
const generatedTypeDefinition = generateInputTypes(
const generatedTypeDefinitions = generateInputTypes(
[[typeName, typeDef]],
shouldGenerateInputType,
getRefType,
)[0];
);
if (shouldGenerateInputType) {
inputTypes.push(generatedTypeDefinition);
generatedTypeDefinitions.forEach((def) => {
const defName = def.split(' ')[1];
inputTypes.set(defName, def);
});
}
}
});
Expand Down Expand Up @@ -1801,7 +1821,7 @@ const schemaPreprocessor = (
schemaComponents.push(CONVERSATION_SCHEMA_GRAPHQL_TYPES);
}

schemaComponents.push(...inputTypes);
schemaComponents.push(...inputTypes.values());

const processedSchema = schemaComponents.join('\n\n');

Expand Down

0 comments on commit c54ed9b

Please sign in to comment.