Skip to content

Commit

Permalink
Simplify image resolver code (#5587)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored May 3, 2021
1 parent 49dd468 commit 3e33cd3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-onions-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/fields': patch
---

Simplified image input resolver.
20 changes: 17 additions & 3 deletions packages-next/fields/src/types/image/Implementation.ts
Original file line number Diff line number Diff line change
@@ -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<P extends string> extends Implementation<P> {
get _supportsUnique() {
Expand Down Expand Up @@ -85,8 +85,22 @@ export class ImageImplementation<P extends string> extends Implementation<P> {
if (data === undefined) {
return undefined;
}
const imageData = await handleImageData(data, context);
return imageData;

type ImageInput = {
upload?: Promise<FileUpload> | 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() {
Expand Down
38 changes: 0 additions & 38 deletions packages-next/fields/src/types/image/handle-image-input.ts

This file was deleted.

1 comment on commit 3e33cd3

@vercel
Copy link

@vercel vercel bot commented on 3e33cd3 May 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.