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 all 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
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",
Copy link
Contributor

@jtoar jtoar Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey all, saw this change when merging the patch release branch back into main. Could we go without this change, or is this package necessary for this PR? I noticed we didn't change the CRWA template, so something may be missing if this one is necessary

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that it shows up here now.
Danny and I discovered a while ago that it's been missing as a web side dependency.
Let me try to find a PR that's relevant

Copy link
Member

@Tobbe Tobbe Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the PR
https://github.com/redwoodjs/redwood/pull/6221/files

humanize-string should not be a dep in the test project
Ackshually... Let me check again

So. That PR I linked above removed the import of humanize-string from all the scaffolded components in the test project (and any actual RW app too). Instead it moved to the new /web/src/lib/formatters.ts file.

So it should be added as a dependency to the web side as soon as you scaffold a model.

Previously it wasn't an explicit dependency on the web package. But it was a dependency on the cli package, so we got away with it.

"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