Skip to content

Commit

Permalink
Setting query name in cell generator (#6442)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Choudhury <dannychoudhury@gmail.com>
  • Loading branch information
Josh-Walker-GM and dac09 committed Sep 26, 2022
1 parent 8d93b75 commit fc19bde
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 9 deletions.
Expand Up @@ -10,7 +10,7 @@ export const standard = defineScenario<Prisma.PostCreateArgs>({
body: 'String',
author: {
create: {
email: 'String4857147',
email: 'String7848705',
hashedPassword: 'String',
fullName: 'String',
salt: 'String',
Expand All @@ -24,7 +24,7 @@ export const standard = defineScenario<Prisma.PostCreateArgs>({
body: 'String',
author: {
create: {
email: 'String1125871',
email: 'String1920513',
hashedPassword: 'String',
fullName: 'String',
salt: 'String',
Expand Down
Expand Up @@ -6,15 +6,15 @@ export const standard = defineScenario<Prisma.UserCreateArgs>({
user: {
one: {
data: {
email: 'String4815975',
email: 'String7590248',
hashedPassword: 'String',
fullName: 'String',
salt: 'String',
},
},
two: {
data: {
email: 'String3376651',
email: 'String2966925',
hashedPassword: 'String',
fullName: 'String',
salt: 'String',
Expand Down
3 changes: 2 additions & 1 deletion __fixtures__/test-project/web/package.json
Expand Up @@ -17,12 +17,13 @@
"@redwoodjs/forms": "3.0.2",
"@redwoodjs/router": "3.0.2",
"@redwoodjs/web": "3.0.2",
"humanize-string": "2.1.0",
"prop-types": "15.8.1",
"react": "17.0.2",
"react-dom": "17.0.2"
},
"devDependencies": {
"autoprefixer": "^10.4.11",
"autoprefixer": "^10.4.12",
"postcss": "^8.4.16",
"postcss-loader": "^7.0.1",
"prettier-plugin-tailwindcss": "^0.1.13",
Expand Down
33 changes: 32 additions & 1 deletion packages/cli/src/commands/generate/cell/__tests__/cell.test.js
Expand Up @@ -8,7 +8,7 @@ import * as cell from '../cell'
jest.mock('@redwoodjs/structure', () => {
return {
getProject: () => ({
cells: [{ queryOperationName: undefined }],
cells: [{ queryOperationName: 'AlreadyDefinedQueryName' }],
}),
}
})
Expand Down Expand Up @@ -636,3 +636,34 @@ test('generates list a cell with a string primary id keys', () => {
expect(modelWithStringIdList[STORY_PATH]).toMatchSnapshot()
expect(modelWithStringIdList[MOCK_PATH]).toMatchSnapshot()
})

describe('Custom query names', () => {
test('Accepts custom query names', async() => {
const generatedFiles = await cell.files({
name: 'Clues',
tests: false,
stories: false,
query: 'FindBluesClues'
})



const CELL_PATH = path.normalize(
'/path/to/project/web/src/components/CluesCell/CluesCell.js'
)


expect(generatedFiles[CELL_PATH]).toContain('query FindBluesClues {')
})


test('Throws if a duplicated query name is used', async() => {
await expect(cell.files({
name: 'Clues',
tests: false,
stories: false,
query: 'AlreadyDefinedQueryName'
})).rejects.toThrow('Specified query name: "AlreadyDefinedQueryName" is not unique')
})

})
24 changes: 21 additions & 3 deletions packages/cli/src/commands/generate/cell/cell.js
Expand Up @@ -17,6 +17,7 @@ import {
import {
checkProjectForQueryField,
getIdType,
operationNameIsUnique,
uniqueOperationName,
} from './utils/utils'

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

let operationName = options.query
if (operationName) {
const userSpecifiedOperationNameIsUnique = await operationNameIsUnique(
operationName
)
if (!userSpecifiedOperationNameIsUnique) {
throw new Error(`Specified query name: "${operationName}" is not unique!`)
}
} else {
operationName = await uniqueOperationName(cellName, {
list: shouldGenerateList,
})
}

const cellFile = templateForComponentFile({
name: cellName,
Expand Down Expand Up @@ -152,6 +164,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 - must be unique.',
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

0 comments on commit fc19bde

Please sign in to comment.