From 12c03e087fc4f0d1c6860799cce80827edf3951c Mon Sep 17 00:00:00 2001 From: Tim Leslie Date: Mon, 3 May 2021 10:56:16 +1000 Subject: [PATCH] Simplify image resolver code --- .changeset/red-onions-exist.md | 5 +++ .../fields/src/types/image/Implementation.ts | 20 ++++++++-- .../src/types/image/handle-image-input.ts | 38 ------------------- 3 files changed, 22 insertions(+), 41 deletions(-) create mode 100644 .changeset/red-onions-exist.md delete mode 100644 packages-next/fields/src/types/image/handle-image-input.ts diff --git a/.changeset/red-onions-exist.md b/.changeset/red-onions-exist.md new file mode 100644 index 00000000000..9df10926265 --- /dev/null +++ b/.changeset/red-onions-exist.md @@ -0,0 +1,5 @@ +--- +'@keystone-next/fields': patch +--- + +Simplified image input resolver. diff --git a/packages-next/fields/src/types/image/Implementation.ts b/packages-next/fields/src/types/image/Implementation.ts index 2ed7ebd2799..f6e1a5ee04c 100644 --- a/packages-next/fields/src/types/image/Implementation.ts +++ b/packages-next/fields/src/types/image/Implementation.ts @@ -1,8 +1,8 @@ +import { FileUpload } from 'graphql-upload'; import { PrismaFieldAdapter, PrismaListAdapter } from '@keystone-next/adapter-prisma-legacy'; import { getImageRef, SUPPORTED_IMAGE_EXTENSIONS } from '@keystone-next/utils-legacy'; import { ImageData, KeystoneContext, BaseKeystoneList } from '@keystone-next/types'; import { Implementation } from '../../Implementation'; -import { handleImageData } from './handle-image-input'; export class ImageImplementation

extends Implementation

{ get _supportsUnique() { @@ -85,8 +85,22 @@ export class ImageImplementation

extends Implementation

{ if (data === undefined) { return undefined; } - const imageData = await handleImageData(data, context); - return imageData; + + type ImageInput = { + upload?: Promise | null; + ref?: string | null; + }; + const { ref, upload }: ImageInput = data; + if (ref) { + if (upload) { + throw new Error('Only one of ref and upload can be passed to ImageFieldInput'); + } + return context.images!.getDataFromRef(ref); + } + if (!upload) { + throw new Error('Either ref or upload must be passed to ImageFieldInput'); + } + return context.images!.getDataFromStream((await upload).createReadStream()); } gqlUpdateInputFields() { diff --git a/packages-next/fields/src/types/image/handle-image-input.ts b/packages-next/fields/src/types/image/handle-image-input.ts deleted file mode 100644 index 450805355a0..00000000000 --- a/packages-next/fields/src/types/image/handle-image-input.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { KeystoneContext } from '@keystone-next/types'; -import { FileUpload } from 'graphql-upload'; - -type ImageInput = { - upload?: Promise | null; - ref?: string | null; -}; - -type ValidatedImageInput = - | { - kind: 'upload'; - upload: Promise; - } - | { - kind: 'ref'; - ref: string; - }; - -function validateImageInput({ ref, upload }: ImageInput): ValidatedImageInput { - if (ref != null) { - if (upload) { - throw new Error('Only one of ref and upload can be passed to ImageFieldInput'); - } - return { kind: 'ref', ref }; - } - if (!upload) { - throw new Error('Either ref or upload must be passed to ImageFieldInput'); - } - return { kind: 'upload', upload }; -} - -export async function handleImageData(input: ImageInput, context: KeystoneContext) { - const data = validateImageInput(input); - if (data.kind === 'upload') { - return context.images!.getDataFromStream((await data.upload).createReadStream()); - } - return context.images!.getDataFromRef(data.ref); -}