fix(graphql): sanitize graphql field names for schema generation (#11556)

### What? Cannot generate GraphQL schema with hyphenated field names
Using field names that do not adhere to the GraphQL `_a-z & A-Z`
standard prevent you from generating a schema, even though it will work
just fine everywhere else.

Example: `my-field-name` will prevent schema generation.

### How? Field name sanitization on generation and querying
This PR adds sanitization to the schema generation that sanitizes field
names.
- It formats field names in a GraphQL safe format for schema generation.
**It does not change your config.**
- It adds resolvers for field names that do not adhere so they can be
mapped from the config name to the GraphQL safe name.

Example:
- `my-field` will turn into `my_field` in the schema generation
- `my_field` will resolve from `my-field` when data comes out

### Other notes
- Moves code from `packages/graphql/src/schema/buildObjectType.ts` to
`packages/graphql/src/schema/fieldToSchemaMap.ts`
- Resolvers are only added when necessary: `if (formatName(field.name)
!== field.name)`.

---------

Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
This commit is contained in:
Jarrod Flesch
2025-03-07 09:43:09 -05:00
committed by GitHub
parent a53876d741
commit 029cac3cd3
8 changed files with 1216 additions and 934 deletions

View File

@@ -64,7 +64,6 @@ export interface Config {
auth: {
users: UserAuthOperations;
};
blocks: {};
collections: {
posts: Post;
users: User;
@@ -119,6 +118,7 @@ export interface UserAuthOperations {
export interface Post {
id: string;
title?: string | null;
'hyphenated-name'?: string | null;
relationToSelf?: (string | null) | Post;
updatedAt: string;
createdAt: string;
@@ -203,6 +203,7 @@ export interface PayloadMigration {
*/
export interface PostsSelect<T extends boolean = true> {
title?: T;
'hyphenated-name'?: T;
relationToSelf?: T;
updatedAt?: T;
createdAt?: T;