@cedarjs/cli 6.0.1-next.0 → 7.0.0-canary.2982

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -183,7 +183,7 @@ You'll manually need to merge it with your existing entry.client${ext} file.`;
183
183
  fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2));
184
184
  }
185
185
  },
186
- addWebPackages(["@apollo/client-react-streaming@0.10.0"]),
186
+ addWebPackages(["@apollo/client-react-streaming@0.14.5"]),
187
187
  {
188
188
  task: () => {
189
189
  printTaskEpilogue(command, description, EXPERIMENTAL_TOPIC_ID);
@@ -22,6 +22,26 @@ const builder = createBuilder({
22
22
  default: "",
23
23
  description: "Use to enforce a specific query name within the generated cell - must be unique.",
24
24
  type: "string"
25
+ },
26
+ beforeQuery: {
27
+ default: false,
28
+ description: "Include a typed `beforeQuery` stub for configuring the query (e.g. variables, fetch policy).",
29
+ type: "boolean"
30
+ },
31
+ afterQuery: {
32
+ default: false,
33
+ description: "Include a typed `afterQuery` stub for sanitizing data returned from the query.",
34
+ type: "boolean"
35
+ },
36
+ isEmpty: {
37
+ default: false,
38
+ description: "Include a typed `isEmpty` stub for overriding the default check for the Empty component.",
39
+ type: "boolean"
40
+ },
41
+ fragment: {
42
+ default: "",
43
+ description: "Generate a fragment cell (exports FRAGMENT instead of QUERY) that reads its data from a parent Cell, given the GraphQL type it selects from - e.g. --fragment User.",
44
+ type: "string"
25
45
  }
26
46
  };
27
47
  }
@@ -26,7 +26,11 @@ const files = async ({
26
26
  list = false,
27
27
  query,
28
28
  stories,
29
- tests
29
+ tests,
30
+ beforeQuery = false,
31
+ afterQuery = false,
32
+ isEmpty = false,
33
+ fragment = ""
30
34
  }) => {
31
35
  let cellName = removeGeneratorName(name, "cell");
32
36
  let idName = "id";
@@ -35,22 +39,49 @@ const files = async ({
35
39
  let model = null;
36
40
  let templateNameSuffix = "";
37
41
  let typeName = cellName;
38
- const shouldGenerateList = (isWordPluralizable(cellName) ? isPlural(cellName) : list) || list;
39
- try {
40
- model = await getSchema(pascalcase(singularize(cellName)));
41
- idName = getIdName(model);
42
- idType = getIdType(model);
43
- typeName = model.name;
44
- mockIdValues = idType === "String" ? mockIdValues.map((value) => `'${value}'`) : mockIdValues;
45
- } catch {
46
- idType = "Int";
42
+ if (fragment) {
43
+ if (list) {
44
+ throw new Error(
45
+ "The --list flag cannot be combined with --fragment; fragment cells always render a single item."
46
+ );
47
+ }
48
+ if (beforeQuery) {
49
+ throw new Error(
50
+ "The --before-query flag cannot be combined with --fragment; fragment cells never fire a query of their own."
51
+ );
52
+ }
53
+ if (query) {
54
+ throw new Error(
55
+ "The --query flag cannot be combined with --fragment; fragment cells don't have an operation name."
56
+ );
57
+ }
58
+ }
59
+ const shouldGenerateList = !fragment && ((isWordPluralizable(cellName) ? isPlural(cellName) : list) || list);
60
+ if (!fragment) {
61
+ try {
62
+ model = await getSchema(pascalcase(singularize(cellName)));
63
+ idName = getIdName(model);
64
+ idType = getIdType(model);
65
+ typeName = model.name;
66
+ mockIdValues = idType === "String" ? mockIdValues.map((value) => `'${value}'`) : mockIdValues;
67
+ } catch {
68
+ idType = "Int";
69
+ }
47
70
  }
48
71
  if (shouldGenerateList) {
49
72
  cellName = forcePluralizeWord(cellName);
50
73
  templateNameSuffix = "List";
51
74
  }
52
75
  let operationName = query;
53
- if (operationName) {
76
+ let fragmentName;
77
+ let fragmentOnType;
78
+ let fragmentPropName;
79
+ if (fragment) {
80
+ const fragmentTypeVariants = nameVariants(fragment);
81
+ fragmentOnType = fragmentTypeVariants.pascalName;
82
+ fragmentPropName = fragmentTypeVariants.camelName;
83
+ fragmentName = `${nameVariants(cellName).pascalName}Cell_${fragmentPropName}`;
84
+ } else if (operationName) {
54
85
  const userSpecifiedOperationNameIsUnique = await operationNameIsUnique(operationName);
55
86
  if (!userSpecifiedOperationNameIsUnique) {
56
87
  throw new Error(`Specified query name: "${operationName}" is not unique!`);
@@ -67,11 +98,20 @@ const files = async ({
67
98
  extension,
68
99
  webPathSection: CEDAR_WEB_PATH_NAME,
69
100
  generator: "cell",
70
- templatePath: `cell${templateNameSuffix}.tsx.template`,
71
- templateVars: {
101
+ templatePath: fragment ? "cellFragment.tsx.template" : `cell${templateNameSuffix}.tsx.template`,
102
+ templateVars: fragment ? {
103
+ fragmentName,
104
+ fragmentOnType,
105
+ camelName: fragmentPropName,
106
+ afterQuery,
107
+ isEmpty
108
+ } : {
72
109
  operationName,
73
110
  idName,
74
- idType
111
+ idType,
112
+ beforeQuery,
113
+ afterQuery,
114
+ isEmpty
75
115
  }
76
116
  });
77
117
  const testFile = await templateForComponentFile({
@@ -80,8 +120,8 @@ const files = async ({
80
120
  extension: `.test${extension}`,
81
121
  webPathSection: CEDAR_WEB_PATH_NAME,
82
122
  generator: "cell",
83
- templatePath: "test.js.template",
84
- templateVars: {
123
+ templatePath: fragment ? "testFragment.js.template" : "test.js.template",
124
+ templateVars: fragment ? { camelName: fragmentPropName } : {
85
125
  idName: shouldGenerateList ? void 0 : idName,
86
126
  mockIdValues: shouldGenerateList ? void 0 : mockIdValues
87
127
  }
@@ -92,7 +132,7 @@ const files = async ({
92
132
  extension: `.stories${extension}`,
93
133
  webPathSection: CEDAR_WEB_PATH_NAME,
94
134
  generator: "cell",
95
- templatePath: "stories.tsx.template"
135
+ templatePath: fragment ? "storiesFragment.tsx.template" : "stories.tsx.template"
96
136
  });
97
137
  const mockFile = await templateForComponentFile({
98
138
  name: cellName,
@@ -101,7 +141,12 @@ const files = async ({
101
141
  webPathSection: CEDAR_WEB_PATH_NAME,
102
142
  generator: "cell",
103
143
  templatePath: `mock${templateNameSuffix}.ts.template`,
104
- templateVars: {
144
+ templateVars: fragment ? {
145
+ idName,
146
+ mockIdValues,
147
+ typeName: fragmentOnType,
148
+ camelName: fragmentPropName
149
+ } : {
105
150
  idName,
106
151
  mockIdValues,
107
152
  typeName
@@ -122,7 +167,10 @@ const files = async ({
122
167
  const handler = createHandler({
123
168
  componentName: "cell",
124
169
  filesFn: files,
125
- includeAdditionalTasks: ({ name: cellName }) => {
170
+ includeAdditionalTasks: ({
171
+ name: cellName,
172
+ fragment
173
+ }) => {
126
174
  return [
127
175
  {
128
176
  title: `Generating types ...`,
@@ -130,7 +178,7 @@ const handler = createHandler({
130
178
  const queryFieldName = nameVariants(
131
179
  removeGeneratorName(cellName, "cell")
132
180
  ).camelName;
133
- const projectHasSdl = await checkProjectForQueryField(queryFieldName);
181
+ const projectHasSdl = Boolean(fragment) || await checkProjectForQueryField(queryFieldName);
134
182
  if (projectHasSdl) {
135
183
  const { errors } = await generateTypes();
136
184
  for (const { message, error } of errors) {
@@ -6,7 +6,9 @@ import type {
6
6
  import type {
7
7
  CellSuccessProps,
8
8
  CellFailureProps,
9
- TypedDocumentNode,
9
+ TypedDocumentNode,<% if (beforeQuery) { %>
10
+ CellBeforeQueryResult,<% } %><% if (afterQuery || isEmpty) { %>
11
+ DataObject,<% } %>
10
12
  } from '@cedarjs/web'
11
13
 
12
14
  export const QUERY: TypedDocumentNode<
@@ -19,7 +21,24 @@ export const QUERY: TypedDocumentNode<
19
21
  }
20
22
  }
21
23
  `
22
-
24
+ <% if (beforeQuery) { %>
25
+ export const beforeQuery = (
26
+ props: ${operationName}Variables,
27
+ ): CellBeforeQueryResult<${operationName}Variables> => {
28
+ return { variables: props, fetchPolicy: 'cache-and-network' }
29
+ }
30
+ <% } %><% if (isEmpty) { %>
31
+ export const isEmpty = (
32
+ data: DataObject,
33
+ { isDataEmpty }: { isDataEmpty: (data: DataObject) => boolean },
34
+ ) => {
35
+ return isDataEmpty(data)
36
+ }
37
+ <% } %><% if (afterQuery) { %>
38
+ export const afterQuery = (data: DataObject): DataObject => {
39
+ return data
40
+ }
41
+ <% } %>
23
42
  export const Loading = () => <div>Loading...</div>
24
43
 
25
44
  export const Empty = () => <div>Empty</div>
@@ -0,0 +1,43 @@
1
+ import type { ${fragmentName} } from 'types/graphql'
2
+
3
+ import type {
4
+ CellSuccessProps,<% if (afterQuery || isEmpty) { %>
5
+ DataObject,<% } %>
6
+ } from '@cedarjs/web'
7
+
8
+ // A parent Cell spreads this fragment by name in its own QUERY, and passes
9
+ // the matching field down as the `${camelName}` prop - no import needed,
10
+ // just render <${pascalName}Cell />:
11
+ //
12
+ // query FindSomething($id: Int!) {
13
+ // something(id: $id) {
14
+ // id
15
+ // ...${fragmentName}
16
+ // }
17
+ // }
18
+ //
19
+ // <${pascalName}Cell ${camelName}={something} />
20
+ export const FRAGMENT = gql`
21
+ fragment ${fragmentName} on ${fragmentOnType} {
22
+ id
23
+ }
24
+ `
25
+ <% if (isEmpty) { %>
26
+ export const isEmpty = (
27
+ data: DataObject,
28
+ { isDataEmpty }: { isDataEmpty: (data: DataObject) => boolean },
29
+ ) => {
30
+ return isDataEmpty(data)
31
+ }
32
+ <% } %><% if (afterQuery) { %>
33
+ export const afterQuery = (data: DataObject): DataObject => {
34
+ return data
35
+ }
36
+ <% } %>
37
+ export const Empty = () => <div>Empty</div>
38
+
39
+ export const Success = ({
40
+ ${camelName},
41
+ }: CellSuccessProps<{ ${camelName}: ${fragmentName} }>) => {
42
+ return <div>{JSON.stringify(${camelName})}</div>
43
+ }
@@ -3,7 +3,9 @@ import type { ${operationName}, ${operationName}Variables } from 'types/graphql
3
3
  import type {
4
4
  CellSuccessProps,
5
5
  CellFailureProps,
6
- TypedDocumentNode,
6
+ TypedDocumentNode,<% if (beforeQuery) { %>
7
+ CellBeforeQueryResult,<% } %><% if (afterQuery || isEmpty) { %>
8
+ DataObject,<% } %>
7
9
  } from '@cedarjs/web'
8
10
 
9
11
  export const QUERY: TypedDocumentNode<
@@ -16,7 +18,24 @@ export const QUERY: TypedDocumentNode<
16
18
  }
17
19
  }
18
20
  `
19
-
21
+ <% if (beforeQuery) { %>
22
+ export const beforeQuery = (
23
+ props: ${operationName}Variables,
24
+ ): CellBeforeQueryResult<${operationName}Variables> => {
25
+ return { variables: props, fetchPolicy: 'cache-and-network' }
26
+ }
27
+ <% } %><% if (isEmpty) { %>
28
+ export const isEmpty = (
29
+ data: DataObject,
30
+ { isDataEmpty }: { isDataEmpty: (data: DataObject) => boolean },
31
+ ) => {
32
+ return isDataEmpty(data)
33
+ }
34
+ <% } %><% if (afterQuery) { %>
35
+ export const afterQuery = (data: DataObject): DataObject => {
36
+ return data
37
+ }
38
+ <% } %>
20
39
  export const Loading = () => <div>Loading...</div>
21
40
 
22
41
  export const Empty = () => <div>Empty</div>
@@ -0,0 +1,23 @@
1
+ import type { Meta, StoryObj } from '@storybook/react'
2
+
3
+ import { Empty, Success } from './${pascalName}Cell'
4
+ import { standard } from './${pascalName}Cell.mock'
5
+
6
+ const meta: Meta = {
7
+ title: 'Cells/${pascalName}Cell',
8
+ tags: ['autodocs']
9
+ }
10
+
11
+ export default meta
12
+
13
+ export const empty: StoryObj<typeof Empty> = {
14
+ render: () => {
15
+ return Empty ? <Empty /> : <></>
16
+ }
17
+ }
18
+
19
+ export const success: StoryObj<typeof Success> = {
20
+ render: (args) => {
21
+ return Success ? <Success {...standard()} {...args} /> : <></>
22
+ }
23
+ }
@@ -0,0 +1,30 @@
1
+ import { render } from '@cedarjs/testing/web'
2
+
3
+ import { Empty, Success } from './${pascalName}Cell'
4
+ import { standard } from './${pascalName}Cell.mock'
5
+
6
+ // Generated boilerplate tests do not account for all circumstances
7
+ // and can fail without adjustments, e.g. Float and DateTime types.
8
+ // Please refer to the RedwoodJS Testing Docs:
9
+ // https://cedarjs.com/docs/testing#testing-cells
10
+ // https://cedarjs.com/docs/testing#jest-expect-type-considerations
11
+
12
+ describe('${pascalName}Cell', () => {
13
+ it('renders Empty successfully', async () => {
14
+ expect(() => {
15
+ render(<Empty />)
16
+ }).not.toThrow()
17
+ })
18
+
19
+ // When you're ready to test the actual output of your component render
20
+ // you could test that, for example, certain text is present:
21
+ //
22
+ // 1. import { screen } from '@cedarjs/testing/web'
23
+ // 2. Add test: expect(screen.getByText('Hello, world')).toBeInTheDocument()
24
+
25
+ it('renders Success successfully', async () => {
26
+ expect(() => {
27
+ render(<Success ${camelName}={standard().${camelName}} />)
28
+ }).not.toThrow()
29
+ })
30
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/cli",
3
- "version": "6.0.1-next.0",
3
+ "version": "7.0.0-canary.2982",
4
4
  "description": "The CedarJS Command Line",
5
5
  "repository": {
6
6
  "type": "git",
@@ -36,17 +36,17 @@
36
36
  "@babel/preset-typescript": "7.29.7",
37
37
  "@babel/traverse": "7.29.8",
38
38
  "@babel/types": "7.29.8",
39
- "@cedarjs/api-server": "6.0.1-next.0",
40
- "@cedarjs/babel-config": "6.0.1-next.0",
41
- "@cedarjs/cli-helpers": "6.0.1-next.0",
42
- "@cedarjs/internal": "6.0.1-next.0",
43
- "@cedarjs/prerender": "6.0.1-next.0",
44
- "@cedarjs/project-config": "6.0.1-next.0",
45
- "@cedarjs/structure": "6.0.1-next.0",
46
- "@cedarjs/telemetry": "6.0.1-next.0",
47
- "@cedarjs/utils": "6.0.1-next.0",
48
- "@cedarjs/vite": "6.0.1-next.0",
49
- "@cedarjs/web-server": "6.0.1-next.0",
39
+ "@cedarjs/api-server": "7.0.0-canary.2982",
40
+ "@cedarjs/babel-config": "7.0.0-canary.2982",
41
+ "@cedarjs/cli-helpers": "7.0.0-canary.2982",
42
+ "@cedarjs/internal": "7.0.0-canary.2982",
43
+ "@cedarjs/prerender": "7.0.0-canary.2982",
44
+ "@cedarjs/project-config": "7.0.0-canary.2982",
45
+ "@cedarjs/structure": "7.0.0-canary.2982",
46
+ "@cedarjs/telemetry": "7.0.0-canary.2982",
47
+ "@cedarjs/utils": "7.0.0-canary.2982",
48
+ "@cedarjs/vite": "7.0.0-canary.2982",
49
+ "@cedarjs/web-server": "7.0.0-canary.2982",
50
50
  "@listr2/prompt-adapter-enquirer": "4.3.0",
51
51
  "@opentelemetry/api": "1.9.1",
52
52
  "@opentelemetry/core": "1.30.1",
@@ -94,7 +94,7 @@
94
94
  "yargs": "17.7.3"
95
95
  },
96
96
  "devDependencies": {
97
- "@cedarjs/framework-tools": "6.0.1-next.0",
97
+ "@cedarjs/framework-tools": "7.0.0-canary.2982",
98
98
  "@prisma/dmmf": "7.8.0",
99
99
  "@types/archiver": "^7.0.0",
100
100
  "memfs": "4.68.1",