-
Notifications
You must be signed in to change notification settings - Fork 8
typesync improvements #295
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,6 +117,7 @@ export class SchemaGenerator extends Effect.Service<SchemaGenerator>()('/typesyn | |
), | ||
Effect.andThen(() => updatePackageJson(app, directory)), | ||
Effect.andThen(() => fs.writeFileString(path.join(directory, 'src', 'schema.ts'), buildSchemaFile(app))), | ||
Effect.andThen(() => fs.writeFileString(path.join(directory, 'src', 'mapping.ts'), buildMappingFile(app))), | ||
Effect.andThen(() => cleanup), | ||
); | ||
|
||
|
@@ -290,9 +291,55 @@ ${fieldStrings.join(',\n')} | |
|
||
function buildSchemaFile(schema: Domain.InsertAppSchema) { | ||
const importStatement = `import { Entity, Type } from '@graphprotocol/hypergraph';`; | ||
|
||
const typeDefinitions = schema.types | ||
.map(typeDefinitionToString) | ||
.filter((def) => def != null) | ||
.join('\n\n'); | ||
return [importStatement, typeDefinitions].join('\n\n'); | ||
} | ||
|
||
export function buildMappingFile(schema: Domain.InsertAppSchema) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Many of the properties won't have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spot on, created an issue here #301 can you pick it up? otherwise I do it as soon as I resolved the other urgent tasks |
||
const importStatement1 = `import { Id } from '@graphprotocol/grc-20';`; | ||
const importStatement2 = `import type { Mapping } from '@graphprotocol/hypergraph';`; | ||
|
||
const typeMappings: string[] = []; | ||
|
||
for (const type of schema.types) { | ||
const properties: string[] = []; | ||
const relations: string[] = []; | ||
|
||
// Process properties and relations | ||
for (const property of type.properties) { | ||
if (Domain.isDataTypeRelation(property.dataType)) { | ||
// This is a relation | ||
relations.push(` ${Utils.toCamelCase(property.name)}: Id.Id('${property.knowledgeGraphId}')`); | ||
} else { | ||
// This is a regular property | ||
properties.push(` ${Utils.toCamelCase(property.name)}: Id.Id('${property.knowledgeGraphId}')`); | ||
} | ||
} | ||
|
||
const typeMapping = ` ${type.name}: { | ||
typeIds: [Id.Id('${type.knowledgeGraphId}')], | ||
properties: { | ||
${properties.join(',\n')}, | ||
},${ | ||
relations.length > 0 | ||
? ` | ||
relations: { | ||
${relations.join(',\n')}, | ||
},` | ||
: '' | ||
} | ||
}`; | ||
|
||
typeMappings.push(typeMapping); | ||
} | ||
|
||
const mappingString = `export const mapping: Mapping = { | ||
${typeMappings.join(',\n')}, | ||
};`; | ||
|
||
return [importStatement1, importStatement2, '', mappingString].join('\n'); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { describe, expect, it } from '@effect/vitest'; | ||
|
||
// @ts-ignore - fix the ts setup | ||
import { buildMappingFile } from '../src/Generator.js'; | ||
|
||
describe('buildMappingFile', () => { | ||
it('should build a valid mapping file', () => { | ||
const expectedMapping = `import { Id } from '@graphprotocol/grc-20'; | ||
import type { Mapping } from '@graphprotocol/hypergraph'; | ||
|
||
export const mapping: Mapping = { | ||
Space: { | ||
typeIds: [Id.Id('362c1dbd-dc64-44bb-a3c4-652f38a642d7')], | ||
properties: { | ||
name: Id.Id('a126ca53-0c8e-48d5-b888-82c734c38935'), | ||
description: Id.Id('9b1f76ff-9711-404c-861e-59dc3fa7d037'), | ||
}, | ||
}, | ||
Activity: { | ||
typeIds: [Id.Id('8275c359-4662-40fb-9aec-27177b520cd2')], | ||
properties: { | ||
name: Id.Id('a126ca53-0c8e-48d5-b888-82c734c38935'), | ||
description: Id.Id('9b1f76ff-9711-404c-861e-59dc3fa7d037'), | ||
}, | ||
relations: { | ||
relatedSpaces: Id.Id('5b722cd3-61d6-494e-8887-1310566437ba'), | ||
}, | ||
}, | ||
};`; | ||
|
||
const mapping = buildMappingFile({ | ||
name: 'test', | ||
description: 'test', | ||
directory: 'test', | ||
template: 'vite_react', | ||
types: [ | ||
{ | ||
name: 'Space', | ||
knowledgeGraphId: '362c1dbd-dc64-44bb-a3c4-652f38a642d7', | ||
properties: [ | ||
{ | ||
name: 'Name', | ||
knowledgeGraphId: 'a126ca53-0c8e-48d5-b888-82c734c38935', | ||
dataType: 'Text', | ||
}, | ||
{ | ||
name: 'Description', | ||
knowledgeGraphId: '9b1f76ff-9711-404c-861e-59dc3fa7d037', | ||
dataType: 'Text', | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'Activity', | ||
knowledgeGraphId: '8275c359-4662-40fb-9aec-27177b520cd2', | ||
properties: [ | ||
{ | ||
name: 'Name', | ||
knowledgeGraphId: 'a126ca53-0c8e-48d5-b888-82c734c38935', | ||
dataType: 'Text', | ||
}, | ||
{ | ||
name: 'Description', | ||
knowledgeGraphId: '9b1f76ff-9711-404c-861e-59dc3fa7d037', | ||
dataType: 'Text', | ||
}, | ||
{ | ||
name: 'Related spaces', | ||
knowledgeGraphId: '5b722cd3-61d6-494e-8887-1310566437ba', | ||
dataType: 'Relation(Related spaces)', | ||
relationType: 'Related spaces', | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
expect(mapping).toBe(expectedMapping); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the
listeners
object on thecreateAppForm.AppField
? It has anonChange
as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no objection to use listener
sadly I'm not familiar with the TypeSync codebase nor TanStack Forms. Is listeners recommended by Tanstack forms or are there other benefits?