diff --git a/.changeset/fix-number-property-parsing.md b/.changeset/fix-number-property-parsing.md new file mode 100644 index 00000000..38ada01e --- /dev/null +++ b/.changeset/fix-number-property-parsing.md @@ -0,0 +1,5 @@ +--- +"@graphprotocol/hypergraph": patch +--- + +fix number, date, point and boolean property parsing when API returns value only on the string field \ No newline at end of file diff --git a/.claude/settings.local.json b/.claude/settings.local.json index f43a83b3..742604a0 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -1,6 +1,12 @@ { "permissions": { - "allow": ["Bash(pnpm lint:fix:*)", "Bash(pnpm typecheck:*)", "Bash(pnpm check:*)"], + "allow": [ + "Bash(pnpm lint:fix:*)", + "Bash(pnpm typecheck:*)", + "Bash(pnpm check:*)", + "Bash(pnpm --filter events test:script:*)", + "Bash(pnpm test:*)" + ], "deny": [], "ask": [] } diff --git a/apps/events/package.json b/apps/events/package.json index 3c6f7864..18fa6dba 100644 --- a/apps/events/package.json +++ b/apps/events/package.json @@ -6,7 +6,8 @@ "scripts": { "dev": "vite --force", "preview": "vite preview", - "typesync": "hypergraph typesync" + "typesync": "hypergraph typesync", + "test:script": "tsx test-script.ts" }, "dependencies": { "@graphprotocol/grc-20": "^0.27.0", diff --git a/apps/events/test-script.ts b/apps/events/test-script.ts new file mode 100644 index 00000000..e31b48ce --- /dev/null +++ b/apps/events/test-script.ts @@ -0,0 +1,33 @@ +import { SystemIds } from '@graphprotocol/grc-20'; +import { Config, Entity, Id, Type } from '@graphprotocol/hypergraph'; + +Config.setApiOrigin('https://testnet-api.geobrowser.io'); + +const BOUNTY_TYPE_ID = Id('327976dea5ad45769b83b7e7ec6337cf'); +const REWARD_PROPERTY_ID = Id('e8e7301136354e84b46b767e7cd530a8'); + +const Bounty = Entity.Schema( + { + name: Type.String, + description: Type.String, + reward: Type.Number, + }, + { + types: [BOUNTY_TYPE_ID], + properties: { + name: SystemIds.NAME_PROPERTY, + description: SystemIds.DESCRIPTION_PROPERTY, + reward: REWARD_PROPERTY_ID, + }, + }, +); + +async function main() { + const bounty = await Entity.findOnePublic(Bounty, { + id: '93c9d09e662840a891fefe4c505f9365', + space: 'b0043a26cb81379c1217dfd2283b67b8', + }); + console.log(bounty); +} + +main(); diff --git a/packages/hypergraph/src/entity/find-many-public.ts b/packages/hypergraph/src/entity/find-many-public.ts index 992ce4c8..ba29c2b5 100644 --- a/packages/hypergraph/src/entity/find-many-public.ts +++ b/packages/hypergraph/src/entity/find-many-public.ts @@ -165,7 +165,7 @@ export const parseResult = a.propertyId === result.value); if (value) { const rawValue = Utils.convertPropertyValue(value, propType); - if (rawValue) { + if (rawValue !== undefined) { rawEntity[String(prop.name)] = rawValue; } } diff --git a/packages/hypergraph/src/entity/find-one-public.ts b/packages/hypergraph/src/entity/find-one-public.ts index f52d0537..6736f159 100644 --- a/packages/hypergraph/src/entity/find-one-public.ts +++ b/packages/hypergraph/src/entity/find-one-public.ts @@ -97,7 +97,7 @@ const parseResult = a.propertyId === result.value); if (value) { const rawValue = Utils.convertPropertyValue(value, propType); - if (rawValue) { + if (rawValue !== undefined) { rawEntity[String(prop.name)] = rawValue; } } diff --git a/packages/hypergraph/src/utils/convert-property-value.ts b/packages/hypergraph/src/utils/convert-property-value.ts index 92874bea..96cc88c1 100644 --- a/packages/hypergraph/src/utils/convert-property-value.ts +++ b/packages/hypergraph/src/utils/convert-property-value.ts @@ -12,16 +12,44 @@ export const convertPropertyValue = ( return property.string; } if (propertyType.value === 'boolean') { - return Boolean(property.boolean); + // Handle case where boolean is stored as string in the API + if (property.boolean != null) { + return Boolean(property.boolean); + } + if (property.string != null && (property.string === '1' || property.string === '0')) { + return property.string === '1'; + } + return undefined; } if (propertyType.value === 'point') { - return property.point; + // Handle case where point is stored as string in the API + if (property.point != null) { + return property.point; + } + if (property.string != null) { + return property.string; + } + return undefined; } if (propertyType.value === 'number') { - return Number(property.number); + // Handle case where number is stored as string in the API + if (property.number != null) { + return Number(property.number); + } + if (property.string != null && property.string !== '' && !Number.isNaN(Number(property.string))) { + return Number(property.string); + } + return undefined; } if (propertyType.value === 'date') { - return property.time; + // Handle case where date is stored as string in the API + if (property.time != null) { + return property.time; + } + if (property.string != null) { + return property.time; + } + return undefined; } } }; diff --git a/packages/hypergraph/src/utils/convert-relations.ts b/packages/hypergraph/src/utils/convert-relations.ts index 6a014006..16f6a9ec 100644 --- a/packages/hypergraph/src/utils/convert-relations.ts +++ b/packages/hypergraph/src/utils/convert-relations.ts @@ -150,7 +150,7 @@ export const convertRelations = <_S extends Schema.Schema.AnyNoContext>( continue; } const rawValue = convertPropertyValue(value, propType); - if (rawValue) { + if (rawValue !== undefined) { nestedRawEntity[String(nestedProp.name)] = rawValue; } }