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

@@ -23,6 +23,7 @@ type Query {
type Post {
id: String!
title: String
hyphenated_name: String
relationToSelf: Post
updatedAt: DateTime
createdAt: DateTime
@@ -49,6 +50,7 @@ type Posts {
input Post_where {
title: Post_title_operator
hyphenated_name: Post_hyphenated_name_operator
relationToSelf: Post_relationToSelf_operator
updatedAt: Post_updatedAt_operator
createdAt: Post_createdAt_operator
@@ -68,6 +70,17 @@ input Post_title_operator {
exists: Boolean
}
input Post_hyphenated_name_operator {
equals: String
not_equals: String
like: String
contains: String
in: [String]
not_in: [String]
all: [String]
exists: Boolean
}
input Post_relationToSelf_operator {
equals: JSON
not_equals: JSON
@@ -117,6 +130,7 @@ input Post_id_operator {
input Post_where_and {
title: Post_title_operator
hyphenated_name: Post_hyphenated_name_operator
relationToSelf: Post_relationToSelf_operator
updatedAt: Post_updatedAt_operator
createdAt: Post_createdAt_operator
@@ -127,6 +141,7 @@ input Post_where_and {
input Post_where_or {
title: Post_title_operator
hyphenated_name: Post_hyphenated_name_operator
relationToSelf: Post_relationToSelf_operator
updatedAt: Post_updatedAt_operator
createdAt: Post_createdAt_operator
@@ -149,6 +164,7 @@ type postsDocAccess {
type PostsDocAccessFields {
title: PostsDocAccessFields_title
hyphenated_name: PostsDocAccessFields_hyphenated_name
relationToSelf: PostsDocAccessFields_relationToSelf
updatedAt: PostsDocAccessFields_updatedAt
createdAt: PostsDocAccessFields_createdAt
@@ -177,6 +193,29 @@ type PostsDocAccessFields_title_Delete {
permission: Boolean!
}
type PostsDocAccessFields_hyphenated_name {
create: PostsDocAccessFields_hyphenated_name_Create
read: PostsDocAccessFields_hyphenated_name_Read
update: PostsDocAccessFields_hyphenated_name_Update
delete: PostsDocAccessFields_hyphenated_name_Delete
}
type PostsDocAccessFields_hyphenated_name_Create {
permission: Boolean!
}
type PostsDocAccessFields_hyphenated_name_Read {
permission: Boolean!
}
type PostsDocAccessFields_hyphenated_name_Update {
permission: Boolean!
}
type PostsDocAccessFields_hyphenated_name_Delete {
permission: Boolean!
}
type PostsDocAccessFields_relationToSelf {
create: PostsDocAccessFields_relationToSelf_Create
read: PostsDocAccessFields_relationToSelf_Read
@@ -1094,6 +1133,7 @@ type postsAccess {
type PostsFields {
title: PostsFields_title
hyphenated_name: PostsFields_hyphenated_name
relationToSelf: PostsFields_relationToSelf
updatedAt: PostsFields_updatedAt
createdAt: PostsFields_createdAt
@@ -1122,6 +1162,29 @@ type PostsFields_title_Delete {
permission: Boolean!
}
type PostsFields_hyphenated_name {
create: PostsFields_hyphenated_name_Create
read: PostsFields_hyphenated_name_Read
update: PostsFields_hyphenated_name_Update
delete: PostsFields_hyphenated_name_Delete
}
type PostsFields_hyphenated_name_Create {
permission: Boolean!
}
type PostsFields_hyphenated_name_Read {
permission: Boolean!
}
type PostsFields_hyphenated_name_Update {
permission: Boolean!
}
type PostsFields_hyphenated_name_Delete {
permission: Boolean!
}
type PostsFields_relationToSelf {
create: PostsFields_relationToSelf_Create
read: PostsFields_relationToSelf_Read
@@ -1649,6 +1712,7 @@ type Mutation {
input mutationPostInput {
title: String
hyphenated_name: String
relationToSelf: String
updatedAt: String
createdAt: String
@@ -1656,6 +1720,7 @@ input mutationPostInput {
input mutationPostUpdateInput {
title: String
hyphenated_name: String
relationToSelf: String
updatedAt: String
createdAt: String