|
| 1 | +# @graphprotocol/typesync |
| 2 | + |
| 3 | +Package for generating the mappings used within the `hypergraph` ecosystem that is used to query for entities on the Knowledge Graph by the `grc-20` id's. |
| 4 | + |
| 5 | +The `Mapping` is passed to the `<HypergraphSpaceProvider>` from the [@graphprotocol/hypergraph-react](../hypergraph-react/README.md) context to inform the query layer what entities to query from the Knowledge Graph. |
| 6 | + |
| 7 | +This package exposes a function: `generateMappings` that takes the schema with an array of types, each with properties. Both `types` and `properties` have a nullable `knowledgeGraphId` UUID value. If a value is provided, the `type/property` exists on the Knowledge Graph; this value will be plugged into to the `Mapping` values. If the `knowledgeGraphId` value is null, the `type/property` will be created using the `@graphprotocol/grc-20` ops and then returned in the mapping. |
| 8 | + |
| 9 | +## Mapping definition |
| 10 | + |
| 11 | +```ts |
| 12 | +export type MappingEntry = { |
| 13 | + typeIds: Array<Grc20Id.Id>; |
| 14 | + properties?: { |
| 15 | + [key: string]: Grc20Id.Id; |
| 16 | + }; |
| 17 | + relations?: { |
| 18 | + [key: string]: Grc20Id.Id; |
| 19 | + }; |
| 20 | +}; |
| 21 | + |
| 22 | +export type Mapping = { |
| 23 | + [key: string]: MappingEntry; |
| 24 | +}; |
| 25 | +``` |
| 26 | + |
| 27 | +## Example |
| 28 | + |
| 29 | +- generated schema |
| 30 | + |
| 31 | +```ts |
| 32 | +import { Entity, Type } from '@graphprotocol/hypergraph' |
| 33 | + |
| 34 | +export class Account extends Entity.Class<Account>('Account')({ |
| 35 | + username: Type.Text, |
| 36 | + createdAt: Type.Date |
| 37 | +}) {} |
| 38 | + |
| 39 | +export class Event extends Entity.Class<Event>('Event')({ |
| 40 | + name: Type.Text, |
| 41 | + description: Type.Text, |
| 42 | + account: Type.Relation(Account) |
| 43 | +}) {} |
| 44 | +``` |
| 45 | + |
| 46 | +- resuling mapping |
| 47 | + |
| 48 | +```ts |
| 49 | +import { Id } from '@graphprotocol/grc-20' |
| 50 | +import type { Mapping } from '@graphprotocol/typesync/Mapping' |
| 51 | + |
| 52 | +export const mapping: Mapping = { |
| 53 | + Account: { |
| 54 | + typeIds: [Id.Id('a5fd07b1-120f-46c6-b46f-387ef98396a6')], |
| 55 | + properties: { |
| 56 | + username: Id.Id('994edcff-6996-4a77-9797-a13e5e3efad8'), |
| 57 | + createdAt: Id.Id('64bfba51-a69b-4746-be4b-213214a879fe') |
| 58 | + } |
| 59 | + }, |
| 60 | + Event: { |
| 61 | + typeIds: [Id.Id('0349187b-526f-435f-b2bb-9e9caf23127a')], |
| 62 | + properties: { |
| 63 | + name: Id.Id('3808e060-fb4a-4d08-8069-35b8c8a1902b'), |
| 64 | + description: Id.Id('1f0d9007-8da2-4b28-ab9f-3bc0709f4837'), |
| 65 | + }, |
| 66 | + relations: { |
| 67 | + account: Id.Id('a5fd07b1-120f-46c6-b46f-387ef98396a6') |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## References |
| 74 | + |
| 75 | +- [@graphprotocol/grc-20](https://github.com/graphprotocol/grc-20-ts) |
0 commit comments