Fix /api/graphql 500: Site settings title separator values are GraphQL enum names

The titleSeparator select stored '|', '–', '-', '·', '/'. Select values become
GraphQL enum names; '|', '–', '·' are invalid and '-' and '/' collide on '_', so
every project's /api/graphql answered 500 since 1.0.1 (admin/REST unaffected).

- Values are now pipe/endash/hyphen/middot/slash (same labels); TITLE_SEPARATORS
  and titleSeparatorChar map them to the character; titles accept old characters
  and custom project values literally
- Field afterRead/beforeChange turn an old stored character into its name
- MongoDB: rewritten at start (migrateTitleSeparator); Postgres: titleSeparatorSql()
  for the generated migration (verified up/down/up against Postgres 16)
- Test builds the full plugin GraphQL schema and fails on invalid or duplicate
  enum names; unit tests for the mapping, hooks, SQL and Mongo rewrite
- CHANGELOG Unreleased (Fixed + upgrade step 5), seo.md, ipal-upgrade, api.md

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-29 12:06:55 +01:00
co-authored by Claude Opus 5.5
parent f57d089f55
commit bb10fb10fd
13 changed files with 385 additions and 9 deletions
+2
View File
@@ -56,5 +56,7 @@ export { buildSitemapCss, DEFAULT_SITEMAP_CREDIT, SITEMAP_STYLE_PARAM } from './
export type { SitemapStyleOptions } from './sitemapStyle.js'
export { SKIP_AUTO_FILL } from './skipAutoFill.js'
export { slugsAcrossLocales } from './slugsAcrossLocales.js'
export { TITLE_SEPARATORS, titleSeparatorChar, titleSeparatorSql } from './titleSeparator.js'
export type { TitleSeparatorName } from './titleSeparator.js'
export type { SeoMeta, SeoOption } from './types.js'
export { validateFaviconField } from './validateFavicon.js'
+2 -1
View File
@@ -4,6 +4,7 @@ import type { TitleOrder } from './composeTitle.js'
import type { VerificationMetadata } from './searchSettings.js'
import { verificationFrom } from './searchSettings.js'
import { titleSeparatorChar } from './titleSeparator.js'
type SettingsShape = {
[key: string]: unknown
@@ -116,6 +117,6 @@ export async function readSiteMetaConfig({
// The panel stores '|'; titles need it padded.
homeSlug,
order: settings.titleOrder ?? undefined,
separator: settings.titleSeparator ? ` ${settings.titleSeparator} ` : undefined,
separator: titleSeparatorChar(settings.titleSeparator) ? ` ${titleSeparatorChar(settings.titleSeparator)} ` : undefined,
}
}
+91
View File
@@ -0,0 +1,91 @@
import type { BasePayload } from 'payload'
/**
* Site Settings → Title separator: stored value → the character in the title.
*
* Stored values are names, not the characters: select values become GraphQL
* enum names, and '|', '–', '·' are not valid ones ('-' and '/' both turn into
* '_'), which made /api/graphql answer 500 for every site up to 1.14.4. Those
* releases stored the character itself; it is still understood everywhere.
*/
export const TITLE_SEPARATORS = {
endash: '–',
hyphen: '-',
middot: '·',
pipe: '|',
slash: '/',
} as const
export type TitleSeparatorName = keyof typeof TITLE_SEPARATORS
const NAME_OF_CHAR: Record<string, TitleSeparatorName> = Object.fromEntries(
Object.entries(TITLE_SEPARATORS).map(([name, char]) => [char, name as TitleSeparatorName]),
)
/**
* Stored value → separator character. Names map to their character, a legacy
* stored character is returned as is, and any other value (a project's own
* option) is used literally. Empty → undefined.
*/
export function titleSeparatorChar(value: unknown): string | undefined {
if (typeof value !== 'string' || !value.trim()) {return undefined}
return (TITLE_SEPARATORS as Record<string, string>)[value] ?? value
}
/**
* Legacy stored character → its name; everything else unchanged. The field's
* afterRead/beforeChange hooks use it, so documents saved before the rename open
* and save without "invalid selection" before anyone touches them.
*/
export function titleSeparatorName(value: unknown): unknown {
return typeof value === 'string' && NAME_OF_CHAR[value] ? NAME_OF_CHAR[value] : value
}
type MongoGlobals = {
collection?: {
updateOne: (filter: Record<string, unknown>, update: Record<string, unknown>) => Promise<{ modifiedCount?: number }>
}
}
/**
* MongoDB: rewrites a legacy stored character to its name, once per start (a
* no-op once done). SQL adapters change the column in a migration — see
* TITLE_SEPARATOR_SQL and docs/seo.md.
*/
export async function migrateTitleSeparator(payload: BasePayload, settingsSlug = 'site-settings'): Promise<void> {
if (payload.db.name !== 'mongoose') {return}
const globals = (payload.db as unknown as { globals?: MongoGlobals }).globals
if (!globals?.collection) {return}
try {
for (const [char, name] of Object.entries(NAME_OF_CHAR)) {
const result = await globals.collection.updateOne({ globalType: settingsSlug, titleSeparator: char }, { $set: { titleSeparator: name } })
if (result.modifiedCount) {
payload.logger.info(`[ipal] Site Settings: title separator '${char}' stored as '${name}' (GraphQL-safe value).`)
}
}
} catch (error) {
payload.logger.error({ err: error, msg: '[ipal] Site Settings: could not migrate the title separator' })
}
}
/**
* SQL that rewrites legacy separator characters to names (`direction: 'up'`) or
* back (`'down'`), for a Postgres or SQLite migration. Postgres: the generated
* migration turns the column into text, recreates the enum and casts back —
* insert this right after `SET DATA TYPE text;`, or the cast fails on '|':
*
* ALTER TABLE "site_settings" ALTER COLUMN "title_separator" SET DATA TYPE text;
* ${sql.raw(titleSeparatorSql())}
*
* Table/column default to Payload's names for the plugin's Site Settings.
*/
export function titleSeparatorSql({
column = 'title_separator',
direction = 'up',
table = 'site_settings',
}: { column?: string; direction?: 'down' | 'up'; table?: string } = {}): string {
const pairs = Object.entries(NAME_OF_CHAR).map(([char, name]) => (direction === 'up' ? [char, name] : [name, char]))
const cases = pairs.map(([from, to]) => `WHEN '${from}' THEN '${to}'`).join(' ')
const from = pairs.map(([f]) => `'${f}'`).join(', ')
return `UPDATE "${table}" SET "${column}" = CASE "${column}" ${cases} ELSE "${column}" END WHERE "${column}" IN (${from});`
}