Skip to content

Commit

Permalink
[refactor] Reduce repetition by directly assigning the needed values
Browse files Browse the repository at this point in the history
  • Loading branch information
Philzen committed Jan 13, 2025
1 parent 74baa79 commit 2278409
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions packages/cli/src/lib/schemaHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const getExistingModelName = async (name) => {
}
}

const schema = await getSchemaDefinitions()
for (let model of schema.datamodel.models) {
const schema = (await getSchemaDefinitions()).datamodel
for (let model of schema.models) {
if (model.name.toLowerCase() === modelName) {
return model.name
}
Expand All @@ -51,8 +51,9 @@ const getExistingModelName = async (name) => {
* entire schema is returned.
*/
export const getSchema = async (name) => {
const schema = (await getSchemaDefinitions()).datamodel
if (!name) {
return (await getSchemaDefinitions()).datamodel
return schema
}

const modelName = await getExistingModelName(name)
Expand All @@ -66,18 +67,15 @@ export const getSchema = async (name) => {
return schemaMemo[modelName]
}

const schema = await getSchemaDefinitions()
const model = schema.datamodel.models.find(
(model) => model.name === modelName,
)
const model = schema.models.find((model) => model.name === modelName)
if (!model) {
return undefined // can this happen, and if yes, should we prefer throwing an error?
}

// look for any fields that are enums and attach the possible enum values
// so we can put them in generated test files
model.fields.forEach((field) => {
const fieldEnum = schema.datamodel.enums.find((e) => field.type === e.name)
const fieldEnum = schema.enums.find((e) => field.type === e.name)
if (fieldEnum) {
field.enumValues = fieldEnum.values
}
Expand Down

0 comments on commit 2278409

Please sign in to comment.