Skip to content

Commit e1860f7

Browse files
committed
fix image field type sub-field ordering
1 parent 9cfe6fd commit e1860f7

File tree

6 files changed

+48
-30
lines changed

6 files changed

+48
-30
lines changed

.changeset/same-field-order.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
----
2+
'@keystone-6/core': patch
3+
----
4+
5+
Fix `image` field type to use consistent sub-field ordering

examples/assets-local/schema.prisma

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ model Post {
1616
id String @id @default(cuid())
1717
title String @default("")
1818
content String @default("")
19+
banner_id String?
1920
banner_filesize Int?
20-
banner_extension String?
2121
banner_width Int?
2222
banner_height Int?
23-
banner_id String?
23+
banner_extension String?
2424
attachment_filesize Int?
2525
attachment_filename String?
2626
}

examples/assets-s3/schema.prisma

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ model Post {
1616
id String @id @default(cuid())
1717
title String @default("")
1818
content String @default("")
19+
banner_id String?
1920
banner_filesize Int?
20-
banner_extension String?
2121
banner_width Int?
2222
banner_height Int?
23-
banner_id String?
23+
banner_extension String?
2424
attachment_filesize Int?
2525
attachment_filename String?
2626
}

packages/core/src/fields/types/file/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
2-
fieldType,
32
type FieldTypeFunc,
43
type CommonFieldConfig,
54
type BaseListTypeInfo,
65
type KeystoneContext,
76
type FileMetadata,
7+
fieldType,
88
} from '../../../types'
99
import { graphql } from '../../..'
1010

@@ -44,9 +44,7 @@ async function inputResolver (
4444
data: graphql.InferValueFromArg<typeof inputArg>,
4545
context: KeystoneContext
4646
) {
47-
if (data === null || data === undefined) {
48-
return { filename: data, filesize: data }
49-
}
47+
if (data === null || data === undefined) return { filename: data, filesize: data }
5048
const upload = await data.upload
5149
return context.files(storage).getDataFromStream(upload.createReadStream(), upload.filename)
5250
}
@@ -111,10 +109,13 @@ export function file <ListTypeInfo extends BaseListTypeInfo>(config: FileFieldCo
111109
output: graphql.field({
112110
type: FileFieldOutput,
113111
resolve ({ value: { filesize, filename } }) {
114-
if (filesize === null || filename === null) {
115-
return null
112+
if (filename === null) return null
113+
if (filesize === null) return null
114+
return {
115+
filename,
116+
filesize,
117+
storage: config.storage
116118
}
117-
return { filename, filesize, storage: config.storage }
118119
},
119120
}),
120121
__ksTelemetryFieldTypeName: '@keystone-6/file',

packages/core/src/fields/types/image/index.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
22
type BaseListTypeInfo,
3-
fieldType,
43
type FieldTypeFunc,
54
type CommonFieldConfig,
65
type ImageData,
76
type ImageExtension,
87
type KeystoneContext,
8+
fieldType,
99
} from '../../../types'
1010
import { graphql } from '../../..'
1111
import { SUPPORTED_IMAGE_EXTENSIONS } from './utils'
@@ -37,9 +37,9 @@ const ImageFieldOutput = graphql.object<ImageData & { storage: string }>()({
3737
fields: {
3838
id: graphql.field({ type: graphql.nonNull(graphql.ID) }),
3939
filesize: graphql.field({ type: graphql.nonNull(graphql.Int) }),
40-
extension: graphql.field({ type: graphql.nonNull(ImageExtensionEnum) }),
4140
width: graphql.field({ type: graphql.nonNull(graphql.Int) }),
4241
height: graphql.field({ type: graphql.nonNull(graphql.Int) }),
42+
extension: graphql.field({ type: graphql.nonNull(ImageExtensionEnum) }),
4343
url: graphql.field({
4444
type: graphql.nonNull(graphql.String),
4545
resolve (data, args, context) {
@@ -55,8 +55,15 @@ async function inputResolver (
5555
context: KeystoneContext
5656
) {
5757
if (data === null || data === undefined) {
58-
return { extension: data, filesize: data, height: data, id: data, width: data }
58+
return {
59+
id: data,
60+
filesize: data,
61+
width: data,
62+
height: data,
63+
extension: data,
64+
}
5965
}
66+
6067
const upload = await data.upload
6168
return context.images(storage).getDataFromStream(upload.createReadStream(), upload.filename)
6269
}
@@ -88,9 +95,9 @@ export function image <ListTypeInfo extends BaseListTypeInfo>(config: ImageField
8895
fields: {
8996
id: { kind: 'scalar', scalar: 'String', mode: 'optional' },
9097
filesize: { kind: 'scalar', scalar: 'Int', mode: 'optional' },
91-
extension: { kind: 'scalar', scalar: 'String', mode: 'optional' },
9298
width: { kind: 'scalar', scalar: 'Int', mode: 'optional' },
9399
height: { kind: 'scalar', scalar: 'Int', mode: 'optional' },
100+
extension: { kind: 'scalar', scalar: 'String', mode: 'optional' },
94101
},
95102
})({
96103
...config,
@@ -133,23 +140,28 @@ export function image <ListTypeInfo extends BaseListTypeInfo>(config: ImageField
133140
},
134141
output: graphql.field({
135142
type: ImageFieldOutput,
136-
resolve ({ value: { extension, filesize, height, id, width } }) {
137-
if (
138-
extension === null ||
139-
!isValidImageExtension(extension) ||
140-
filesize === null ||
141-
height === null ||
142-
width === null ||
143-
id === null
144-
) {
145-
return null
143+
resolve ({
144+
value: {
145+
id,
146+
filesize,
147+
width,
148+
height,
149+
extension,
146150
}
151+
}) {
152+
if (id === null) return null
153+
if (filesize === null) return null
154+
if (width === null) return null
155+
if (height === null) return null
156+
if (extension === null) return null
157+
if (!isValidImageExtension(extension)) return null
158+
147159
return {
148-
extension,
160+
id,
149161
filesize,
150-
height,
151162
width,
152-
id,
163+
height,
164+
extension,
153165
storage: config.storage,
154166
}
155167
},

tests/sandbox/schema.prisma

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ model Thing {
3939
decimal Decimal? @postgresql.Decimal(32, 8)
4040
bigInt BigInt? @unique
4141
float Float?
42+
image_id String?
4243
image_filesize Int?
43-
image_extension String?
4444
image_width Int?
4545
image_height Int?
46-
image_id String?
46+
image_extension String?
4747
file_filesize Int?
4848
file_filename String?
4949
document Json @default("[{\"type\":\"paragraph\",\"children\":[{\"text\":\"\"}]}]")

0 commit comments

Comments
 (0)