Skip to content

Commit 8e8959a

Browse files
committed
re-implement filters
1 parent 852c957 commit 8e8959a

File tree

6 files changed

+23
-44
lines changed

6 files changed

+23
-44
lines changed

apps/events/src/components/event.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const Event = ({ spaceId, entityId }: { spaceId: string; entityId: string
1313
space: spaceId,
1414
});
1515

16-
console.log({ component: 'Event', isPending, isError, data });
16+
// console.log({ component: 'Event', isPending, isError, data });
1717

1818
return (
1919
<div>

apps/events/src/components/playground.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ export const Playground = ({ spaceId }: { spaceId: string }) => {
1111
jobOffers: {},
1212
},
1313
},
14-
// filter: {
15-
// or: [{ name: { startsWith: 'test' } }, { name: { startsWith: 'ETH' } }],
16-
// },
14+
filter: {
15+
or: [{ name: { startsWith: 'My' } }, { name: { startsWith: 'ETH' } }],
16+
},
17+
// filter: { name: { startsWith: 'My Test Event' } },
1718
first: 100,
1819
space: spaceId,
1920
});

packages/hypergraph-react/src/internal/translate-filter-to-graphql.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Graph } from '@graphprotocol/grc-20';
2-
import { type Entity, TypeUtils } from '@graphprotocol/hypergraph';
3-
import { PropertyIdSymbol } from '@graphprotocol/hypergraph/constants';
2+
import { Constants, type Entity } from '@graphprotocol/hypergraph';
3+
import * as Option from 'effect/Option';
44
import type * as Schema from 'effect/Schema';
55
import * as SchemaAST from 'effect/SchemaAST';
66

@@ -72,17 +72,19 @@ export function translateFilterToGraphql<S extends Schema.Schema.AnyNoContext>(
7272
if (!propertySignature) continue;
7373

7474
// find the property id for the field
75-
const propertyId = SchemaAST.getAnnotation<string>(PropertyIdSymbol)(propertySignature.type);
76-
if (!propertyId) continue;
75+
const propertyId = SchemaAST.getAnnotation<string>(Constants.PropertyIdSymbol)(propertySignature.type);
76+
const propertyType = SchemaAST.getAnnotation<string>(Constants.PropertyTypeSymbol)(propertySignature.type);
77+
78+
if (!Option.isSome(propertyId) || !Option.isSome(propertyType)) continue;
7779

7880
if (
79-
TypeUtils.isStringOrOptionalStringType(type.fields[fieldName]) &&
81+
propertyType.value === 'string' &&
8082
(fieldFilter.is || fieldFilter.startsWith || fieldFilter.endsWith || fieldFilter.contains)
8183
) {
8284
graphqlFilter.push({
8385
values: {
8486
some: {
85-
propertyId: { is: propertyId },
87+
propertyId: { is: propertyId.value },
8688
string: fieldFilter.is
8789
? { is: fieldFilter.is }
8890
: fieldFilter.startsWith
@@ -95,25 +97,22 @@ export function translateFilterToGraphql<S extends Schema.Schema.AnyNoContext>(
9597
});
9698
}
9799

98-
if (TypeUtils.isBooleanOrOptionalBooleanType(type.fields[fieldName]) && fieldFilter.is) {
100+
if (propertyType.value === 'boolean' && fieldFilter.is) {
99101
graphqlFilter.push({
100102
values: {
101103
some: {
102-
propertyId: { is: propertyId },
104+
propertyId: { is: propertyId.value },
103105
boolean: { is: fieldFilter.is },
104106
},
105107
},
106108
});
107109
}
108110

109-
if (
110-
TypeUtils.isNumberOrOptionalNumberType(type.fields[fieldName]) &&
111-
(fieldFilter.is || fieldFilter.greaterThan || fieldFilter.lessThan)
112-
) {
111+
if (propertyType.value === 'number' && (fieldFilter.is || fieldFilter.greaterThan || fieldFilter.lessThan)) {
113112
graphqlFilter.push({
114113
values: {
115114
some: {
116-
propertyId: { is: propertyId },
115+
propertyId: { is: propertyId.value },
117116
number: fieldFilter.is
118117
? { is: Graph.serializeNumber(fieldFilter.is) }
119118
: fieldFilter.greaterThan

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,16 @@ export const parseResult = <S extends Schema.Schema.AnyNoContext>(queryData: Ent
204204
...convertRelations(queryEntity, ast),
205205
};
206206

207-
console.log('rawEntity', rawEntity);
208-
209207
const decodeResult = decode({
210208
...rawEntity,
211209
__deleted: false,
212210
// __version: queryEntity.currentVersion.versionId,
213211
__version: '',
214212
});
215213

216-
console.log('decodeResult', decodeResult);
217-
218214
if (Either.isRight(decodeResult)) {
219215
return {
216+
// injecting the schema to the entity to be able to access it in the preparePublish function
220217
data: { ...decodeResult.right, __schema: type } as Entity.Entity<S>,
221218
invalidEntity: null,
222219
};

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export const parseResult = <S extends Schema.Schema.AnyNoContext>(queryData: Ent
227227
});
228228

229229
if (Either.isRight(decodeResult)) {
230-
// TODO: do we need __schema here?
230+
// injecting the schema to the entity to be able to access it in the preparePublish function
231231
data.push({ ...decodeResult.right, __schema: type });
232232
} else {
233233
invalidEntities.push(rawEntity);
@@ -267,13 +267,16 @@ export const useQueryPublic = <S extends Schema.Schema.AnyNoContext>(type: S, pa
267267
queryDocument = entitiesQueryDocumentLevel2;
268268
}
269269

270+
const filterResult = filter ? translateFilterToGraphql(filter, type) : {};
271+
console.log('filterResult', filterResult);
272+
270273
const result = await request<EntityQueryResult>(`${Graph.TESTNET_API_ORIGIN}/graphql`, queryDocument, {
271274
spaceId: space,
272275
typeIds,
273276
relationTypeIdsLevel1: relationTypeIds.level1,
274277
relationTypeIdsLevel2: relationTypeIds.level2,
275278
first,
276-
filter: filter ? translateFilterToGraphql(filter, type) : {},
279+
filter: filterResult,
277280
});
278281
return result;
279282
},

packages/hypergraph/src/type-utils/type-utils.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)