Skip to content

Commit 6ce7676

Browse files
authored
extend id filter to support in (#600)
1 parent 5ced7e7 commit 6ce7676

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

.changeset/extend-id-filter-in.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphprotocol/hypergraph": patch
3+
---
4+
5+
Add `in` operator support to entity id filter, allowing filtering by multiple ids (e.g. `id: { in: [id1, id2] }`)

packages/hypergraph/src/entity/types.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,15 @@ export type EntityStringFilter = {
7070
contains?: string;
7171
};
7272

73-
export type EntityIdFilter = {
74-
is?: string;
75-
};
73+
export type EntityIdFilter =
74+
| {
75+
is: string;
76+
in?: never;
77+
}
78+
| {
79+
in: readonly string[];
80+
is?: never;
81+
};
7682

7783
export type RelationEntityIdFilter =
7884
| {

packages/hypergraph/src/utils/translate-filter-to-graphql.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ type GraphqlFilterEntry =
5353
};
5454
}
5555
| {
56-
id: {
57-
is: string;
58-
};
56+
id: EntityIdGraphqlFilter;
5957
}
6058
| { [k: string]: never };
6159

@@ -95,10 +93,14 @@ export function translateFilterToGraphql<S extends Schema.Schema.AnyNoContext>(
9593

9694
if (fieldName === 'id') {
9795
const idFilter = fieldFilter as Entity.EntityIdFilter;
98-
if (typeof idFilter.is === 'string') {
96+
if ('is' in idFilter && typeof idFilter.is === 'string') {
9997
graphqlFilter.push({
10098
id: { is: idFilter.is },
10199
});
100+
} else if ('in' in idFilter && Array.isArray(idFilter.in)) {
101+
graphqlFilter.push({
102+
id: { in: idFilter.in },
103+
});
102104
}
103105
continue;
104106
}

packages/hypergraph/test/utils/translate-filter-to-graphql.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ describe('translateFilterToGraphql id filters', () => {
189189
});
190190
});
191191

192+
it('should translate id `in` filter correctly', () => {
193+
const filter: TodoFilter = {
194+
id: { in: ['entity-id-1', 'entity-id-2'] },
195+
};
196+
197+
const result = translateFilterToGraphql(filter, Todo);
198+
199+
expect(result).toEqual({
200+
id: { in: ['entity-id-1', 'entity-id-2'] },
201+
});
202+
});
203+
192204
it('should combine id filter with other property filters', () => {
193205
const filter: TodoFilter = {
194206
id: { is: 'entity-id' },

0 commit comments

Comments
 (0)