Self-relation does not propagate (go deeper) with other relations
First of all, this lib is awesome, saves tons of time for the projects which use GraphQL. I really hope it gets to v1 sometime :)
Now, I've found an issue: when doing self-relations, GraphQL schema adds a relation properly, but does not go deeper than that. Example:
export const asset = testSchema.table(
'asset',
{
id: uuid().notNull().primaryKey().defaultRandom(),
templateId: uuid(),
assetTypeId: uuid()
.notNull()
.references(() => assetType.id, { onDelete: 'restrict', onUpdate: 'cascade' }),
name: text().notNull(),
},
(table) => [
foreignKey({
name: 'template',
columns: [table.templateId],
foreignColumns: [table.id],
})
.onDelete('cascade')
.onUpdate('cascade'),
],
)
export const assetRelations = relations(asset, ({ many, one }) => ({
assetType: one(assetType, {
fields: [asset.assetTypeId],
references: [assetType.id],
}),
template: one(asset, { fields: [asset.templateId], references: [asset.id] }),
}))
I've tried (just in case) both Apollo and Yoga, and both behave the same way. I can query the asset, with assetType (and go deeper if there's a relation in other cases), but with a template, I can only query own asset fields, can't go deeper with assetType etc, only assetTypeId.
I've checked some code, and I see there's this logic in src/util/builders/common.ts:315:
if (
usedTables.has(tableName) || (typeof relationsDepthLimit === 'number' && currentDepth >= relationsDepthLimit)
|| !relationEntries.length
) {
return {
order,
filters,
tableFields,
relationFields: {},
} as SelectData<TWithOrder>;
}
When generating the GraphQL type for the asset table, it processes the template relation (which is a self-relation to asset). When it recursively calls generateSelectFields for the template relation, it passes updatedUsedTables which already contains 'asset'. This causes the function to return early with empty relationFields at line 316, preventing the nested assetType relation (from the example above) from being included.
What's the best approach to improve this? I suppose @Sukairo-02 was working on this, would appreciate the help.