Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image field type to use consistent sub-field ordering #9017

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/same-field-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
----
'@keystone-6/core': patch
----

Fix `image` field type to use consistent sub-field ordering
4 changes: 2 additions & 2 deletions examples/assets-local/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ model Post {
id String @id @default(cuid())
title String @default("")
content String @default("")
banner_id String?
banner_filesize Int?
banner_extension String?
banner_width Int?
banner_height Int?
banner_id String?
banner_extension String?
attachment_filesize Int?
attachment_filename String?
}
4 changes: 2 additions & 2 deletions examples/assets-s3/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ model Post {
id String @id @default(cuid())
title String @default("")
content String @default("")
banner_id String?
banner_filesize Int?
banner_extension String?
banner_width Int?
banner_height Int?
banner_id String?
banner_extension String?
attachment_filesize Int?
attachment_filename String?
}
15 changes: 8 additions & 7 deletions packages/core/src/fields/types/file/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
fieldType,
type FieldTypeFunc,
type CommonFieldConfig,
type BaseListTypeInfo,
type KeystoneContext,
type FileMetadata,
fieldType,
} from '../../../types'
import { graphql } from '../../..'

Expand Down Expand Up @@ -44,9 +44,7 @@ async function inputResolver (
data: graphql.InferValueFromArg<typeof inputArg>,
context: KeystoneContext
) {
if (data === null || data === undefined) {
return { filename: data, filesize: data }
}
if (data === null || data === undefined) return { filename: data, filesize: data }
const upload = await data.upload
return context.files(storage).getDataFromStream(upload.createReadStream(), upload.filename)
}
Expand Down Expand Up @@ -111,10 +109,13 @@ export function file <ListTypeInfo extends BaseListTypeInfo>(config: FileFieldCo
output: graphql.field({
type: FileFieldOutput,
resolve ({ value: { filesize, filename } }) {
if (filesize === null || filename === null) {
return null
if (filename === null) return null
if (filesize === null) return null
return {
filename,
filesize,
storage: config.storage
}
return { filename, filesize, storage: config.storage }
},
}),
__ksTelemetryFieldTypeName: '@keystone-6/file',
Expand Down
46 changes: 29 additions & 17 deletions packages/core/src/fields/types/image/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
type BaseListTypeInfo,
fieldType,
type FieldTypeFunc,
type CommonFieldConfig,
type ImageData,
type ImageExtension,
type KeystoneContext,
fieldType,
} from '../../../types'
import { graphql } from '../../..'
import { SUPPORTED_IMAGE_EXTENSIONS } from './utils'
Expand Down Expand Up @@ -55,8 +55,15 @@ async function inputResolver (
context: KeystoneContext
) {
if (data === null || data === undefined) {
return { extension: data, filesize: data, height: data, id: data, width: data }
return {
id: data,
filesize: data,
width: data,
height: data,
extension: data,
}
}

const upload = await data.upload
return context.images(storage).getDataFromStream(upload.createReadStream(), upload.filename)
}
Expand Down Expand Up @@ -86,11 +93,11 @@ export function image <ListTypeInfo extends BaseListTypeInfo>(config: ImageField
kind: 'multi',
extendPrismaSchema: config.db?.extendPrismaSchema,
fields: {
id: { kind: 'scalar', scalar: 'String', mode: 'optional' },
filesize: { kind: 'scalar', scalar: 'Int', mode: 'optional' },
extension: { kind: 'scalar', scalar: 'String', mode: 'optional' },
width: { kind: 'scalar', scalar: 'Int', mode: 'optional' },
height: { kind: 'scalar', scalar: 'Int', mode: 'optional' },
id: { kind: 'scalar', scalar: 'String', mode: 'optional' },
extension: { kind: 'scalar', scalar: 'String', mode: 'optional' },
},
})({
...config,
Expand Down Expand Up @@ -133,23 +140,28 @@ export function image <ListTypeInfo extends BaseListTypeInfo>(config: ImageField
},
output: graphql.field({
type: ImageFieldOutput,
resolve ({ value: { extension, filesize, height, id, width } }) {
if (
extension === null ||
!isValidImageExtension(extension) ||
filesize === null ||
height === null ||
width === null ||
id === null
) {
return null
resolve ({
value: {
id,
filesize,
width,
height,
extension,
}
}) {
if (id === null) return null
if (filesize === null) return null
if (width === null) return null
if (height === null) return null
if (extension === null) return null
if (!isValidImageExtension(extension)) return null

return {
extension,
id,
filesize,
height,
width,
id,
height,
extension,
storage: config.storage,
}
},
Expand Down
4 changes: 2 additions & 2 deletions tests/sandbox/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ model Thing {
decimal Decimal? @postgresql.Decimal(32, 8)
bigInt BigInt? @unique
float Float?
image_id String?
image_filesize Int?
image_extension String?
image_width Int?
image_height Int?
image_id String?
image_extension String?
file_filesize Int?
file_filename String?
document Json @default("[{\"type\":\"paragraph\",\"children\":[{\"text\":\"\"}]}]")
Expand Down