Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion apps/events/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ import { Id } from '@graphprotocol/grc-20';
import type { Mapping } from '@graphprotocol/hypergraph-react';

export const mapping: Mapping = {
RelationEntry: {
typeIds: [Id.Id('8f151ba4-de20-4e3c-9cb4-99ddf96f48f1')],
properties: {
name: Id.Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
},
},
Event: {
typeIds: [Id.Id('4d876b81-787e-41fc-ab5d-075d4da66a3f')],
properties: {
name: Id.Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
// description: Id.Id('9b1f76ff-9711-404c-861e-59dc3fa7d037'),
description: Id.Id('9b1f76ff-9711-404c-861e-59dc3fa7d037'),
},
relations: {
any: Id.Id('8f151ba4-de20-4e3c-9cb4-99ddf96f48f1'),
},
},
// Todo2: {
Expand Down
2 changes: 1 addition & 1 deletion apps/events/src/routes/playground.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const Route = createLazyFileRoute('/playground')({

function RouteComponent() {
return (
<HypergraphSpaceProvider space="aa84b08d-779a-495c-93f1-44e667baf6d7" mapping={mapping}>
<HypergraphSpaceProvider space="93952dae-46e3-4682-b290-80028ca95403" mapping={mapping}>
<div className="flex flex-col gap-4 max-w-(--breakpoint-sm) mx-auto py-8">
<h1 className="text-2xl font-bold">Playground</h1>
<Playground />
Expand Down
7 changes: 6 additions & 1 deletion apps/events/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ export class Todo2 extends Entity.Class<Todo2>('Todo2')({
website: Type.Url,
}) {}

export class RelationEntry extends Entity.Class<RelationEntry>('RelationEntry')({
name: Type.Text,
}) {}

export class Event extends Entity.Class<Event>('Event')({
name: Type.Text,
// description: Type.Text,
description: Type.Text,
// publishDate: Type.Text,
any: Type.Relation(RelationEntry),
}) {}
6 changes: 2 additions & 4 deletions packages/hypergraph-react/src/internal/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
// const ENDPOINT = 'https://kg-staging.service.pinax.network/graphql';
export const KG_ENDPOINT = 'https://kg.thegraph.com/graphql';

export const GEO_ENDPOINT = 'https://hypergraph.up.railway.app/graphql';
export const GEO_API_MAINNET_ENDPOINT = 'https://hypergraph-v2.up.railway.app//graphql';
export const GEO_API_TESTNET_ENDPOINT = 'https://hypergraph-v2-testnet.up.railway.app/graphql';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Op } from '@graphprotocol/grc-20';
import { gql, request } from 'graphql-request';
import { GEO_ENDPOINT } from './constants.js';
import { GEO_API_TESTNET_ENDPOINT } from './constants.js';

const deleteEntityQueryDocument = gql`
query entity($entityId: String!) {
Expand Down Expand Up @@ -45,7 +45,7 @@ type DeleteEntityResult = {
};

export const generateDeleteOps = async ({ id, space }: { id: string; space: string }) => {
const result = await request<DeleteEntityResult>(GEO_ENDPOINT, deleteEntityQueryDocument, {
const result = await request<DeleteEntityResult>(GEO_API_TESTNET_ENDPOINT, deleteEntityQueryDocument, {
entityId: id,
});
if (result.entity === null) {
Expand Down
134 changes: 49 additions & 85 deletions packages/hypergraph-react/src/internal/use-query-public.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,27 @@ import { gql, request } from 'graphql-request';
import { useMemo } from 'react';
import { useHypergraph } from '../HypergraphSpaceContext.js';
import type { Mapping, MappingEntry } from '../types.js';
import { GEO_ENDPOINT } from './constants.js';
import { GEO_API_TESTNET_ENDPOINT } from './constants.js';
import type { QueryPublicParams } from './types.js';

const entitiesQueryDocument = gql`
query entities($spaceId: String!, $typeId: String!, $relationTypeIds: [String!]!) {
entities(
filter: {
currentVersion: {
version: {
versionTypes: {some: {type: {entityId: {equalTo: $typeId}}}}
versionSpaces: {some: {spaceId: {equalTo: $spaceId}}}
}
}
query entities($spaceId: String!) {
entities(spaceId: $spaceId, filter: {}) {
id
name
values {
propertyId
value
}
) {
nodes {
id
name
currentVersion {
versionId
version {
triples {
nodes {
attributeId
textValue
booleanValue
numberValue
valueType
unitOption
}
}
relationsByFromVersionId(filter: {typeOfId: {in: $relationTypeIds}}) {
nodes {
toEntity {
nodeId
id
name
}
typeOf {
id
name
}
}
}
relations {
to {
id
name
}
type {
id
entity {
name
}
}
}
Expand All @@ -59,39 +36,25 @@ query entities($spaceId: String!, $typeId: String!, $relationTypeIds: [String!]!

type EntityQueryResult = {
entities: {
nodes: {
id: string;
name: string;
currentVersion: {
versionId: string;
version: {
triples: {
nodes: {
attributeId: string;
textValue: string;
booleanValue: boolean;
numberValue: number;
valueType: 'TEXT' | 'CHECKBOX' | 'POINT' | 'URL' | 'TIME' | 'NUMBER';
unitOption: unknown;
}[];
};
relationsByFromVersionId: {
nodes: {
typeOf: {
name: string;
id: string;
};
toEntity: {
nodeId: string;
id: string;
name: string;
};
}[];
};
id: string;
name: string;
values: {
propertyId: string;
value: string;
}[];
relations: {
to: {
id: string;
name: string;
};
type: {
id: string;
entity: {
name: string;
};
};
}[];
};
}[];
};

export const parseResult = <S extends Entity.AnyNoContext>(
Expand All @@ -104,33 +67,33 @@ export const parseResult = <S extends Entity.AnyNoContext>(
const data: Entity.Entity<S>[] = [];
const invalidEntities: Record<string, unknown>[] = [];

for (const queryEntity of queryData.entities.nodes) {
const queryEntityVersion = queryEntity.currentVersion.version;
for (const queryEntity of queryData.entities) {
const rawEntity: Record<string, string | boolean | number | unknown[] | URL | Date> = {
id: queryEntity.id,
};

// take the mappingEntry and assign the attributes to the rawEntity
for (const [key, value] of Object.entries(mappingEntry?.properties ?? {})) {
const property = queryEntityVersion.triples.nodes.find((a) => a.attributeId === value);
const property = queryEntity.values.find((a) => a.propertyId === value);
if (property) {
if (type.fields[key] === Type.Checkbox) {
rawEntity[key] = property.booleanValue;
rawEntity[key] = Boolean(property.value);
} else if (type.fields[key] === Type.Point) {
rawEntity[key] = property.textValue;
rawEntity[key] = property.value;
} else if (type.fields[key] === Type.Url) {
rawEntity[key] = property.textValue;
rawEntity[key] = property.value;
} else if (type.fields[key] === Type.Date) {
rawEntity[key] = property.textValue;
rawEntity[key] = property.value;
} else if (type.fields[key] === Type.Number) {
rawEntity[key] = Number(property.textValue);
rawEntity[key] = Number(property.value);
} else {
rawEntity[key] = property.textValue;
rawEntity[key] = property.value;
}
}
}

for (const [key, relationId] of Object.entries(mappingEntry?.relations ?? {})) {
const properties = queryEntityVersion.relationsByFromVersionId.nodes.filter((a) => a.typeOf.id === relationId);
const properties = queryEntity.relations.filter((a) => a.type.id === relationId);
if (properties.length === 0) {
rawEntity[key] = [] as unknown[];
continue;
Expand Down Expand Up @@ -158,9 +121,9 @@ export const parseResult = <S extends Entity.AnyNoContext>(
}

const newRelationEntities = properties.map((property) => ({
id: property.toEntity.id,
name: property.toEntity.name,
type: relationMappingEntry.typeIds[0],
id: property.to.id,
name: property.to.name,
type: property.type.id,
// TODO: should be determined by the actual value
__deleted: false,
// TODO: should be determined by the actual value
Expand All @@ -181,7 +144,8 @@ export const parseResult = <S extends Entity.AnyNoContext>(
const decodeResult = decode({
...rawEntity,
__deleted: false,
__version: queryEntity.currentVersion.versionId,
// __version: queryEntity.currentVersion.versionId,
__version: '',
});

if (Either.isRight(decodeResult)) {
Expand Down Expand Up @@ -215,7 +179,7 @@ export const useQueryPublic = <S extends Entity.AnyNoContext>(type: S, params?:
const result = useQueryTanstack({
queryKey: [`entities:geo:${typeName}`],
queryFn: async () => {
const result = await request<EntityQueryResult>(GEO_ENDPOINT, entitiesQueryDocument, {
const result = await request<EntityQueryResult>(GEO_API_TESTNET_ENDPOINT, entitiesQueryDocument, {
spaceId: space,
typeId: mappingEntry?.typeIds[0],
relationTypeIds,
Expand Down
Loading