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
28 changes: 21 additions & 7 deletions backend/backend-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
4. [GameEdge](#gameedge)
5. [GameFilter](#gamefilter)
6. [GameSort](#gamesort)
7. [ReleaseStatus](#releasestatus)
8. [PageInfo](#pageinfo)
9. [User](#user)
10. [AuthPayload](#authpayload)
11. [Review](#review)
7. [SortOrder](#sortorder)
8. [ReleaseStatus](#releasestatus)
9. [PageInfo](#pageinfo)
10. [User](#user)
11. [AuthPayload](#authpayload)
12. [Review](#review)

## Autentisering

Expand Down Expand Up @@ -108,6 +109,7 @@ games(
search: String
filter: GameFilter
sort: GameSort! = POPULARITY
order: SortOrder
first: Int
after: String
last: Int
Expand All @@ -122,6 +124,7 @@ games(
| `search` | `String` | — | Søk i tittel og alternative navn. Skiller ikke mellom store og små bokstaver. Maks 128 tegn. |
| `filter` | [`GameFilter`](#gamefilter) | — | Filtrering på sjanger, plattform, utgivelsesår og om spillet er lansert. |
| `sort` | [`GameSort!`](#gamesort) | `POPULARITY` | Sorteringsrekkefølge. |
| `order` | [`SortOrder`](#sortorder) | — | Retning på sorteringen. Uten verdi brukes standardretningen til `sort`. |
| `first` | `Int` | `20` | Antall spill fremover fra `after`. Begrenses til 1-50. |
| `after` | `String` | — | Cursor fra `pageInfo.endCursor`. Henter spillene etter denne. |
| `last` | `Int` | — | Antall spill bakover fra `before`. Begrenses til 1-50. |
Expand All @@ -131,9 +134,9 @@ games(

#### Returnerer

[`GameConnection!`](#gameconnection). `totalCount` er antall treff for søk og filter, uavhengig av cursor. En cursor er bare gyldig for sorteringen den ble laget med.
[`GameConnection!`](#gameconnection). `totalCount` er antall treff for søk og filter, uavhengig av cursor. En cursor er bare gyldig for sorteringen og retningen den ble laget med.

Gir feilen `BAD_USER_INPUT` hvis begge retningene brukes samtidig, hvis cursoren er ugyldig eller tilhører en annen sortering, hvis søket er lengre enn 128 tegn, eller hvis et år i filteret er utenfor 1-9999.
Gir feilen `BAD_USER_INPUT` hvis begge retningene brukes samtidig, hvis cursoren er ugyldig eller tilhører en annen sortering eller retning, hvis søket er lengre enn 128 tegn, eller hvis et år i filteret er utenfor 1-9999.

#### Eksempel: søk

Expand Down Expand Up @@ -966,6 +969,17 @@ Enum for sortering i [`games`](#games).

---

### SortOrder

Enum for `order` i [`games`](#games). Beskrivelsene i [`GameSort`](#gamesort) gjelder standardretningen, som er `DESC` for alle unntatt `NAME`, der den er `ASC`. Den andre verdien gir motsatt rekkefølge.

| Verdi | Beskrivelse |
| ------ | ------------- |
| `ASC` | Stigende. |
| `DESC` | Synkende. |

---

### ReleaseStatus

Enum for `released` i [`GameFilter`](#gamefilter). Sammenlignes med dagens dato ved hver spørring, så et spill går fra kommende til lansert på lanseringsdagen uten ny import.
Expand Down
8 changes: 4 additions & 4 deletions backend/src/cursor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GameSort } from './types.js';
import type { GameSort, SortOrder } from './types.js';
import { badInput } from './validation.js';

type Cursor = { s: GameSort; k: string; id: number };
type Cursor = { s: GameSort; o: SortOrder; k: string; id: number };

const error = () => badInput('Ugyldig cursor');

Expand All @@ -11,7 +11,7 @@ export function encodeCursor(cursor: Cursor) {
}

// konverter base64url til cursor-JSON
export function decodeCursor(str: string, sort: GameSort) {
export function decodeCursor(str: string, sort: GameSort, order: SortOrder) {
let cursor: Cursor;

try {
Expand All @@ -24,7 +24,7 @@ export function decodeCursor(str: string, sort: GameSort) {
throw error();
}

if (cursor.s !== sort) {
if (cursor.s !== sort || cursor.o !== order) {
throw error();
}
if (typeof cursor.k !== 'string') {
Expand Down
10 changes: 7 additions & 3 deletions backend/src/resolvers/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
SQL_GENRES,
SQL_PLATFORMS,
SQL_SUGGESTIONS,
SORTS,
sqlGames,
} from '../sql/games.js';

Expand Down Expand Up @@ -67,13 +68,16 @@ export async function games(_parent: unknown, args: GamesArgs) {
throw badInput('Bruk enten first/after eller last/before');
}
const reverse = hasReverseArgs;
// snur SQL-retningen én gang for motsatt rekkefølge og én gang for å bla bakover
const order = args.order ?? SORTS[args.sort].dir;
const flip = order !== SORTS[args.sort].dir;

const limit = Math.min(Math.max(args.first ?? args.last ?? 20, 1), MAX_PAGE_SIZE);
const raw = args.after ?? args.before;
const cursor = raw ? decodeCursor(raw, args.sort) : null;
const cursor = raw ? decodeCursor(raw, args.sort, order) : null;

const values = filterValues(args);
const res = await pool.query(sqlGames(args.sort, reverse), [
const res = await pool.query(sqlGames(args.sort, reverse !== flip), [
...values,
limit + 1,
cursor?.k ?? null,
Expand All @@ -86,7 +90,7 @@ export async function games(_parent: unknown, args: GamesArgs) {

const edges = rows.map((row) => ({
node: row,
cursor: encodeCursor({ s: args.sort, k: row.sortKey, id: Number(row.id) }),
cursor: encodeCursor({ s: args.sort, o: order, k: row.sortKey, id: Number(row.id) }),
}));

return {
Expand Down
11 changes: 10 additions & 1 deletion backend/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export const typeDefs = `#graphql
NAME
}
"""
Retning på sorteringen. Uten verdi brukes standardretningen til GameSort.
"""
enum SortOrder {
ASC,
DESC
}
"""
Definerer input argumentene som er gyldige mot serveren.
Brukes for å filtrere visningen av spill.
Expand Down Expand Up @@ -162,7 +170,8 @@ export const typeDefs = `#graphql
games(
search: String,
filter: GameFilter,
sort: GameSort!=POPULARITY
sort: GameSort!=POPULARITY
order: SortOrder,
first: Int,
after: String,
last: Int,
Expand Down
2 changes: 2 additions & 0 deletions backend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type GamesArgs = {
search?: string | null;
filter?: GameFilter | null;
sort: GameSort;
order?: SortOrder | null;
first?: number | null;
after?: string | null;
last?: number | null;
Expand All @@ -26,3 +27,4 @@ export type GameFilter = {
export type ReleaseStatus = 'RELEASED' | 'UPCOMING';

export type GameSort = keyof typeof SORTS;
export type SortOrder = 'ASC' | 'DESC';
31 changes: 18 additions & 13 deletions backend/test/unit/cursor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,42 @@ function expectInvalid(fn: () => unknown) {

describe('encodeCursor / decodeCursor', () => {
it('gir tilbake samme cursor etter koding og dekoding', () => {
const cursor = { s: 'NAME' as const, k: 'Ærlig Talt', id: 42 };
expect(decodeCursor(encodeCursor(cursor), 'NAME')).toEqual(cursor);
const cursor = { s: 'NAME' as const, o: 'ASC' as const, k: 'Ærlig Talt', id: 42 };
expect(decodeCursor(encodeCursor(cursor), 'NAME', 'ASC')).toEqual(cursor);
});

it('lager en URL-sikker streng', () => {
const encoded = encodeCursor({ s: 'NEWEST', k: '2020-01-01??>>', id: 1 });
const encoded = encodeCursor({ s: 'NEWEST', o: 'DESC', k: '2020-01-01??>>', id: 1 });
expect(encoded).toMatch(/^[A-Za-z0-9_-]+$/);
});

it('avviser cursor laget med en annen sortering', () => {
const encoded = encodeCursor({ s: 'NAME', k: 'a', id: 1 });
expectInvalid(() => decodeCursor(encoded, 'POPULARITY'));
const encoded = encodeCursor({ s: 'NAME', o: 'ASC', k: 'a', id: 1 });
expectInvalid(() => decodeCursor(encoded, 'POPULARITY', 'DESC'));
});

it('avviser cursor laget med en annen retning', () => {
const encoded = encodeCursor({ s: 'NAME', o: 'ASC', k: 'a', id: 1 });
expectInvalid(() => decodeCursor(encoded, 'NAME', 'DESC'));
});

it('avviser tekst som ikke er gyldig JSON', () => {
expectInvalid(() => decodeCursor('ikke-en-cursor', 'NAME'));
expectInvalid(() => decodeCursor('', 'NAME'));
expectInvalid(() => decodeCursor('ikke-en-cursor', 'NAME', 'ASC'));
expectInvalid(() => decodeCursor('', 'NAME', 'ASC'));
});

it('avviser JSON som ikke er et objekt', () => {
expectInvalid(() => decodeCursor(b64(123), 'NAME'));
expectInvalid(() => decodeCursor(b64(null), 'NAME'));
expectInvalid(() => decodeCursor(b64('NAME'), 'NAME'));
expectInvalid(() => decodeCursor(b64(123), 'NAME', 'ASC'));
expectInvalid(() => decodeCursor(b64(null), 'NAME', 'ASC'));
expectInvalid(() => decodeCursor(b64('NAME'), 'NAME', 'ASC'));
});

it('avviser cursor der sorteringsnøkkelen ikke er en streng', () => {
expectInvalid(() => decodeCursor(b64({ s: 'POPULARITY', k: 100, id: 1 }), 'POPULARITY'));
expectInvalid(() => decodeCursor(b64({ s: 'POPULARITY', id: 1 }), 'POPULARITY'));
expectInvalid(() => decodeCursor(b64({ s: 'POPULARITY', o: 'DESC', k: 100, id: 1 }), 'POPULARITY', 'DESC'));
expectInvalid(() => decodeCursor(b64({ s: 'POPULARITY', o: 'DESC', id: 1 }), 'POPULARITY', 'DESC'));
});

it.each([0, -1, 1.5, Number.MAX_SAFE_INTEGER + 2, '1', null])('avviser ugyldig id: %s', (id) => {
expectInvalid(() => decodeCursor(b64({ s: 'NAME', k: 'a', id }), 'NAME'));
expectInvalid(() => decodeCursor(b64({ s: 'NAME', o: 'ASC', k: 'a', id }), 'NAME', 'ASC'));
});
});
Loading