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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ The search path is measured against synthetic catalogs of 100,000 and 1,000,000
albums in separate performance databases. At one million albums, selective artist
search took 11.7 ms, while a broad search matching every row took 83.7 ms. A full
broad `searchAlbums` request with facets took about 580 ms because counts and
facets cover the full matching set.
facets cover the full matching set. Only the first page pays for them: Load more
leaves counts and facets out, and the server skips those queries (#96).

Issue #74 adds composite cursor indexes for title, release year and rating sorts.
At a 500,000-row depth, a local benchmark measured indexed cursor pages at
Expand Down
57 changes: 55 additions & 2 deletions client/src/api/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import schemaSource from '../../../schema.graphql?raw';
import { buildSchema, validate } from 'graphql';
import { buildSchema, print, validate, type FieldNode } from 'graphql';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { cache } from './client.js';
import { SEARCH_ALBUMS } from './searchAlbums.js';
import { SEARCH_ALBUMS, SEARCH_ALBUMS_PAGE } from './searchAlbums.js';
import { MY_SEARCH_HISTORY } from './searchHistory.js';
import type {
AlbumSearchInput,
Expand Down Expand Up @@ -64,6 +64,30 @@ it('validates the search operation against the repository schema', () => {
const schema = buildSchema(schemaSource);

expect(validate(schema, SEARCH_ALBUMS)).toEqual([]);
expect(validate(schema, SEARCH_ALBUMS_PAGE)).toEqual([]);
});

// The connection fields each search operation selects, keyed by name.
function connectionFields(document: typeof SEARCH_ALBUMS | typeof SEARCH_ALBUMS_PAGE) {
const [operation] = document.definitions;
if (operation?.kind !== 'OperationDefinition') throw new Error('No operation');
const [searchAlbums] = operation.selectionSet.selections as FieldNode[];
return new Map(
(searchAlbums?.selectionSet?.selections as FieldNode[]).map((field) => [
field.name.value,
print(field),
]),
);
}

it('requests Load more pages without the count and facets, but with the same albums', () => {
const first = connectionFields(SEARCH_ALBUMS);
const next = connectionFields(SEARCH_ALBUMS_PAGE);

expect([...next.keys()]).toEqual(['edges', 'pageInfo']);
// Every album field the first page shows must also arrive with later pages.
expect(next.get('edges')).toBe(first.get('edges'));
expect(next.get('pageInfo')).toBe(first.get('pageInfo'));
});

it('replaces complete history snapshots and clears them without affecting album results', () => {
Expand Down Expand Up @@ -112,6 +136,35 @@ describe('searchAlbums cache policy', () => {
expect(read({ first: 20, after: 'cursor-2' })).toEqual(read());
});

it('keeps the first page count and facets when Load more leaves them out', () => {
const firstPage = result(['1', '2'], 'cursor-2');
firstPage.searchAlbums.genreFacets = [{ genre: { id: '7', name: 'Rock' }, count: 3 }];
firstPage.searchAlbums.decadeFacets = [{ decade: 1990, count: 4 }];
firstPage.searchAlbums.countryFacets = [
{ countryCode: 'NO', countryName: 'Norway', count: 2 },
];
firstPage.searchAlbums.reviewedCount = 1;
write(firstPage);
const { edges, pageInfo } = result(['3', '4'], 'cursor-4', false).searchAlbums;
cache.writeQuery({
query: SEARCH_ALBUMS_PAGE,
variables: { ...variables, after: 'cursor-2' },
data: { searchAlbums: { edges, pageInfo } },
});

const connection = read()?.searchAlbums;

expect(connection?.edges.map((edge) => edge.node.id)).toEqual(['1', '2', '3', '4']);
expect(connection?.pageInfo).toEqual({ endCursor: 'cursor-4', hasNextPage: false });
expect(connection).toMatchObject({
totalCount: 4,
genreFacets: [{ genre: { id: '7', name: 'Rock' }, count: 3 }],
decadeFacets: [{ decade: 1990, count: 4 }],
countryFacets: [{ countryCode: 'NO', countryName: 'Norway', count: 2 }],
reviewedCount: 1,
});
});

it.each([
{ ...input, query: 'different' },
{ ...input, genreIds: ['rock'] },
Expand Down
11 changes: 7 additions & 4 deletions client/src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import type { AlbumConnection } from './searchAlbums.js';
import { getUserId } from './userIdentity.js';

// Album nodes are normalized references inside the cache, not query-result objects.
type CachedConnection = Omit<AlbumConnection, 'edges'> & {
edges: { cursor: string; node: Reference | StoreObject }[];
};
// A Load more page (SEARCH_ALBUMS_PAGE) carries only edges and pageInfo.
type CachedConnection = Partial<Omit<AlbumConnection, 'edges' | 'pageInfo'>> &
Pick<AlbumConnection, 'pageInfo'> & {
edges: { cursor: string; node: Reference | StoreObject }[];
};

// The crate is one list per user, so no arguments belong in its cache key; only
// the paging argument decides whether this is another page or a refresh.
Expand Down Expand Up @@ -59,7 +61,8 @@ export const cache = new InMemoryCache({
}
}

return { ...incoming, edges };
// Later pages leave out the count and facets, so the first page's stay.
return { ...existing, ...incoming, edges };
},
},

Expand Down
41 changes: 41 additions & 0 deletions client/src/api/searchAlbums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { graphql } from '../generated/gql.js';
import type {
AlbumSearchInput,
AlbumSort,
SearchAlbumsPageQuery,
SearchAlbumsPageQueryVariables,
SearchAlbumsQuery,
SearchAlbumsQueryVariables,
} from '../generated/graphql.js';
Expand Down Expand Up @@ -32,6 +34,8 @@ export type DecadeFacet = AlbumConnection['decadeFacets'][number];
export type CountryFacet = AlbumConnection['countryFacets'][number];
export type SearchAlbumsData = SearchAlbumsQuery;
export type SearchAlbumsVariables = SearchAlbumsQueryVariables;
export type SearchAlbumsPageData = SearchAlbumsPageQuery;
export type SearchAlbumsPageVariables = SearchAlbumsPageQueryVariables;

// Only the search-page projection is requested; details belong to the album page.
export const SEARCH_ALBUMS = graphql(`
Expand Down Expand Up @@ -82,3 +86,40 @@ export const SEARCH_ALBUMS = graphql(`
}
}
`);

// Load more asks only for the next albums. The count and facets cover the whole
// result set, so they do not change between pages; the cache keeps the first
// page's values (see `searchAlbums` in client.ts) and the server skips their
// queries when they are not selected (#96). The node selection must match
// SEARCH_ALBUMS, or the merged list would be missing fields; client.test.ts
// checks that.
export const SEARCH_ALBUMS_PAGE = graphql(`
query SearchAlbumsPage($input: AlbumSearchInput!, $first: Int!, $after: String) {
searchAlbums(input: $input, first: $first, after: $after) {
edges {
cursor
node {
id
title
releaseYear
musicBrainzReleaseGroupId
artists {
id
name
}
genres {
id
name
}
averageRating
reviewCount
isInCrate
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
`);
6 changes: 6 additions & 0 deletions client/src/generated/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Documents = {
"\n mutation UpsertReview($input: UpsertReviewInput!) {\n upsertReview(input: $input) {\n review {\n id\n rating\n comment\n }\n album {\n id\n }\n }\n }\n": typeof types.UpsertReviewDocument,
"\n mutation DeleteReview($id: ID!) {\n deleteReview(id: $id) {\n deletedReviewId\n album {\n id\n }\n }\n }\n": typeof types.DeleteReviewDocument,
"\n query SearchAlbums($input: AlbumSearchInput!, $first: Int!, $after: String) {\n searchAlbums(input: $input, first: $first, after: $after) {\n edges {\n cursor\n node {\n id\n title\n releaseYear\n musicBrainzReleaseGroupId\n artists {\n id\n name\n }\n genres {\n id\n name\n }\n averageRating\n reviewCount\n isInCrate\n }\n }\n totalCount\n genreFacets {\n genre {\n id\n name\n }\n count\n }\n decadeFacets {\n decade\n count\n }\n countryFacets {\n countryCode\n countryName\n count\n }\n reviewedCount\n pageInfo {\n hasNextPage\n endCursor\n }\n }\n }\n": typeof types.SearchAlbumsDocument,
"\n query SearchAlbumsPage($input: AlbumSearchInput!, $first: Int!, $after: String) {\n searchAlbums(input: $input, first: $first, after: $after) {\n edges {\n cursor\n node {\n id\n title\n releaseYear\n musicBrainzReleaseGroupId\n artists {\n id\n name\n }\n genres {\n id\n name\n }\n averageRating\n reviewCount\n isInCrate\n }\n }\n pageInfo {\n hasNextPage\n endCursor\n }\n }\n }\n": typeof types.SearchAlbumsPageDocument,
"\n query MySearchHistory {\n mySearchHistory {\n query\n lastSearchedAt\n }\n }\n": typeof types.MySearchHistoryDocument,
"\n mutation RecordSearch($query: String!) {\n recordSearch(query: $query) {\n query\n lastSearchedAt\n }\n }\n": typeof types.RecordSearchDocument,
"\n mutation ClearSearchHistory {\n clearSearchHistory\n }\n": typeof types.ClearSearchHistoryDocument,
Expand All @@ -33,6 +34,7 @@ const documents: Documents = {
"\n mutation UpsertReview($input: UpsertReviewInput!) {\n upsertReview(input: $input) {\n review {\n id\n rating\n comment\n }\n album {\n id\n }\n }\n }\n": types.UpsertReviewDocument,
"\n mutation DeleteReview($id: ID!) {\n deleteReview(id: $id) {\n deletedReviewId\n album {\n id\n }\n }\n }\n": types.DeleteReviewDocument,
"\n query SearchAlbums($input: AlbumSearchInput!, $first: Int!, $after: String) {\n searchAlbums(input: $input, first: $first, after: $after) {\n edges {\n cursor\n node {\n id\n title\n releaseYear\n musicBrainzReleaseGroupId\n artists {\n id\n name\n }\n genres {\n id\n name\n }\n averageRating\n reviewCount\n isInCrate\n }\n }\n totalCount\n genreFacets {\n genre {\n id\n name\n }\n count\n }\n decadeFacets {\n decade\n count\n }\n countryFacets {\n countryCode\n countryName\n count\n }\n reviewedCount\n pageInfo {\n hasNextPage\n endCursor\n }\n }\n }\n": types.SearchAlbumsDocument,
"\n query SearchAlbumsPage($input: AlbumSearchInput!, $first: Int!, $after: String) {\n searchAlbums(input: $input, first: $first, after: $after) {\n edges {\n cursor\n node {\n id\n title\n releaseYear\n musicBrainzReleaseGroupId\n artists {\n id\n name\n }\n genres {\n id\n name\n }\n averageRating\n reviewCount\n isInCrate\n }\n }\n pageInfo {\n hasNextPage\n endCursor\n }\n }\n }\n": types.SearchAlbumsPageDocument,
"\n query MySearchHistory {\n mySearchHistory {\n query\n lastSearchedAt\n }\n }\n": types.MySearchHistoryDocument,
"\n mutation RecordSearch($query: String!) {\n recordSearch(query: $query) {\n query\n lastSearchedAt\n }\n }\n": types.RecordSearchDocument,
"\n mutation ClearSearchHistory {\n clearSearchHistory\n }\n": types.ClearSearchHistoryDocument,
Expand Down Expand Up @@ -80,6 +82,10 @@ export function graphql(source: "\n mutation DeleteReview($id: ID!) {\n dele
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function graphql(source: "\n query SearchAlbums($input: AlbumSearchInput!, $first: Int!, $after: String) {\n searchAlbums(input: $input, first: $first, after: $after) {\n edges {\n cursor\n node {\n id\n title\n releaseYear\n musicBrainzReleaseGroupId\n artists {\n id\n name\n }\n genres {\n id\n name\n }\n averageRating\n reviewCount\n isInCrate\n }\n }\n totalCount\n genreFacets {\n genre {\n id\n name\n }\n count\n }\n decadeFacets {\n decade\n count\n }\n countryFacets {\n countryCode\n countryName\n count\n }\n reviewedCount\n pageInfo {\n hasNextPage\n endCursor\n }\n }\n }\n"): (typeof documents)["\n query SearchAlbums($input: AlbumSearchInput!, $first: Int!, $after: String) {\n searchAlbums(input: $input, first: $first, after: $after) {\n edges {\n cursor\n node {\n id\n title\n releaseYear\n musicBrainzReleaseGroupId\n artists {\n id\n name\n }\n genres {\n id\n name\n }\n averageRating\n reviewCount\n isInCrate\n }\n }\n totalCount\n genreFacets {\n genre {\n id\n name\n }\n count\n }\n decadeFacets {\n decade\n count\n }\n countryFacets {\n countryCode\n countryName\n count\n }\n reviewedCount\n pageInfo {\n hasNextPage\n endCursor\n }\n }\n }\n"];
/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function graphql(source: "\n query SearchAlbumsPage($input: AlbumSearchInput!, $first: Int!, $after: String) {\n searchAlbums(input: $input, first: $first, after: $after) {\n edges {\n cursor\n node {\n id\n title\n releaseYear\n musicBrainzReleaseGroupId\n artists {\n id\n name\n }\n genres {\n id\n name\n }\n averageRating\n reviewCount\n isInCrate\n }\n }\n pageInfo {\n hasNextPage\n endCursor\n }\n }\n }\n"): (typeof documents)["\n query SearchAlbumsPage($input: AlbumSearchInput!, $first: Int!, $after: String) {\n searchAlbums(input: $input, first: $first, after: $after) {\n edges {\n cursor\n node {\n id\n title\n releaseYear\n musicBrainzReleaseGroupId\n artists {\n id\n name\n }\n genres {\n id\n name\n }\n averageRating\n reviewCount\n isInCrate\n }\n }\n pageInfo {\n hasNextPage\n endCursor\n }\n }\n }\n"];
/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
Expand Down
Loading