@powerhousedao/academy 2.5.0-dev.2 → 2.5.0-dev.21
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.
- package/CHANGELOG.md +124 -0
- package/docs/academy/01-GetStarted/00-ExploreDemoPackage.md +18 -10
- package/docs/academy/01-GetStarted/01-CreateNewPowerhouseProject.md +36 -39
- package/docs/academy/01-GetStarted/02-DefineToDoListDocumentModel.md +21 -6
- package/docs/academy/01-GetStarted/03-ImplementOperationReducers.md +5 -0
- package/docs/academy/01-GetStarted/04-BuildToDoListEditor.md +148 -420
- package/docs/academy/01-GetStarted/_04-BuildToDoListEditor +495 -0
- package/docs/academy/01-GetStarted/home.mdx +15 -15
- package/docs/academy/02-MasteryTrack/02-DocumentModelCreation/01-WhatIsADocumentModel.md +11 -2
- package/docs/academy/02-MasteryTrack/02-DocumentModelCreation/02-SpecifyTheStateSchema.md +5 -0
- package/docs/academy/02-MasteryTrack/03-BuildingUserExperiences/01-BuildingDocumentEditors.md +5 -37
- package/docs/academy/02-MasteryTrack/05-Launch/02-PublishYourProject.md +70 -5
- package/docs/academy/02-MasteryTrack/05-Launch/03-SetupEnvironment.md +159 -73
- package/docs/academy/02-MasteryTrack/05-Launch/{03-RunOnACloudServer.md → _03-RunOnACloudServer} +8 -5
- package/docs/academy/04-APIReferences/00-PowerhouseCLI.md +7 -40
- package/docs/academy/06-ComponentLibrary/00-DocumentEngineering.md +67 -21
- package/docs/academy/06-ComponentLibrary/02-CreateCustomScalars.md +403 -0
- package/docs/academy/06-ComponentLibrary/{02-BuildingWithScalars.md → 02-ScalarComponent.mdx} +10 -12
- package/docs/academy/06-ComponentLibrary/{04-Complex-Components/01-sidebar.mdx → 03-ComplexComponent.mdx} +3 -1
- package/docs/academy/06-ComponentLibrary/03-IntegrateIntoAReactComponent.md +1 -0
- package/docs/academy/06-ComponentLibrary/04-LayoutComponent.mdx +5 -0
- package/docs/academy/06-ComponentLibrary/05-FragmentsComponent.mdx +5 -0
- package/docs/academy/07-Cookbook.md +0 -4
- package/docs/academy/08-Glossary.md +4 -1
- package/docs/academy/09-AIResources +23 -0
- package/package.json +1 -1
- package/sidebars.ts +6 -45
- package/docs/academy/06-ComponentLibrary/03-Scalar-Components/01-phid-field.mdx +0 -72
- package/docs/academy/06-ComponentLibrary/03-Scalar-Components/02-input-field.mdx +0 -0
- package/docs/academy/06-ComponentLibrary/05-Layout-Components/01-test-toupdate.mdx +0 -61
- package/docs/academy/06-ComponentLibrary/06-Fragments/01-test-toupdate.mdx +0 -61
- /package/docs/academy/02-MasteryTrack/05-Launch/{02-IntroductionToPackages.md → 01-IntroductionToPackages.md} +0 -0
- /package/docs/academy/02-MasteryTrack/05-Launch/{00-IntegrateInAFront-End → _00-IntegrateInAFront-End} +0 -0
- /package/docs/academy/02-MasteryTrack/05-Launch/{01-IntroducingFusion → _01-IntroducingFusion} +0 -0
- /package/docs/academy/02-MasteryTrack/05-Launch/{04-GraphQLNamespacing → _04-GraphQLNamespacing} +0 -0
- /package/docs/academy/02-MasteryTrack/05-Launch/{05-LaunchYourBackend.md → _05-LaunchYourBackend} +0 -0
- /package/docs/academy/02-MasteryTrack/05-Launch/{06-LaunchYourFrontend.md → _06-LaunchYourFrontend} +0 -0
- /package/docs/academy/04-APIReferences/{01-ReactHooks.md → 01-ReactHooks} +0 -0
- /package/docs/academy/04-APIReferences/{02-ReactorAPI.md → 02-ReactorAPI} +0 -0
- /package/docs/academy/04-APIReferences/{03-Configuration.md → 03-Configuration} +0 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Document-Engineering
|
|
2
2
|
|
|
3
|
-
The reusable components in the Document-Engineering
|
|
3
|
+
The reusable components in the Document-Engineering system are a set of of front-end components based on graphQL scalars.
|
|
4
4
|
Powerhouse also has a set of custom scalars that are not part of the graphQL standard but are specific to the web3 ecosystem.
|
|
5
|
-
These components are offered through the **Powerhouse Document-Engineering
|
|
5
|
+
These components are offered through the **Powerhouse Document-Engineering system** with the help of storybook & the Academy documentation.
|
|
6
6
|
|
|
7
7
|
It provides a collection of pre-built, reusable UI components designed for consistency and efficiency across Powerhouse applications and editors. Think of it as a toolkit of standard UI elements like buttons, inputs, and checkboxes with many of these components based on graphql scalars.
|
|
8
8
|
|
|
@@ -19,29 +19,19 @@ In the next few chapters of our documentation, we will start with the simplest s
|
|
|
19
19
|
|
|
20
20
|
## **3 types of components**
|
|
21
21
|
|
|
22
|
-
1. **
|
|
22
|
+
1. **Scalar Component** has a simple scalar value as input
|
|
23
23
|
- Component, Composition of smaller UI controls (e.g. Scalar component)
|
|
24
|
-
|
|
25
24
|
2. **Complex Component** has an object/array value
|
|
26
25
|
- Group of components (e.g. sidebar tree view)
|
|
27
|
-
|
|
28
26
|
3. **Layout Component** will contain other components
|
|
29
27
|
- Containers for other components, Sections (e.g. list of other components, color layouts, etc.)
|
|
28
|
+
4. **fragments Component**
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
1. **Component Context**
|
|
34
|
-
2. **Scalar Definition**
|
|
35
|
-
3. **Component Storybook Base Example**
|
|
36
|
-
- Component Code
|
|
37
|
-
- Component Default Props
|
|
38
|
-
4. **Component Storybook Usage Examples**
|
|
30
|
+
For each of these components an implementation example will be given in our documentation.
|
|
39
31
|
|
|
40
32
|
## Exploring Components with Storybook
|
|
41
33
|
|
|
42
|
-
We use Storybook as an interactive catalog for our design system components. It allows you to visually explore each component, interact with different states, and understand how to integrate them into your projects.
|
|
43
|
-
|
|
44
|
-
[https://storybook.powerhouse.academy](https://storybook.powerhouse.academy)
|
|
34
|
+
We use Storybook as an interactive catalog for our design system components. It allows you to visually explore each component, interact with different states, and understand how to integrate them into your projects. [https://storybook.powerhouse.academy](https://storybook.powerhouse.academy)
|
|
45
35
|
|
|
46
36
|
**Understanding the Storybook Interface:**
|
|
47
37
|
|
|
@@ -54,8 +44,8 @@ We use Storybook as an interactive catalog for our design system components. It
|
|
|
54
44
|
Let's walk through the typical workflow for using a component from the document-engineering system, using the `Checkbox` from the [To-do List editor](/academy/GetStarted/BuildToDoListEditor).
|
|
55
45
|
|
|
56
46
|
1. **Identify the Need:** While building your feature (e.g., the To-do List editor in `editor.tsx`), you determine the need for a standard UI element, like a checkbox.
|
|
57
|
-
2. **Consult the
|
|
58
|
-
* Open the Powerhouse Storybook instance.
|
|
47
|
+
2. **Consult the Document Engineering Components in Storybook:**
|
|
48
|
+
* Open the Powerhouse Storybook instance. [https://storybook.powerhouse.academy](https://storybook.powerhouse.academy)
|
|
59
49
|
* Navigate or search to find the `Checkbox` component.
|
|
60
50
|
* Review the visual examples and interactive demo.
|
|
61
51
|
* Examine the "Usage" snippet and the **Props table** to understand the basic implementation and available configuration options (`label`, `value`, `onChange`, etc.).
|
|
@@ -85,10 +75,66 @@ Let's walk through the typical workflow for using a component from the document-
|
|
|
85
75
|
You configure the component's appearance and behavior by passing the appropriate values to its props.
|
|
86
76
|
5. **Test and Refine:** Run your application (e.g., using `ph connect`) to see the component in context. Verify its appearance and functionality.
|
|
87
77
|
|
|
88
|
-
|
|
78
|
+
## Usage
|
|
79
|
+
|
|
80
|
+
The Document Engineering package provides several entry points for different use cases in your powerhouse project:
|
|
81
|
+
|
|
82
|
+
### Main Package
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { ... } from '@powerhousedao/document-engineering';
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### UI Components
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { ... } from '@powerhousedao/document-engineering/ui';
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Scalars
|
|
95
|
+
|
|
96
|
+
For data manipulation and transformation utilities:
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
import { ... } from '@powerhousedao/document-engineering/scalars';
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### GraphQL
|
|
103
|
+
|
|
104
|
+
For GraphQL related utilities and schema definitions:
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import { ... } from '@powerhousedao/document-engineering/graphql';
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Styles
|
|
111
|
+
|
|
112
|
+
To include the package's styles:
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import '@powerhousedao/document-engineering/style.css';
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Import Maps
|
|
119
|
+
|
|
120
|
+
Within the project, the following import maps are available:
|
|
121
|
+
|
|
122
|
+
- `#assets` - Assets utilities and components
|
|
123
|
+
- `#scalars` - Scalar transformations and utilities
|
|
124
|
+
- `#ui` - UI components
|
|
125
|
+
- `#graphql` - GraphQL related utilities
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
## **Storybook vs. Source Code:**
|
|
89
129
|
|
|
90
130
|
Storybook serves as essential documentation and a usage guide. Our developers write Storybook "stories" to demonstrate components and document their common props. However, the **ultimate source of truth** for a component's capabilities is its actual source code (e.g., the `.tsx` file within the `@powerhousedao/document-engineering/scalars` package).
|
|
91
131
|
While Storybook aims for accuracy, there might occasionally be discrepancies or undocumented props.
|
|
92
132
|
|
|
93
|
-
Please don't hesitate to reach out in our discord channels with any questions.
|
|
94
|
-
Happy designing!
|
|
133
|
+
Please don't hesitate to reach out in our discord channels with any questions.
|
|
134
|
+
Happy designing!
|
|
135
|
+
|
|
136
|
+
### Up next: Create Custom Scalars
|
|
137
|
+
|
|
138
|
+
You can learn how to do so in our guide on [Creating Custom Scalars](/academy/ComponentLibrary/CreateCustomScalars).
|
|
139
|
+
|
|
140
|
+
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
# Step 1: Create Custom Scalars
|
|
2
|
+
|
|
3
|
+
This tutorial provides step-by-step instructions for creating custom scalars & components, and to contributing to the document-engineering project.
|
|
4
|
+
The github repo for the Document-Engineering can be found [here](https://github.com/powerhouse-inc/document-engineering/tree/main)
|
|
5
|
+
|
|
6
|
+
:::info
|
|
7
|
+
When contributing as an open source developer please submit a pull request to the Powerhouse team.
|
|
8
|
+
Although a design or UI for your scalar is not mandatory, it definitely helps reviewers if there's a visual reference.
|
|
9
|
+
That said, not all scalars require a dedicated UI component.
|
|
10
|
+
Some scalars, like scalar Title or scalar Description, might both map to a string and use the same UI, in this case scalars plays more semantic role than a type definition.
|
|
11
|
+
:::
|
|
12
|
+
|
|
13
|
+
## Table of Contents
|
|
14
|
+
|
|
15
|
+
- [Creating New GraphQL Scalars](#creating-new-graphql-scalars)
|
|
16
|
+
- [Step 1: Create the Scalar File](#step-1-create-the-scalar-file)
|
|
17
|
+
- [Key Components to Update](#key-components-to-update)
|
|
18
|
+
- [Step 2: Register the Scalar in scalars.ts](#step-2-register-the-scalar-in-scalarts)
|
|
19
|
+
- [2.1 Add Namespace Import](#21-add-namespace-import)
|
|
20
|
+
- [2.2 Add Type Export](#22-add-type-export)
|
|
21
|
+
- [2.3 Add to Export Object](#23-add-to-export-object)
|
|
22
|
+
- [2.4 Add to Custom Scalars](#24-add-to-custom-scalars)
|
|
23
|
+
- [2.5 Add to Resolvers](#25-add-to-resolvers)
|
|
24
|
+
- [2.6 Add to Type Definitions](#26-add-to-type-definitions)
|
|
25
|
+
- [2.7 Add to Generator Type Definitions](#27-add-to-generator-type-definitions)
|
|
26
|
+
- [2.8 Add to Validation Schema](#28-add-to-validation-schema)
|
|
27
|
+
- [Step 3: Create Tests for Your Scalar](#step-3-create-tests-for-your-scalar)
|
|
28
|
+
- [Required Test Cases](#required-test-cases)
|
|
29
|
+
- [Testing Best Practices](#testing-best-practices)
|
|
30
|
+
- [Example Edge Cases for Different Scalar Types](#example-edge-cases-for-different-scalar-types)
|
|
31
|
+
- [Step 4: Validate Your Implementation](#step-4-validate-your-implementation)
|
|
32
|
+
- [Common Scalar Types](#common-scalar-types)
|
|
33
|
+
- [Tips](#tips)
|
|
34
|
+
|
|
35
|
+
## Creating New GraphQL Scalars
|
|
36
|
+
|
|
37
|
+
GraphQL scalars are custom data types that define how data is validated, serialized, and parsed. This guide will walk you through creating a new scalar in the `src/scalars/graphql/` directory.
|
|
38
|
+
|
|
39
|
+
### Step 1: Create the Scalar File
|
|
40
|
+
|
|
41
|
+
Create a new TypeScript file in `src/scalars/graphql/` for your scalar. Use `EmailAddress.ts` as a reference.
|
|
42
|
+
|
|
43
|
+
**Example: Creating a `PhoneNumber.ts` scalar**
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { GraphQLError, GraphQLScalarType, type GraphQLScalarTypeConfig, Kind } from 'graphql'
|
|
47
|
+
import { z } from 'zod'
|
|
48
|
+
|
|
49
|
+
export interface ScalarType {
|
|
50
|
+
input: string
|
|
51
|
+
output: string
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const type = 'string' // TS type in string form
|
|
55
|
+
|
|
56
|
+
export const typedef = 'scalar PhoneNumber'
|
|
57
|
+
|
|
58
|
+
export const schema = z.string().regex(/^\+?[1-9]\d{1,14}$/, 'Invalid phone number format')
|
|
59
|
+
|
|
60
|
+
export const stringSchema = 'z.string().regex(/^\\+?[1-9]\\d{1,14}$/, "Invalid phone number format")'
|
|
61
|
+
|
|
62
|
+
const phoneValidation = (value: unknown): string => {
|
|
63
|
+
if (typeof value !== 'string') {
|
|
64
|
+
throw new GraphQLError(`Value is not string: ${JSON.stringify(value)}`)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const result = schema.safeParse(value)
|
|
68
|
+
|
|
69
|
+
if (result.success) return result.data
|
|
70
|
+
throw new GraphQLError(result.error.message)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const config: GraphQLScalarTypeConfig<string, string> = {
|
|
74
|
+
name: 'PhoneNumber',
|
|
75
|
+
description: 'A field whose value conforms to international phone number format.',
|
|
76
|
+
serialize: phoneValidation,
|
|
77
|
+
parseValue: phoneValidation,
|
|
78
|
+
parseLiteral: (value) => {
|
|
79
|
+
if (value.kind !== Kind.STRING) {
|
|
80
|
+
throw new GraphQLError(`Can only validate strings as phone numbers but got a: ${value.kind}`, { nodes: value })
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return phoneValidation(value.value)
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export const scalar = new GraphQLScalarType(config)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### Key Components to Update:
|
|
91
|
+
|
|
92
|
+
1. **`type`**: The TypeScript type (usually `'string'` for text-based scalars)
|
|
93
|
+
2. **`typedef`**: The GraphQL type definition (e.g., `'scalar PhoneNumber'`)
|
|
94
|
+
3. **`schema`**: Zod validation schema for your data type
|
|
95
|
+
4. **`stringSchema`**: String representation of the zod schema (used for code generation)
|
|
96
|
+
5. **Validation function**: Custom validation logic for your scalar
|
|
97
|
+
6. **`config.name`**: The name of your scalar (must match the typedef)
|
|
98
|
+
7. **`config.description`**: Human-readable description of the scalar
|
|
99
|
+
|
|
100
|
+
### Step 2: Register the Scalar in `scalars.ts`
|
|
101
|
+
|
|
102
|
+
After creating your scalar file, you need to register it in `src/scalars/graphql/scalars.ts`. This involves updating multiple sections of the file.
|
|
103
|
+
The github repo for the Document-Engineering can be found [here](https://github.com/powerhouse-inc/document-engineering/tree/main)
|
|
104
|
+
|
|
105
|
+
#### 2.1 Add Namespace Import
|
|
106
|
+
|
|
107
|
+
Add your scalar to the namespace imports section (around line 2):
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// namespace imports -- DO NOT REMOVE OR EDIT THIS COMMENT
|
|
111
|
+
import * as Amount from './Amount.js'
|
|
112
|
+
import * as AmountCrypto from './AmountCrypto.js'
|
|
113
|
+
// ... other imports ...
|
|
114
|
+
import * as PhoneNumber from './PhoneNumber.js' // ADD THIS LINE
|
|
115
|
+
import * as URLScalar from './URL.js'
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### 2.2 Add Type Export
|
|
119
|
+
|
|
120
|
+
Add the type export (around line 22):
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
// export types -- DO NOT REMOVE OR EDIT THIS COMMENT
|
|
124
|
+
export type { ScalarType as AmountScalarType } from './Amount.js'
|
|
125
|
+
// ... other type exports ...
|
|
126
|
+
export type { ScalarType as PhoneNumberScalarType } from './PhoneNumber.js' // ADD THIS LINE
|
|
127
|
+
export type { ScalarType as URLScalarType } from './URL.js'
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### 2.3 Add to Export Object
|
|
131
|
+
|
|
132
|
+
Add your scalar to the main export object (around line 40):
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
export {
|
|
136
|
+
Amount,
|
|
137
|
+
AmountCrypto,
|
|
138
|
+
// ... other exports ...
|
|
139
|
+
PhoneNumber, // ADD THIS LINE
|
|
140
|
+
URLScalar,
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
#### 2.4 Add to Custom Scalars
|
|
145
|
+
|
|
146
|
+
Add your scalar to the `customScalars` object (around line 54):
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
export const customScalars: Record<string, BasePHScalar<any>> = {
|
|
150
|
+
// ... other scalars ...
|
|
151
|
+
PhoneNumber, // ADD THIS LINE
|
|
152
|
+
URLScalar,
|
|
153
|
+
} as const
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
#### 2.5 Add to Resolvers
|
|
157
|
+
|
|
158
|
+
Add your scalar to the `resolvers` object (around line 74):
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
export const resolvers = {
|
|
162
|
+
// export resolvers -- DO NOT REMOVE OR EDIT THIS COMMENT
|
|
163
|
+
AmountTokens: AmountTokens.scalar,
|
|
164
|
+
// ... other resolvers ...
|
|
165
|
+
PhoneNumber: PhoneNumber.scalar, // ADD THIS LINE
|
|
166
|
+
Amount: Amount.scalar,
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
#### 2.6 Add to Type Definitions
|
|
171
|
+
|
|
172
|
+
Add your typedef to the `typeDefs` array (around line 90):
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
export const typeDefs = [
|
|
176
|
+
// export typedefs -- DO NOT REMOVE OR EDIT THIS COMMENT
|
|
177
|
+
AmountTokens.typedef,
|
|
178
|
+
// ... other typedefs ...
|
|
179
|
+
PhoneNumber.typedef, // ADD THIS LINE
|
|
180
|
+
Amount.typedef,
|
|
181
|
+
]
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### 2.7 Add to Generator Type Definitions
|
|
185
|
+
|
|
186
|
+
Add your scalar to the `generatorTypeDefs` object (around line 105):
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
export const generatorTypeDefs = {
|
|
190
|
+
// export generator typedefs -- DO NOT REMOVE OR EDIT THIS COMMENT
|
|
191
|
+
[AmountTokens.config.name]: AmountTokens.type,
|
|
192
|
+
// ... other entries ...
|
|
193
|
+
[PhoneNumber.config.name]: PhoneNumber.type, // ADD THIS LINE
|
|
194
|
+
[Amount.config.name]: Amount.type,
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### 2.8 Add to Validation Schema
|
|
199
|
+
|
|
200
|
+
Add your scalar to the `validationSchema` object (around line 120):
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
export const validationSchema = {
|
|
204
|
+
// export validation schema -- DO NOT REMOVE OR EDIT THIS COMMENT
|
|
205
|
+
[AmountTokens.config.name]: AmountTokens.stringSchema,
|
|
206
|
+
// ... other entries ...
|
|
207
|
+
[PhoneNumber.config.name]: PhoneNumber.stringSchema, // ADD THIS LINE
|
|
208
|
+
[Amount.config.name]: Amount.stringSchema,
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Step 3: Create Tests for Your Scalar
|
|
213
|
+
|
|
214
|
+
Every scalar must have comprehensive tests to ensure it works correctly. Create a test file in `src/scalars/graphql/test/` following the naming convention `YourScalar.test.ts`.
|
|
215
|
+
|
|
216
|
+
**Example: Creating `PhoneNumber.test.ts`**
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
import { Kind } from 'graphql'
|
|
220
|
+
import { scalar } from '../PhoneNumber.js'
|
|
221
|
+
|
|
222
|
+
describe('PhoneNumber Scalar', () => {
|
|
223
|
+
it('should serialize a phone number', () => {
|
|
224
|
+
const phoneNumber = '+1234567890'
|
|
225
|
+
|
|
226
|
+
expect(scalar.serialize(phoneNumber)).toBe(phoneNumber)
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
it('should throw an error if the value is not a string', () => {
|
|
230
|
+
const phoneNumber = 123
|
|
231
|
+
|
|
232
|
+
expect(() => scalar.serialize(phoneNumber)).toThrow()
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
it('should throw an error if the value is not a valid phone number', () => {
|
|
236
|
+
const phoneNumber = 'invalid-phone'
|
|
237
|
+
|
|
238
|
+
expect(() => scalar.serialize(phoneNumber)).toThrow()
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
it('should parse a valid phone number', () => {
|
|
242
|
+
const phoneNumber = '+1234567890'
|
|
243
|
+
|
|
244
|
+
expect(scalar.parseValue(phoneNumber)).toBe(phoneNumber)
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
it('should throw an error if parse a value that is not a valid phone number', () => {
|
|
248
|
+
const phoneNumber = 'invalid-phone'
|
|
249
|
+
|
|
250
|
+
expect(() => scalar.parseValue(phoneNumber)).toThrow()
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
it('should throw an error if parse a value that is not a string', () => {
|
|
254
|
+
const phoneNumber = 123
|
|
255
|
+
|
|
256
|
+
expect(() => scalar.parseValue(phoneNumber)).toThrow()
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
it('should parse a valid phone number from a literal', () => {
|
|
260
|
+
const phoneNumber = '+1234567890'
|
|
261
|
+
|
|
262
|
+
expect(
|
|
263
|
+
scalar.parseLiteral({
|
|
264
|
+
kind: Kind.STRING,
|
|
265
|
+
value: phoneNumber,
|
|
266
|
+
})
|
|
267
|
+
).toBe(phoneNumber)
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
it('should throw an error if parse a literal that is not a valid phone number', () => {
|
|
271
|
+
const phoneNumber = 'invalid-phone'
|
|
272
|
+
|
|
273
|
+
expect(() =>
|
|
274
|
+
scalar.parseLiteral({
|
|
275
|
+
kind: Kind.STRING,
|
|
276
|
+
value: phoneNumber,
|
|
277
|
+
})
|
|
278
|
+
).toThrow()
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
it('should throw an error if parse a literal that is not a string', () => {
|
|
282
|
+
const phoneNumber = '+1234567890'
|
|
283
|
+
|
|
284
|
+
expect(() =>
|
|
285
|
+
scalar.parseLiteral({
|
|
286
|
+
kind: Kind.INT,
|
|
287
|
+
value: phoneNumber,
|
|
288
|
+
})
|
|
289
|
+
).toThrow()
|
|
290
|
+
})
|
|
291
|
+
})
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
#### Required Test Cases
|
|
295
|
+
|
|
296
|
+
Your scalar tests should cover these essential scenarios:
|
|
297
|
+
|
|
298
|
+
##### Serialization Tests
|
|
299
|
+
|
|
300
|
+
- ✅ **Valid values**: Test that valid inputs are serialized correctly
|
|
301
|
+
- ❌ **Invalid types**: Test that non-string inputs throw errors
|
|
302
|
+
- ❌ **Invalid format**: Test that strings not matching your validation throw errors
|
|
303
|
+
|
|
304
|
+
##### Parse Value Tests
|
|
305
|
+
|
|
306
|
+
- ✅ **Valid values**: Test that valid inputs are parsed correctly
|
|
307
|
+
- ❌ **Invalid format**: Test that invalid strings throw errors
|
|
308
|
+
- ❌ **Invalid types**: Test that non-string inputs throw errors
|
|
309
|
+
|
|
310
|
+
##### Parse Literal Tests
|
|
311
|
+
|
|
312
|
+
- ✅ **Valid STRING literals**: Test that valid string literals are parsed correctly
|
|
313
|
+
- ❌ **Invalid STRING literals**: Test that invalid string literals throw errors
|
|
314
|
+
- ❌ **Non-STRING literals**: Test that non-string literal kinds (INT, FLOAT, etc.) throw errors
|
|
315
|
+
|
|
316
|
+
#### Testing Best Practices
|
|
317
|
+
|
|
318
|
+
1. **Test edge cases**: Include boundary values and common invalid inputs
|
|
319
|
+
2. **Test multiple valid formats**: If your scalar accepts different valid formats, test them all
|
|
320
|
+
3. **Use descriptive test names**: Make it clear what each test is validating
|
|
321
|
+
4. **Follow the naming convention**: `YourScalar.test.ts` in the `test/` directory
|
|
322
|
+
|
|
323
|
+
#### Example Edge Cases for Different Scalar Types
|
|
324
|
+
|
|
325
|
+
**String-based scalars (like PhoneNumber):**
|
|
326
|
+
|
|
327
|
+
```typescript
|
|
328
|
+
// Test empty string
|
|
329
|
+
expect(() => scalar.parseValue('')).toThrow()
|
|
330
|
+
|
|
331
|
+
// Test too long/short values
|
|
332
|
+
expect(() => scalar.parseValue('123')).toThrow()
|
|
333
|
+
expect(() => scalar.parseValue('+' + '1'.repeat(20))).toThrow()
|
|
334
|
+
|
|
335
|
+
// Test special characters
|
|
336
|
+
expect(() => scalar.parseValue('+1-234-567-890')).not.toThrow()
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
**Number-based scalars:**
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
// Test zero
|
|
343
|
+
expect(scalar.parseValue(0)).toBe(0)
|
|
344
|
+
|
|
345
|
+
// Test negative numbers
|
|
346
|
+
expect(() => scalar.parseValue(-1)).toThrow()
|
|
347
|
+
|
|
348
|
+
// Test decimal numbers
|
|
349
|
+
expect(scalar.parseValue(123.45)).toBe(123.45)
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
**Date-based scalars:**
|
|
353
|
+
|
|
354
|
+
```typescript
|
|
355
|
+
// Test valid ISO date
|
|
356
|
+
expect(scalar.parseValue('2023-12-25T00:00:00Z')).toBe('2023-12-25T00:00:00Z')
|
|
357
|
+
|
|
358
|
+
// Test invalid date format
|
|
359
|
+
expect(() => scalar.parseValue('25/12/2023')).toThrow()
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
### Step 4: Validate Your Implementation
|
|
363
|
+
|
|
364
|
+
After implementing your scalar and tests, make sure to:
|
|
365
|
+
|
|
366
|
+
1. **Run the tests** to ensure they all pass
|
|
367
|
+
2. **Build the project** to ensure there are no TypeScript errors
|
|
368
|
+
3. **Test GraphQL queries** that use your new scalar
|
|
369
|
+
4. **Verify code generation** works with your new scalar
|
|
370
|
+
|
|
371
|
+
### Common Scalar Types
|
|
372
|
+
|
|
373
|
+
Here are some common patterns for different types of scalars:
|
|
374
|
+
|
|
375
|
+
#### String-based Scalars
|
|
376
|
+
|
|
377
|
+
```typescript
|
|
378
|
+
export const type = 'string'
|
|
379
|
+
export const schema = z.string().min(1).max(100)
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
#### Number-based Scalars
|
|
383
|
+
|
|
384
|
+
```typescript
|
|
385
|
+
export const type = 'number'
|
|
386
|
+
export const schema = z.number().positive()
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
#### Date-based Scalars
|
|
390
|
+
|
|
391
|
+
```typescript
|
|
392
|
+
export const type = 'string'
|
|
393
|
+
export const schema = z.string().datetime()
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
### Tips
|
|
397
|
+
|
|
398
|
+
- Always follow the naming convention: use PascalCase for scalar names
|
|
399
|
+
- Include meaningful validation in your Zod schema
|
|
400
|
+
- Write clear, descriptive error messages
|
|
401
|
+
- Keep the `stringSchema` in sync with your `schema` definition
|
|
402
|
+
- Test edge cases in your validation function
|
|
403
|
+
- Update all required sections in `scalars.ts`
|
package/docs/academy/06-ComponentLibrary/{02-BuildingWithScalars.md → 02-ScalarComponent.mdx}
RENAMED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { Tabs, TabItem } from '@theme/Tabs';
|
|
2
|
+
|
|
3
|
+
# Scalar Component Example
|
|
2
4
|
|
|
3
5
|
Scalars are here to help you define custom fields in your document model schema and speed up the development process.
|
|
4
6
|
There are two applications of scalar components in the document model workflow:
|
|
@@ -10,20 +12,13 @@ There are two applications of scalar components in the document model workflow:
|
|
|
10
12
|
|
|
11
13
|
As you might know, the document model schema defines the structure of the document and serves as the backbone of the document model. The GraphQL state schema defines how data is captured with the help of types & scalars. When you define a scalar in the document model schema, you are essentially defining a new field that can be used in the document model to capture data.
|
|
12
14
|
|
|
13
|
-
In the Document Model Editor, you can find all the custom scalar types available
|
|
14
|
-
|
|
15
|
-
Insert image of the feature.
|
|
15
|
+
In the Document Model Editor, you can find all the custom scalar types available under the 'Scalars' section.
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
scalar PHID <-- imported from the design system library? not sure
|
|
19
|
-
type MyType {
|
|
20
|
-
myScalar: PHID
|
|
21
|
-
}
|
|
22
|
-
````
|
|
17
|
+
Insert image of the feature. @callme-t
|
|
23
18
|
|
|
24
19
|
## React Component Implementation in the Frontend
|
|
25
20
|
|
|
26
|
-
All of our reusable components are available in the Document-Engineering
|
|
21
|
+
All of our reusable components are available in the Document-Engineering system package.
|
|
27
22
|
This package comes as a dependency in your project when creating a new document model project.
|
|
28
23
|
````
|
|
29
24
|
import PHIDField from 'document-engineering'
|
|
@@ -51,4 +46,7 @@ return (
|
|
|
51
46
|
To make your life easier, Powerhouse has defined all useful scalars with a set of reusable code and UI components.
|
|
52
47
|
The reusable components are essentially a set of front-end components based on GraphQL scalars. Powerhouse also has a set of custom scalars that are not part of the GraphQL standard but are specific to the web3 ecosystem.
|
|
53
48
|
|
|
54
|
-
Read the next chapter to get familiar with our reusable components.
|
|
49
|
+
Read the next chapter to get familiar with our reusable components.
|
|
50
|
+
|
|
51
|
+
# PHID Field Implementation Example
|
|
52
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Integrate your Scalar into a React Component
|
|
@@ -404,7 +404,6 @@ In the "New Document" section at the bottom of the page, click the `DocumentMode
|
|
|
404
404
|
- Implementing Document Model Reducers (Details to be added)
|
|
405
405
|
|
|
406
406
|
## Further Reading
|
|
407
|
-
- [Domain Modeling Guide](/domain-modeling)
|
|
408
407
|
- [GraphQL Schema Best Practices](/academy/MasteryTrack/WorkWithData/GraphQLAtPowerhouse)
|
|
409
408
|
</details>
|
|
410
409
|
|
|
@@ -451,9 +450,6 @@ The command will output the generated reducer scaffolding code in the designated
|
|
|
451
450
|
- [Initializing a New Project & Document Model](#initializing-a-new-project-and-document-model)
|
|
452
451
|
- Generating a Document Editor
|
|
453
452
|
|
|
454
|
-
## Further Reading
|
|
455
|
-
- [Domain Modeling Guide](/domain-modeling)
|
|
456
|
-
|
|
457
453
|
</details>
|
|
458
454
|
|
|
459
455
|
<details id="updating-your-powerhouse-project-dependencies">
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
- **Powerhouse Academy** – A training platform for onboarding and upskilling SNO contributors.
|
|
18
18
|
- **Connect** – The contributor's public or private workspace, serving as the entry point for individual contributors to install apps and packages for specific business solutions.
|
|
19
19
|
- **Powergrid** – A decentralized network of reactors that sync with each other.
|
|
20
|
-
- **
|
|
20
|
+
- **Powerhouse CLI (ph)** – The command-line tool for Powerhouse project initialization, code generation, package management, and running local development environments (Connect Studio). It also manages services, ensuring the terminology aligns with the updated setup guide.
|
|
21
21
|
- **Connect App (Connect Studio)** – The primary Powerhouse application for defining document models, building/testing editors (in Studio mode), and collaborating on documents.
|
|
22
22
|
- **Document Tools** – Built-in features within Powerhouse applications (e.g., Connect) that assist with document management, inspection, and interaction, such as Operations History.
|
|
23
23
|
- **Operations History** – A Document Tool in Connect providing a chronological, immutable log of all operations on a document for traceability.
|
|
@@ -30,7 +30,9 @@
|
|
|
30
30
|
- **Actions (Document Actions)** – Typed objects representing an intent to change a document's state, dispatched to reducers, containing an operation type and input data.
|
|
31
31
|
- **API Integration (for Document Models)** – The capability of Document Models to connect with Switchboard API or external APIs, facilitating data exchange between Powerhouse applications and other systems.
|
|
32
32
|
- **Boilerplate (Powerhouse Project)** – The `ph init` command's initial project structure, providing a standard starting point for new Powerhouse packages.
|
|
33
|
+
- **Connect Build** – The output of the `ph connect build` command, which packages a Connect project into a distributable format. This build includes all necessary local/external packages, assets, and styles, and can be previewed locally with `ph connect preview` or deployed.
|
|
33
34
|
- **Data Analysis (with Document Models)** – Leveraging the structured data within Document Models, often via read models, to extract insights, generate reports, and perform analytics on operational and historical data.
|
|
35
|
+
- **Development Environment (Powerhouse)** – A local setup for developing Powerhouse applications, typically initiated with the `ph dev` command. It runs essential backend services like the Powerhouse Switchboard to enable real-time document model processing, code generation, and live updates, separate from the front-end Connect Studio.
|
|
34
36
|
- **Dispatch (in Document Models)** – The act of sending an action (representing an operation) to a document model's reducer to trigger a state update.
|
|
35
37
|
- **Document Model Editors** – An interface or UI to a document model that allows users to create and modify the data captured by the document models.
|
|
36
38
|
- **Document Model Specification** – The formal definition of a document model (state, operations), created in Connect Studio (using GraphQL SDL) and exported (e.g., `.phdm.zip`) for code generation.
|
|
@@ -39,6 +41,7 @@
|
|
|
39
41
|
- **Document Type** – A unique string identifier (e.g., `powerhouse/todolist`) for a Document Model, used by host apps to select the correct editor/logic.
|
|
40
42
|
- **Drive** – A logical container in Powerhouse for storing, organizing, and managing collections of documents.
|
|
41
43
|
- **Drive App (Custom Drive Explorer)** – A UI application, often custom, providing tailored views and interactions with documents in a Drive.
|
|
44
|
+
- **Environments (Powerhouse Environments)** – Pre-defined configurations for a project's Powerhouse dependencies, such as `dev` (development), `prod` (production/latest), and `local`. The Powerhouse CLI (`ph use` command) allows developers to easily switch between these environments to use different versions of packages (e.g., bleeding-edge, stable, or from a local monorepo).
|
|
42
45
|
- **Event History (Append-Only Log)** – An immutable, append-only log where every operation applied to a Powerhouse document is stored as an event. It provides a transparent audit trail and enables features like time travel debugging and state reconstruction.
|
|
43
46
|
- **GraphQL Scalars** – Data types used in Powerhouse document modeling (e.g., `String`, `Int`, `Currency`, `OID` for unique object IDs).
|
|
44
47
|
- **GraphQL Schema Definition Language (SDL) (for Document Models)** – Language used in Connect Studio to define a Document Model's data structure (state) and operations.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# AI Resources
|
|
2
|
+
|
|
3
|
+
We have a couple of AI resources to help you with your daily work.
|
|
4
|
+
|
|
5
|
+
## Deepwiki
|
|
6
|
+
A searchable/queriable wiki to understand our growing mono repo better.
|
|
7
|
+
https://deepwiki.com/powerhouse-inc/powerhouse
|
|
8
|
+
|
|
9
|
+
:::info
|
|
10
|
+
What is DeepWiki?
|
|
11
|
+
DeepWiki provides up-to-date documentation you can talk to, for every repo in the world. Think Deep Research for GitHub.
|
|
12
|
+
:::
|
|
13
|
+
|
|
14
|
+
## Context7
|
|
15
|
+
The Powerhouse Academy is also available as context through the context7 MCP Server.
|
|
16
|
+
https://context7.com/powerhouse-inc/powerhouse
|
|
17
|
+
|
|
18
|
+
:::info
|
|
19
|
+
What is Context7?
|
|
20
|
+
LLMs rely on outdated or generic information about the libraries you use.
|
|
21
|
+
|
|
22
|
+
Context7 pulls up-to-date, version-specific documentation and code examples directly from the source. Paste accurate, relevant documentation directly into tools like Cursor, Claude, or any LLM. Get better answers, no hallucinations and an AI that actually understands your stack.
|
|
23
|
+
:::
|