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
7 changes: 7 additions & 0 deletions .changeset/fast-geese-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@graphprotocol/hypergraph": patch
"@graphprotocol/hypergraph-react": patch
---

extend filter capability to check for existing relation e.g. `filter: { cover: { exists: true } }`

19 changes: 18 additions & 1 deletion apps/events/src/routes/podcasts.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEntities } from '@graphprotocol/hypergraph-react';
import { createLazyFileRoute } from '@tanstack/react-router';
import { Podcast } from '@/schema';
import { Podcast, Topic } from '@/schema';

export const Route = createLazyFileRoute('/podcasts')({
component: RouteComponent,
Expand Down Expand Up @@ -53,6 +53,23 @@ function RouteComponent() {
orderBy: { property: 'dateFounded', direction: 'asc' },
backlinksTotalCountsTypeId1: '972d201a-d780-4568-9e01-543f67b26bee',
});

const { data: topics } = useEntities(Topic, {
mode: 'public',
first: 10,
space: space,
filter: {
cover: {
exists: true,
},
},
include: {
cover: {},
},
});

console.log({ topics });

console.log({ data, isLoading, isError });
return (
<>
Expand Down
2 changes: 0 additions & 2 deletions packages/hypergraph/src/entity/find-many-public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ export const parseResult = <S extends Schema.Schema.AnyNoContext>(
...Utils.convertRelations(queryEntity, ast, relationInfoLevel1),
};

console.log('rawEntity', rawEntity);

const decodeResult = decode({
...rawEntity,
__deleted: false,
Expand Down
35 changes: 23 additions & 12 deletions packages/hypergraph/src/entity/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,34 @@ export type CrossFieldFilter<T> = {
not?: CrossFieldFilter<T>;
};

export type EntityFieldFilter<T> = {
is?: T;
} & (T extends boolean
type RelationExistsFilter<T> = [T] extends [readonly unknown[] | undefined]
? {
is?: boolean;
exists?: boolean;
}
: T extends number
: Record<never, never>;

type ScalarFieldFilter<T> = [T] extends [readonly unknown[] | undefined]
? Record<never, never>
: T extends boolean
? {
greaterThan?: number;
lessThan?: number;
is?: boolean;
}
: T extends string
: T extends number
? {
startsWith?: string;
endsWith?: string;
contains?: string;
greaterThan?: number;
lessThan?: number;
}
: Record<string, never>);
: T extends string
? {
startsWith?: string;
endsWith?: string;
contains?: string;
}
: Record<string, never>;

export type EntityFieldFilter<T> = {
is?: T;
} & RelationExistsFilter<T> &
ScalarFieldFilter<T>;

export type EntityFilter<T> = CrossFieldFilter<T>;
32 changes: 32 additions & 0 deletions packages/hypergraph/src/utils/translate-filter-to-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ type GraphqlFilterEntry =
| {
and: GraphqlFilterEntry[];
}
| {
relations: {
some: {
typeId: { is: string };
};
};
}
| { [k: string]: never };

/**
Expand All @@ -47,6 +54,14 @@ export function translateFilterToGraphql<S extends Schema.Schema.AnyNoContext>(

const graphqlFilter: GraphqlFilterEntry[] = [];

const buildRelationExistsFilter = (propertyId: string): GraphqlFilterEntry => ({
relations: {
some: {
typeId: { is: propertyId },
},
},
});

for (const [fieldName, fieldFilter] of Object.entries(filter)) {
if (fieldName === 'or') {
graphqlFilter.push({
Expand Down Expand Up @@ -77,6 +92,23 @@ export function translateFilterToGraphql<S extends Schema.Schema.AnyNoContext>(

if (!Option.isSome(propertyId) || !Option.isSome(propertyType)) continue;

if (propertyType.value === 'relation') {
const relationFilter = fieldFilter as { exists?: boolean };

if (relationFilter.exists === true) {
graphqlFilter.push(buildRelationExistsFilter(propertyId.value));
continue;
}

if (relationFilter.exists === false) {
const existsFilter = buildRelationExistsFilter(propertyId.value);
graphqlFilter.push({
not: existsFilter,
});
continue;
}
}

if (
propertyType.value === 'string' &&
(fieldFilter.is || fieldFilter.startsWith || fieldFilter.endsWith || fieldFilter.contains)
Expand Down
52 changes: 52 additions & 0 deletions packages/hypergraph/test/utils/translate-filter-to-graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,32 @@ import type * as Schema from 'effect/Schema';
import { describe, expect, it } from 'vitest';
import { translateFilterToGraphql } from '../../src/utils/translate-filter-to-graphql.js';

export const User = Entity.Schema(
{
username: Type.String,
},
{
types: [Id('f6fa5a6a-7dbf-4c31-aba5-7b4cd0a9b2de')],
properties: {
username: Id('f0dfb5c0-3c90-4d30-98a3-6a139c8b5943'),
},
},
);

export const Todo = Entity.Schema(
{
name: Type.String,
completed: Type.Boolean,
priority: Type.Number,
assignees: Type.Relation(User),
},
{
types: [Id('a288444f-06a3-4037-9ace-66fe325864d0')],
properties: {
name: Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
completed: Id('d2d64cd3-a337-4784-9e30-25bea0349471'),
priority: Id('ee920534-42ce-4113-a63b-8f3c889dd772'),
assignees: Id('f399677c-2bf9-40c3-9622-815be7b83344'),
},
},
);
Expand Down Expand Up @@ -147,6 +161,44 @@ describe('translateFilterToGraphql number filters', () => {
});
});

describe('translateFilterToGraphql relation filters', () => {
it('should translate relation `exists` filter correctly', () => {
const filter: TodoFilter = {
// @ts-expect-error - this is a test
assignees: { exists: true },
};

const result = translateFilterToGraphql(filter, Todo);

expect(result).toEqual({
relations: {
some: {
typeId: { is: 'f399677c-2bf9-40c3-9622-815be7b83344' },
},
},
});
});

it('should translate relation `exists: false` filter correctly', () => {
const filter: TodoFilter = {
// @ts-expect-error - this is a test
assignees: { exists: false },
};

const result = translateFilterToGraphql(filter, Todo);

expect(result).toEqual({
not: {
relations: {
some: {
typeId: { is: 'f399677c-2bf9-40c3-9622-815be7b83344' },
},
},
},
});
});
});

describe('translateFilterToGraphql multiple filters', () => {
it('should translate multiple filters correctly', () => {
const filter: TodoFilter = {
Expand Down
Loading