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

Give a Cell's query an operation name #867

Merged
merged 2 commits into from
Jul 20, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
],
/**
* NOTE
* Experimental decorators are used in `@redwoodjs/project-model`.
* Experimental decorators are used in `@redwoodjs/structure`.
* https://github.com/tc39/proposal-decorators
**/
['@babel/plugin-proposal-decorators', { legacy: true }],
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/commands/destroy/cell/__tests__/cell.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ jest.mock('src/lib', () => {
}
})

jest.mock('@redwoodjs/structure', () => {
return {
getProject: () => ({
cells: [{ queryOperationName: undefined }],
}),
}
})

import fs from 'fs'

import 'src/lib/test'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import path from 'path'

import { loadGeneratorFixture } from 'src/lib/test'

jest.mock('@redwoodjs/structure', () => {
return {
getProject: () => ({
cells: [{ queryOperationName: undefined }],
}),
}
})

import * as cell from '../cell'

let singleWordFiles, multiWordFiles
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const QUERY = gql`
query {
query UserProfileQuery {
userProfile {
id
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const QUERY = gql`
query {
query UserQuery {
user {
id
}
Expand Down
24 changes: 24 additions & 0 deletions packages/cli/src/commands/generate/cell/cell.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getProject } from '@redwoodjs/structure'

import {
templateForComponentFile,
createYargsForComponentGeneration,
Expand All @@ -6,13 +8,35 @@ import {
const COMPONENT_SUFFIX = 'Cell'
const REDWOOD_WEB_PATH_NAME = 'components'

const getCellOperationNames = () => {
return getProject()
.cells.map((x) => {
return x.queryOperationName
})
.filter(Boolean)
}

const uniqueOperationName = (name, index = 1) => {
let operationName = index <= 1 ? `${name}Query` : `${name}Query_${index}`
if (!getCellOperationNames().includes(operationName)) {
return operationName
}
return uniqueOperationName(name, index + 1)
}

export const files = ({ name }) => {
// Create a unique operation name.
const operationName = uniqueOperationName(name)

const cellFile = templateForComponentFile({
name,
suffix: COMPONENT_SUFFIX,
webPathSection: REDWOOD_WEB_PATH_NAME,
generator: 'cell',
templatePath: 'cell.js.template',
templateVars: {
operationName,
},
})
const testFile = templateForComponentFile({
name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const QUERY = gql`
query {
query ${operationName} {
${camelName} {
id
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getDefaultArgs } from 'src/lib'
import { yargsDefaults as defaults } from '../../../generate'
import * as scaffold from '../scaffold'

describe('in javascript (defualt) mode', () => {
describe('in javascript (default) mode', () => {
let files

beforeAll(async () => {
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.compilerOption.json",
"compilerOptions": {
"tsBuildInfoFile": "dist/tsconfig.tsbuildinfo",
"baseUrl": ".",
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"],
"references": [
{ "path": "../internal" },
{ "path": "../structure" },
]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{ "path": "packages/internal" },
{ "path": "packages/auth" },
{ "path": "packages/testing" },
{ "path": "packages/project-model" }
{ "path": "packages/structure" }
],
"files": []
}