Skip to content

Commit 9f40750

Browse files
committed
refactor(keystone): DOMA-13081 improve ExternalContent field initialization and add graphQLAdminFragment support
1 parent 221bbf3 commit 9f40750

2 files changed

Lines changed: 55 additions & 21 deletions

File tree

packages/keystone/fields/ExternalContent/Implementation.js

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,29 @@ class ExternalContentImplementation extends Implementation {
143143
processors = {},
144144
maxSizeBytes = DEFAULT_MAX_SIZE_BYTES,
145145
batchDelayMs,
146+
graphQLInputType,
147+
graphQLReturnType,
148+
serialize,
149+
deserialize,
150+
mimetype,
151+
fileExt,
152+
schemaDoc,
153+
graphQLAdminFragment = '',
146154
} = {}, meta) {
147-
super(path, { ...arguments[1], maxSizeBytes }, meta)
155+
// Compute processor config
156+
const byFormat = { ...DEFAULT_PROCESSORS, ...processors }
157+
const cfg = byFormat[format]
158+
if (!cfg) {
159+
throw new Error(`ExternalContent: unknown format "${format}" for ${path}`)
160+
}
148161

149-
const {
150-
graphQLInputType,
151-
graphQLReturnType,
152-
serialize,
153-
deserialize,
154-
mimetype,
155-
fileExt,
156-
} = arguments[1]
162+
// Resolve final values using defaults from processor config
163+
const finalGraphQLInputType = graphQLInputType || cfg.graphQLInputType
164+
const finalGraphQLReturnType = graphQLReturnType || cfg.graphQLReturnType
165+
const finalSerialize = serialize || cfg.serialize
166+
const finalDeserialize = deserialize || cfg.deserialize
167+
const finalMimetype = mimetype || cfg.mimetype
168+
const finalFileExt = fileExt || cfg.fileExt
157169

158170
if (!adapter) {
159171
throw new Error(`ExternalContent: "adapter" is required for ${path}`)
@@ -164,29 +176,49 @@ class ExternalContentImplementation extends Implementation {
164176
throw new Error(`ExternalContent: adapter is not properly configured for ${path}. Check FILE_FIELD_ADAPTER and storage configuration.`)
165177
}
166178

167-
const byFormat = { ...DEFAULT_PROCESSORS, ...processors }
168-
const cfg = byFormat[format]
169-
if (!cfg) {
170-
throw new Error(`ExternalContent: unknown format "${format}" for ${path}`)
171-
}
179+
// Pass resolved values to parent class (don't spread arguments[1] to avoid passing undefined graphQLReturnType)
180+
super(path, {
181+
adapter,
182+
format,
183+
processors,
184+
maxSizeBytes,
185+
batchDelayMs,
186+
graphQLInputType: finalGraphQLInputType,
187+
graphQLReturnType: finalGraphQLReturnType,
188+
serialize: finalSerialize,
189+
deserialize: finalDeserialize,
190+
mimetype: finalMimetype,
191+
fileExt: finalFileExt,
192+
schemaDoc,
193+
graphQLAdminFragment,
194+
}, meta)
172195

173196
this.adapter = adapter
174197
this.format = format
175198
this.maxSizeBytes = maxSizeBytes
176199
this.batchDelayMs = batchDelayMs
177-
this.serialize = serialize || cfg.serialize
178-
this.deserialize = deserialize || cfg.deserialize
179-
this.graphQLInputType = graphQLInputType || cfg.graphQLInputType
180-
this.graphQLReturnType = graphQLReturnType || cfg.graphQLReturnType
181-
this.mimetype = mimetype || cfg.mimetype
182-
this.fileExt = fileExt || cfg.fileExt
200+
this.serialize = finalSerialize
201+
this.deserialize = finalDeserialize
202+
this.graphQLInputType = finalGraphQLInputType
203+
this.graphQLReturnType = finalGraphQLReturnType
204+
this.mimetype = finalMimetype
205+
this.fileExt = finalFileExt
206+
this.graphQLAdminFragment = graphQLAdminFragment
183207
}
184208

185209
// GQL Output
186210
gqlOutputFields () {
187211
return [`${this.path}: ${this.graphQLReturnType}`]
188212
}
189213

214+
// Admin
215+
extendAdminMeta (meta) {
216+
return {
217+
graphQLAdminFragment: this.graphQLAdminFragment,
218+
...meta,
219+
}
220+
}
221+
190222
/**
191223
* Resolves the field value for GraphQL output by reading the file content from storage.
192224
*

packages/keystone/utils/externalContentFieldType.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function isFileMeta (value, opts = {}) {
7676
* isRequired: false,
7777
* })
7878
*/
79-
function createExternalDataField ({ adapter, format = 'json', processors = {}, maxSizeBytes, batchDelayMs, ...otherProps }) {
79+
function createExternalDataField ({ adapter, format = 'json', processors = {}, maxSizeBytes, batchDelayMs, schemaDoc, ...otherProps }) {
8080
if (!adapter) {
8181
throw new Error('createExternalDataField: adapter is required')
8282
}
@@ -95,6 +95,8 @@ function createExternalDataField ({ adapter, format = 'json', processors = {}, m
9595
adapter,
9696
format,
9797
processors,
98+
graphQLAdminFragment: '', // Prevent 'undefined' in admin UI queries (JsonController adds this to query)
99+
schemaDoc: schemaDoc || `External content field storing ${format} data in files`,
98100
}
99101

100102
// Only include maxSizeBytes if explicitly provided

0 commit comments

Comments
 (0)