Skip to content

Commit 029cac3

Browse files
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 <[email protected]>
1 parent a53876d commit 029cac3

File tree

8 files changed

+1216
-934
lines changed

8 files changed

+1216
-934
lines changed

Diff for: packages/graphql/src/schema/buildMutationInputType.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,20 @@ export function buildMutationInputType({
107107
type = new GraphQLList(withNullableType({ type, field, forceNullable, parentIsLocalized }))
108108
return {
109109
...inputObjectTypeConfig,
110-
[field.name]: { type },
110+
[formatName(field.name)]: { type },
111111
}
112112
},
113113
blocks: (inputObjectTypeConfig: InputObjectTypeConfig, field: BlocksField) => ({
114114
...inputObjectTypeConfig,
115-
[field.name]: { type: GraphQLJSON },
115+
[formatName(field.name)]: { type: GraphQLJSON },
116116
}),
117117
checkbox: (inputObjectTypeConfig: InputObjectTypeConfig, field: CheckboxField) => ({
118118
...inputObjectTypeConfig,
119-
[field.name]: { type: GraphQLBoolean },
119+
[formatName(field.name)]: { type: GraphQLBoolean },
120120
}),
121121
code: (inputObjectTypeConfig: InputObjectTypeConfig, field: CodeField) => ({
122122
...inputObjectTypeConfig,
123-
[field.name]: {
123+
[formatName(field.name)]: {
124124
type: withNullableType({ type: GraphQLString, field, forceNullable, parentIsLocalized }),
125125
},
126126
}),
@@ -134,13 +134,13 @@ export function buildMutationInputType({
134134
}, inputObjectTypeConfig),
135135
date: (inputObjectTypeConfig: InputObjectTypeConfig, field: DateField) => ({
136136
...inputObjectTypeConfig,
137-
[field.name]: {
137+
[formatName(field.name)]: {
138138
type: withNullableType({ type: GraphQLString, field, forceNullable, parentIsLocalized }),
139139
},
140140
}),
141141
email: (inputObjectTypeConfig: InputObjectTypeConfig, field: EmailField) => ({
142142
...inputObjectTypeConfig,
143-
[field.name]: {
143+
[formatName(field.name)]: {
144144
type: withNullableType({ type: GraphQLString, field, forceNullable, parentIsLocalized }),
145145
},
146146
}),
@@ -165,20 +165,20 @@ export function buildMutationInputType({
165165
}
166166
return {
167167
...inputObjectTypeConfig,
168-
[field.name]: { type },
168+
[formatName(field.name)]: { type },
169169
}
170170
},
171171
json: (inputObjectTypeConfig: InputObjectTypeConfig, field: JSONField) => ({
172172
...inputObjectTypeConfig,
173-
[field.name]: {
173+
[formatName(field.name)]: {
174174
type: withNullableType({ type: GraphQLJSON, field, forceNullable, parentIsLocalized }),
175175
},
176176
}),
177177
number: (inputObjectTypeConfig: InputObjectTypeConfig, field: NumberField) => {
178178
const type = field.name === 'id' ? GraphQLInt : GraphQLFloat
179179
return {
180180
...inputObjectTypeConfig,
181-
[field.name]: {
181+
[formatName(field.name)]: {
182182
type: withNullableType({
183183
type: field.hasMany === true ? new GraphQLList(type) : type,
184184
field,
@@ -190,7 +190,7 @@ export function buildMutationInputType({
190190
},
191191
point: (inputObjectTypeConfig: InputObjectTypeConfig, field: PointField) => ({
192192
...inputObjectTypeConfig,
193-
[field.name]: {
193+
[formatName(field.name)]: {
194194
type: withNullableType({
195195
type: new GraphQLList(GraphQLFloat),
196196
field,
@@ -201,7 +201,7 @@ export function buildMutationInputType({
201201
}),
202202
radio: (inputObjectTypeConfig: InputObjectTypeConfig, field: RadioField) => ({
203203
...inputObjectTypeConfig,
204-
[field.name]: {
204+
[formatName(field.name)]: {
205205
type: withNullableType({ type: GraphQLString, field, forceNullable, parentIsLocalized }),
206206
},
207207
}),
@@ -247,12 +247,12 @@ export function buildMutationInputType({
247247

248248
return {
249249
...inputObjectTypeConfig,
250-
[field.name]: { type: field.hasMany ? new GraphQLList(type) : type },
250+
[formatName(field.name)]: { type: field.hasMany ? new GraphQLList(type) : type },
251251
}
252252
},
253253
richText: (inputObjectTypeConfig: InputObjectTypeConfig, field: RichTextField) => ({
254254
...inputObjectTypeConfig,
255-
[field.name]: {
255+
[formatName(field.name)]: {
256256
type: withNullableType({ type: GraphQLJSON, field, forceNullable, parentIsLocalized }),
257257
},
258258
}),
@@ -292,7 +292,7 @@ export function buildMutationInputType({
292292

293293
return {
294294
...inputObjectTypeConfig,
295-
[field.name]: { type },
295+
[formatName(field.name)]: { type },
296296
}
297297
},
298298
tabs: (inputObjectTypeConfig: InputObjectTypeConfig, field: TabsField) => {
@@ -336,7 +336,7 @@ export function buildMutationInputType({
336336
},
337337
text: (inputObjectTypeConfig: InputObjectTypeConfig, field: TextField) => ({
338338
...inputObjectTypeConfig,
339-
[field.name]: {
339+
[formatName(field.name)]: {
340340
type: withNullableType({
341341
type: field.hasMany === true ? new GraphQLList(GraphQLString) : GraphQLString,
342342
field,
@@ -347,7 +347,7 @@ export function buildMutationInputType({
347347
}),
348348
textarea: (inputObjectTypeConfig: InputObjectTypeConfig, field: TextareaField) => ({
349349
...inputObjectTypeConfig,
350-
[field.name]: {
350+
[formatName(field.name)]: {
351351
type: withNullableType({ type: GraphQLString, field, forceNullable, parentIsLocalized }),
352352
},
353353
}),
@@ -393,7 +393,7 @@ export function buildMutationInputType({
393393

394394
return {
395395
...inputObjectTypeConfig,
396-
[field.name]: { type: field.hasMany ? new GraphQLList(type) : type },
396+
[formatName(field.name)]: { type: field.hasMany ? new GraphQLList(type) : type },
397397
}
398398
},
399399
}

0 commit comments

Comments
 (0)