Skip to content
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

Setting query name in cell generator #6442

Merged
merged 14 commits into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 32 additions & 3 deletions packages/cli/src/commands/generate/cell/cell.js
@@ -1,4 +1,5 @@
import pascalcase from 'pascalcase'
import prompts from 'prompts'

import { generate as generateTypes } from '@redwoodjs/internal/dist/generate/generate'

Expand All @@ -17,6 +18,7 @@ import {
import {
checkProjectForQueryField,
getIdType,
operationNameIsUnique,
uniqueOperationName,
} from './utils/utils'

Expand Down Expand Up @@ -61,9 +63,30 @@ export const files = async ({
templateNameSuffix = 'List'
// override operationName so that its find_operationName
}
const operationName = await uniqueOperationName(cellName, {
list: shouldGenerateList,
})

let operationName = options.query == null ? '' : options.query.trim()
if (operationName == '') {
Josh-Walker-GM marked this conversation as resolved.
Show resolved Hide resolved
operationName = await uniqueOperationName(cellName, {
list: shouldGenerateList,
})
} else {
const userSpecifiedNameIsUnique = await operationNameIsUnique(operationName)
if (!userSpecifiedNameIsUnique) {
Josh-Walker-GM marked this conversation as resolved.
Show resolved Hide resolved
const answer = await prompts([
{
type: 'confirm',
name: 'continue',
message: `Specified query name: "${operationName}" is not unique! Do you wish to continue?\r\n`,
initial: false,
active: 'Yes',
inactive: 'No',
},
])
if (!answer.continue) {
process.exit(0)
}
}
}

const cellFile = templateForComponentFile({
name: cellName,
Expand Down Expand Up @@ -152,6 +175,12 @@ export const { command, description, builder, handler } =
'Use when you want to generate a cell for a list of the model name.',
type: 'boolean',
},
query: {
default: '',
description:
'Use to enforce a specific query name within the generated cell.',
Josh-Walker-GM marked this conversation as resolved.
Show resolved Hide resolved
type: 'string',
},
},
includeAdditionalTasks: ({ name: cellName }) => {
return [
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/commands/generate/cell/utils/utils.js
Expand Up @@ -34,6 +34,11 @@ export const uniqueOperationName = async (
return uniqueOperationName(name, { index: index + 1 })
}

export const operationNameIsUnique = async (operationName) => {
const cellOperationNames = await getCellOperationNames()
return !cellOperationNames.includes(operationName)
}

export const getIdType = (model) => {
return model.fields.find((field) => field.isId)?.type
}
Expand Down