Skip to content

Commit 6aee811

Browse files
committed
implement Type.optional
1 parent eb5ab5e commit 6aee811

File tree

10 files changed

+64
-13
lines changed

10 files changed

+64
-13
lines changed

apps/events/src/mapping.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const mapping: Mapping = {
66
typeIds: [Id.Id('7f9562d4-034d-4385-bf5c-f02cdebba47a')],
77
properties: {
88
name: Id.Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
9+
description: Id.Id('9b1f76ff-9711-404c-861e-59dc3fa7d037'),
910
},
1011
relations: {
1112
sponsors: Id.Id('6860bfac-f703-4289-b789-972d0aaf3abe'),

apps/events/src/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ export class Company extends Entity.Class<Company>('Company')({
3333

3434
export class Event extends Entity.Class<Event>('Event')({
3535
name: Type.Text,
36-
// description: Type.Text,
36+
description: Type.optional(Type.Text),
3737
sponsors: Type.Relation(Company),
3838
}) {}

apps/typesync/client/src/Components/App/Schema/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { AppSchema } from '../../../schema.js';
22

3+
// TODO
34
function fieldToEntityString({
45
name,
56
type_name,

apps/typesync/src/Generator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { execSync } from 'node:child_process';
2-
import { readdirSync } from 'node:fs';
31
import { FileSystem, Path, type Error as PlatformError } from '@effect/platform';
42
import { NodeFileSystem } from '@effect/platform-node';
53
import { Doc } from '@effect/printer';
64
import { Mapping } from '@graphprotocol/typesync';
75
import { Cause, Console, Data, Effect, Array as EffectArray, String as EffectString } from 'effect';
6+
import { execSync } from 'node:child_process';
7+
import { readdirSync } from 'node:fs';
88

99
import type * as Domain from '../domain/Domain.js';
1010
import * as Utils from './Utils.js';
@@ -301,6 +301,7 @@ function validatePackageName(name: string): {
301301
};
302302
}
303303

304+
// TODO
304305
// --------------------
305306
// schema builder
306307
// --------------------

docs/docs/schema.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
The Hypergraph schema allows you to define the data model for your application. It is based on the GRC-20 specification and allows you to define Types with properties and relations to other Types.
44

5+
TODO update this doc
6+
57
## Example
68

79
Here is an example of a schema for an Event app with the properties `name` and `description`.

packages/hypergraph-react/src/internal/use-create-entity-public.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function useCreateEntityPublic<const S extends Entity.AnyNoContext>(
3232

3333
const fields = type.fields;
3434
const values: PropertiesParam = [];
35+
// TODO
3536
for (const [key, value] of Object.entries(mappingEntry.properties || {})) {
3637
let serializedValue: string = data[key];
3738
if (fields[key] === Type.Checkbox) {

packages/hypergraph-react/src/internal/use-query-public.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ type RecursiveQueryEntity = {
149149
}[];
150150
};
151151

152+
// TODO
152153
const convertPropertyValue = (
153154
property: { propertyId: string; value: string },
154155
key: string,

packages/hypergraph-react/src/prepare-publish.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
type RelationsParam,
88
} from '@graphprotocol/grc-20';
99
import type { Entity } from '@graphprotocol/hypergraph';
10-
import { store, Type } from '@graphprotocol/hypergraph';
10+
import { store, TypeUtils } from '@graphprotocol/hypergraph';
1111
import request, { gql } from 'graphql-request';
1212

1313
export type PreparePublishParams<S extends Entity.AnyNoContext> = {
@@ -67,15 +67,16 @@ export const preparePublish = async <S extends Entity.AnyNoContext>({
6767
const fields = entity.__schema.fields;
6868

6969
if (data?.entity === null) {
70+
// TODO
7071
for (const [key, propertyId] of Object.entries(mappingEntry.properties || {})) {
7172
let serializedValue: string = entity[key];
72-
if (fields[key] === Type.Checkbox) {
73+
if (TypeUtils.isCheckboxOrOptionalCheckboxType(fields[key])) {
7374
serializedValue = Graph.serializeCheckbox(entity[key]);
74-
} else if (fields[key] === Type.Date) {
75+
} else if (TypeUtils.isDateOrOptionalDateType(fields[key])) {
7576
serializedValue = Graph.serializeDate(entity[key]);
76-
} else if (fields[key] === Type.Point) {
77+
} else if (TypeUtils.isPointOrOptionalPointType(fields[key])) {
7778
serializedValue = Graph.serializePoint(entity[key]);
78-
} else if (fields[key] === Type.Number) {
79+
} else if (TypeUtils.isNumberOrOptionalNumberType(fields[key])) {
7980
serializedValue = Graph.serializeNumber(entity[key]);
8081
}
8182
values.push({ property: propertyId, value: serializedValue });
@@ -105,14 +106,15 @@ export const preparePublish = async <S extends Entity.AnyNoContext>({
105106

106107
if (data?.entity) {
107108
for (const [key, propertyId] of Object.entries(mappingEntry.properties || {})) {
109+
// TODO
108110
let serializedValue: string = entity[key];
109-
if (fields[key] === Type.Checkbox) {
111+
if (TypeUtils.isCheckboxOrOptionalCheckboxType(fields[key])) {
110112
serializedValue = Graph.serializeCheckbox(entity[key]);
111-
} else if (fields[key] === Type.Date) {
113+
} else if (TypeUtils.isDateOrOptionalDateType(fields[key])) {
112114
serializedValue = Graph.serializeDate(entity[key]);
113-
} else if (fields[key] === Type.Point) {
115+
} else if (TypeUtils.isPointOrOptionalPointType(fields[key])) {
114116
serializedValue = Graph.serializePoint(entity[key]);
115-
} else if (fields[key] === Type.Number) {
117+
} else if (TypeUtils.isNumberOrOptionalNumberType(fields[key])) {
116118
serializedValue = Graph.serializeNumber(entity[key]);
117119
}
118120

packages/hypergraph/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ export * as Key from './key/index.js';
66
export * as Messages from './messages/index.js';
77
export * as SpaceEvents from './space-events/index.js';
88
export * as SpaceInfo from './space-info/index.js';
9-
export * from './store.js';
109
export * as StoreConnect from './store-connect.js';
10+
export * from './store.js';
11+
export * as TypeUtils from './type-utils/type-utils.js';
1112
export * as Type from './type/type.js';
1213
export * from './types.js';
1314
export * as Utils from './utils/index.js';
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as Type from '../type/type.js';
2+
3+
// biome-ignore lint/suspicious/noExplicitAny: TODO
4+
export const isStringOrOptionalStringType = (type: any) => {
5+
if (type.ast && type.ast._tag === 'PropertySignatureDeclaration' && type.ast.isOptional) {
6+
return type === Type.Text;
7+
}
8+
return type === Type.Text;
9+
};
10+
11+
// biome-ignore lint/suspicious/noExplicitAny: TODO
12+
export const isNumberOrOptionalNumberType = (type: any) => {
13+
if (type.ast && type.ast._tag === 'PropertySignatureDeclaration' && type.ast.isOptional) {
14+
return type === Type.Number;
15+
}
16+
return type === Type.Number;
17+
};
18+
19+
// biome-ignore lint/suspicious/noExplicitAny: TODO
20+
export const isDateOrOptionalDateType = (type: any) => {
21+
if (type.ast && type.ast._tag === 'PropertySignatureDeclaration' && type.ast.isOptional) {
22+
return type === Type.Date;
23+
}
24+
return type === Type.Date;
25+
};
26+
27+
// biome-ignore lint/suspicious/noExplicitAny: TODO
28+
export const isCheckboxOrOptionalCheckboxType = (type: any) => {
29+
if (type.ast && type.ast._tag === 'PropertySignatureDeclaration' && type.ast.isOptional) {
30+
return type === Type.Checkbox;
31+
}
32+
return type === Type.Checkbox;
33+
};
34+
35+
// biome-ignore lint/suspicious/noExplicitAny: TODO
36+
export const isPointOrOptionalPointType = (type: any) => {
37+
if (type.ast && type.ast._tag === 'PropertySignatureDeclaration' && type.ast.isOptional) {
38+
return type === Type.Point;
39+
}
40+
return type === Type.Point;
41+
};

0 commit comments

Comments
 (0)