Skip to content

Commit

Permalink
Add first
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Jan 20, 2025
1 parent eb5ce70 commit 0ba5a85
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
6 changes: 3 additions & 3 deletions apps/web/src/page/BlogPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Link} from '@/layout/nav/Link'
import {BlogOverview} from '@/schema/BlogOverview'
import {BlogPost} from '@/schema/BlogPost'
import styler from '@alinea/styler'
import {Query} from 'alinea'
import {Entry} from 'alinea/core/Entry'
import {VStack} from 'alinea/ui'
import {MetadataRoute} from 'next'
Expand All @@ -18,8 +19,7 @@ export default async function BlogPage() {
type: BlogOverview,
select: {
title: BlogOverview.title,
posts: {
children: {},
posts: Query.children({
type: BlogPost,
select: {
...Entry,
Expand All @@ -28,7 +28,7 @@ export default async function BlogPage() {
author: BlogPost.author,
publishDate: BlogPost.publishDate
}
}
})
}
})
return (
Expand Down
1 change: 0 additions & 1 deletion src/backend/Database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ test('remove field contents', async () => {

test('take/skip', async () => {
const example = createExample()
const {Page} = example.schema
const lastTwo = await example.find({
root: example.workspaces.main.pages,
skip: 1,
Expand Down
8 changes: 7 additions & 1 deletion src/backend/resolver/EntryResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,19 @@ export class EntryResolver {
is(ctx.Table.locale, from.locale)
)
.orderBy(asc(ctx.Table.level))
case 'link':
case 'entryMultiple': {
const linkedField = this.field(from, query.field)
return cursor
.innerJoin(
{[internalTarget]: sql`json_each(${linkedField}) as lF`} as any,
eq(ctx.Table.id, sql`lF.value->>'_entry'`)
)
.orderBy(asc(sql`lF.id`))
}
case 'entrySingle': {
const linkedField = this.field(from, query.field)
return cursor.where(eq(ctx.Table.id, sql`${linkedField}->>'_entry'`))
}
default:
return cursor.orderBy(asc(ctx.Table.index))
}
Expand Down Expand Up @@ -567,6 +572,7 @@ export class EntryResolver {
return Boolean(
query.first ||
query.get ||
query.count ||
query.edge === 'parent' ||
query.edge === 'next' ||
query.edge === 'previous'
Expand Down
16 changes: 11 additions & 5 deletions src/core/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ export interface EdgePrevious {
edge: 'previous'
}

export interface EdgeLink {
edge: 'link'
export interface EdgeEntries {
edge: 'entryMultiple'
field: HasField & Expr
}

export interface EdgeEntry {
edge: 'entrySingle'
field: HasField & Expr
}

Expand All @@ -67,7 +72,8 @@ export type Edge =
| EdgeParent
| EdgeNext
| EdgePrevious
| EdgeLink
| EdgeEntries
| EdgeEntry

export type EdgeQuery<
Selection = unknown,
Expand Down Expand Up @@ -138,10 +144,10 @@ export type AnyQueryResult<Query extends GraphQuery> = Query extends CountQuery<
any
>
? number
: Query extends FirstQuery<infer S, infer T, infer I>
? QueryResult<S, T, I> | null
: Query extends GetQuery<infer S, infer T, infer I>
? QueryResult<S, T, I>
: Query extends FirstQuery<infer S, infer T, infer I>
? QueryResult<S, T, I> | null
: Query extends GraphQuery<infer S, infer T, infer I>
? Array<QueryResult<S, T, I>>
: unknown
Expand Down
34 changes: 32 additions & 2 deletions src/field/link/LinkField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ export interface LinkOptions<Value> extends LinkFieldOptions<Value> {
export class LinkField<
StoredValue extends Reference,
QueryValue
> extends UnionField<StoredValue, QueryValue, LinkOptions<StoredValue>> {}
> extends UnionField<StoredValue, QueryValue, LinkOptions<StoredValue>> {
first<
Selection extends SelectionGuard = undefined,
const Type extends TypeGuard = undefined,
Include extends IncludeGuard = undefined
>(
query: GraphQuery<Selection, Type, Include>
): EdgeQuery<Selection, Type, Include> & {first: true} {
return {edge: 'entrySingle', first: true, field: this, ...query}
}
}

export function createLink<StoredValue extends Reference, QueryValue>(
label: string,
Expand Down Expand Up @@ -72,7 +82,27 @@ export class LinksField<
>(
query: GraphQuery<Selection, Type, Include>
): EdgeQuery<Selection, Type, Include> {
return {edge: 'link', field: this, ...query}
return {edge: 'entryMultiple', field: this, ...query}
}

first<
Selection extends SelectionGuard = undefined,
const Type extends TypeGuard = undefined,
Include extends IncludeGuard = undefined
>(
query?: GraphQuery<Selection, Type, Include>
): EdgeQuery<Selection, Type, Include> & {first: true} {
return {edge: 'entryMultiple', first: true, field: this, ...query}
}

count<
Selection extends SelectionGuard = undefined,
const Type extends TypeGuard = undefined,
Include extends IncludeGuard = undefined
>(
query?: GraphQuery<Selection, Type, Include>
): EdgeQuery<Selection, Type, Include> & {count: true} {
return {edge: 'entryMultiple', count: true, field: this, ...query}
}
}

Expand Down

0 comments on commit 0ba5a85

Please sign in to comment.