From fc19bde8743c3f1b55e30b0bfdc1096e04022c68 Mon Sep 17 00:00:00 2001 From: Josh GM Walker <56300765+Josh-Walker-GM@users.noreply.github.com> Date: Mon, 26 Sep 2022 13:19:39 +0100 Subject: [PATCH] Setting query name in cell generator (#6442) Co-authored-by: Daniel Choudhury --- .../migration.sql | 0 .../migration.sql | 0 .../api/src/services/posts/posts.scenarios.ts | 4 +-- .../api/src/services/users/users.scenarios.ts | 4 +-- __fixtures__/test-project/web/package.json | 3 +- .../generate/cell/__tests__/cell.test.js | 33 ++++++++++++++++++- .../cli/src/commands/generate/cell/cell.js | 24 ++++++++++++-- .../src/commands/generate/cell/utils/utils.js | 5 +++ 8 files changed, 64 insertions(+), 9 deletions(-) rename __fixtures__/test-project/api/db/migrations/{20220918091756_create_post_user => 20220926103708_create_post_user}/migration.sql (100%) rename __fixtures__/test-project/api/db/migrations/{20220918091807_create_contact => 20220926103723_create_contact}/migration.sql (100%) diff --git a/__fixtures__/test-project/api/db/migrations/20220918091756_create_post_user/migration.sql b/__fixtures__/test-project/api/db/migrations/20220926103708_create_post_user/migration.sql similarity index 100% rename from __fixtures__/test-project/api/db/migrations/20220918091756_create_post_user/migration.sql rename to __fixtures__/test-project/api/db/migrations/20220926103708_create_post_user/migration.sql diff --git a/__fixtures__/test-project/api/db/migrations/20220918091807_create_contact/migration.sql b/__fixtures__/test-project/api/db/migrations/20220926103723_create_contact/migration.sql similarity index 100% rename from __fixtures__/test-project/api/db/migrations/20220918091807_create_contact/migration.sql rename to __fixtures__/test-project/api/db/migrations/20220926103723_create_contact/migration.sql diff --git a/__fixtures__/test-project/api/src/services/posts/posts.scenarios.ts b/__fixtures__/test-project/api/src/services/posts/posts.scenarios.ts index db7e35d2dd4a..6f23115cdf32 100644 --- a/__fixtures__/test-project/api/src/services/posts/posts.scenarios.ts +++ b/__fixtures__/test-project/api/src/services/posts/posts.scenarios.ts @@ -10,7 +10,7 @@ export const standard = defineScenario({ body: 'String', author: { create: { - email: 'String4857147', + email: 'String7848705', hashedPassword: 'String', fullName: 'String', salt: 'String', @@ -24,7 +24,7 @@ export const standard = defineScenario({ body: 'String', author: { create: { - email: 'String1125871', + email: 'String1920513', hashedPassword: 'String', fullName: 'String', salt: 'String', diff --git a/__fixtures__/test-project/api/src/services/users/users.scenarios.ts b/__fixtures__/test-project/api/src/services/users/users.scenarios.ts index 26207f6aaaad..e029dff0c869 100644 --- a/__fixtures__/test-project/api/src/services/users/users.scenarios.ts +++ b/__fixtures__/test-project/api/src/services/users/users.scenarios.ts @@ -6,7 +6,7 @@ export const standard = defineScenario({ user: { one: { data: { - email: 'String4815975', + email: 'String7590248', hashedPassword: 'String', fullName: 'String', salt: 'String', @@ -14,7 +14,7 @@ export const standard = defineScenario({ }, two: { data: { - email: 'String3376651', + email: 'String2966925', hashedPassword: 'String', fullName: 'String', salt: 'String', diff --git a/__fixtures__/test-project/web/package.json b/__fixtures__/test-project/web/package.json index 5df1628f6c4a..cacf5a5e12ce 100644 --- a/__fixtures__/test-project/web/package.json +++ b/__fixtures__/test-project/web/package.json @@ -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", diff --git a/packages/cli/src/commands/generate/cell/__tests__/cell.test.js b/packages/cli/src/commands/generate/cell/__tests__/cell.test.js index 89285bdf3659..cb61b2b31c8f 100644 --- a/packages/cli/src/commands/generate/cell/__tests__/cell.test.js +++ b/packages/cli/src/commands/generate/cell/__tests__/cell.test.js @@ -8,7 +8,7 @@ import * as cell from '../cell' jest.mock('@redwoodjs/structure', () => { return { getProject: () => ({ - cells: [{ queryOperationName: undefined }], + cells: [{ queryOperationName: 'AlreadyDefinedQueryName' }], }), } }) @@ -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') + }) + +}) diff --git a/packages/cli/src/commands/generate/cell/cell.js b/packages/cli/src/commands/generate/cell/cell.js index e0c4843b4950..d380ed453a40 100644 --- a/packages/cli/src/commands/generate/cell/cell.js +++ b/packages/cli/src/commands/generate/cell/cell.js @@ -17,6 +17,7 @@ import { import { checkProjectForQueryField, getIdType, + operationNameIsUnique, uniqueOperationName, } from './utils/utils' @@ -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, @@ -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 [ diff --git a/packages/cli/src/commands/generate/cell/utils/utils.js b/packages/cli/src/commands/generate/cell/utils/utils.js index 67fbfa8ee40f..347bcd0ebfe4 100644 --- a/packages/cli/src/commands/generate/cell/utils/utils.js +++ b/packages/cli/src/commands/generate/cell/utils/utils.js @@ -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 }