Skip to content

Commit f73f907

Browse files
lisa-assistantTobbe
authored andcommitted
fix(codegen): guard gqlorm generation behind experimental.gqlorm.enabled flag (#1677)
1 parent de688ca commit f73f907

1 file changed

Lines changed: 66 additions & 56 deletions

File tree

packages/internal/src/generate/gqlormSchema.ts

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,23 @@ export async function generateGqlormArtifacts(): Promise<{
923923
files: string[]
924924
errors: { message: string; error: unknown }[]
925925
}> {
926+
if (!getConfig().experimental?.gqlorm?.enabled) {
927+
// Clean up any stale files left over from when gqlorm was previously
928+
// enabled, so that disabling the flag removes all generated artifacts.
929+
const generatedBase = getPaths().generated.base
930+
const staleFiles = [
931+
path.join(generatedBase, 'gqlorm', 'backend.ts'),
932+
path.join(generatedBase, 'gqlorm-schema.json'),
933+
path.join(generatedBase, 'types', 'includes', 'web-gqlorm-models.d.ts'),
934+
]
935+
for (const staleFile of staleFiles) {
936+
if (fs.existsSync(staleFile)) {
937+
fs.unlinkSync(staleFile)
938+
}
939+
}
940+
return { files: [], errors: [] }
941+
}
942+
926943
const files: string[] = []
927944
const errors: { message: string; error: unknown }[] = []
928945

@@ -968,68 +985,61 @@ export async function generateGqlormArtifacts(): Promise<{
968985
const backendOutputDir = path.join(generatedBase, 'gqlorm')
969986
const backendOutputPath = path.join(backendOutputDir, 'backend.ts')
970987

971-
if (getConfig().experimental?.gqlorm?.enabled) {
972-
const graphqlDir = paths.api.graphql
973-
const existingTypes = getExistingSdlTypeNames(graphqlDir)
974-
const allModels = buildBackendModelInfo(dmmf)
975-
976-
const gqlormConfig = getConfig().experimental.gqlorm
977-
const membershipModel: string =
978-
gqlormConfig.membershipModel ?? 'Membership'
979-
const membershipModelCamel =
980-
membershipModel.charAt(0).toLowerCase() + membershipModel.slice(1)
981-
const membershipUserField: string =
982-
gqlormConfig.membershipUserField ?? 'userId'
983-
const membershipOrganizationField: string =
984-
gqlormConfig.membershipOrganizationField ?? 'organizationId'
985-
986-
// Check if membership model exists in the DMMF (not just gqlorm-visible models)
987-
const membershipModelExists = dmmf.datamodel.models.some(
988-
(m) => m.name === membershipModel,
989-
)
988+
const graphqlDir = paths.api.graphql
989+
const existingTypes = getExistingSdlTypeNames(graphqlDir)
990+
const allModels = buildBackendModelInfo(dmmf)
991+
992+
const gqlormConfig = getConfig().experimental.gqlorm
993+
const membershipModel: string = gqlormConfig.membershipModel ?? 'Membership'
994+
const membershipModelCamel =
995+
membershipModel.charAt(0).toLowerCase() + membershipModel.slice(1)
996+
const membershipUserField: string =
997+
gqlormConfig.membershipUserField ?? 'userId'
998+
const membershipOrganizationField: string =
999+
gqlormConfig.membershipOrganizationField ?? 'organizationId'
1000+
1001+
// Check if membership model exists in the DMMF (not just gqlorm-visible models)
1002+
const membershipModelExists = dmmf.datamodel.models.some(
1003+
(m) => m.name === membershipModel,
1004+
)
9901005

991-
const backendConfig: GqlormBackendConfig = {
992-
membershipModel,
993-
membershipModelCamel,
994-
membershipUserField,
995-
membershipOrganizationField,
996-
membershipModelExists,
997-
}
1006+
const backendConfig: GqlormBackendConfig = {
1007+
membershipModel,
1008+
membershipModelCamel,
1009+
membershipUserField,
1010+
membershipOrganizationField,
1011+
membershipModelExists,
1012+
}
9981013

999-
// Filter out models whose type name already exists in user-authored SDLs
1000-
const gqlormModels = allModels.filter(
1001-
(m) => !existingTypes.has(m.modelName),
1002-
)
1014+
// Filter out models whose type name already exists in user-authored SDLs
1015+
const gqlormModels = allModels.filter(
1016+
(m) => !existingTypes.has(m.modelName),
1017+
)
10031018

1004-
// Warn when org scoping can't be applied because the membership model is missing
1005-
const anyModelHasOrgField = gqlormModels.some((m) =>
1006-
m.fields.some((f) => f.name === membershipOrganizationField),
1019+
// Warn when org scoping can't be applied because the membership model is missing
1020+
const anyModelHasOrgField = gqlormModels.some((m) =>
1021+
m.fields.some((f) => f.name === membershipOrganizationField),
1022+
)
1023+
if (anyModelHasOrgField && !membershipModelExists) {
1024+
console.warn(
1025+
`[gqlorm] One or more models have a \`${membershipOrganizationField}\` field, ` +
1026+
`but the membership model "${membershipModel}" was not found in the schema. ` +
1027+
`Organization-based access scoping will not be applied for these models. ` +
1028+
`Add a \`${membershipModel}\` model to your schema.prisma or configure ` +
1029+
`\`experimental.gqlorm.membershipModel\` in cedar.toml.`,
10071030
)
1008-
if (anyModelHasOrgField && !membershipModelExists) {
1009-
console.warn(
1010-
`[gqlorm] One or more models have a \`${membershipOrganizationField}\` field, ` +
1011-
`but the membership model "${membershipModel}" was not found in the schema. ` +
1012-
`Organization-based access scoping will not be applied for these models. ` +
1013-
`Add a \`${membershipModel}\` model to your schema.prisma or configure ` +
1014-
`\`experimental.gqlorm.membershipModel\` in cedar.toml.`,
1015-
)
1016-
}
1031+
}
10171032

1018-
if (gqlormModels.length > 0) {
1019-
const backendContent = generateGqlormBackendContent(
1020-
gqlormModels,
1021-
backendConfig,
1022-
)
1023-
fs.mkdirSync(backendOutputDir, { recursive: true })
1024-
fs.writeFileSync(backendOutputPath, backendContent)
1025-
files.push(backendOutputPath)
1026-
} else {
1027-
// No models to generate — clean up stale file if it exists
1028-
if (fs.existsSync(backendOutputPath)) {
1029-
fs.unlinkSync(backendOutputPath)
1030-
}
1031-
}
1033+
if (gqlormModels.length > 0) {
1034+
const backendContent = generateGqlormBackendContent(
1035+
gqlormModels,
1036+
backendConfig,
1037+
)
1038+
fs.mkdirSync(backendOutputDir, { recursive: true })
1039+
fs.writeFileSync(backendOutputPath, backendContent)
1040+
files.push(backendOutputPath)
10321041
} else {
1042+
// No models to generate — clean up stale file if it exists
10331043
if (fs.existsSync(backendOutputPath)) {
10341044
fs.unlinkSync(backendOutputPath)
10351045
}

0 commit comments

Comments
 (0)