【问题标题】:How to seed/upload images in KeystoneJS 6?如何在 KeystoneJS 6 中播种/上传图像?
【发布时间】:2022-06-20 23:56:04
【问题描述】:

使用示例here,如果我随后将图像字段添加到帖子:

// schema.ts
import { list } from '@keystone-6/core';
import { select, relationship, text, timestamp } from '@keystone-6/core/fields';

export const lists = {
  Post: list({
    fields: {
       featureImage: image(),
    }),
    /* ... */
  },
  /* ... */
});

然后如何调整seed/index.ts 文件以从本地驱动器上传图像?

// seed/index.ts
await context.query.Post.createOne({
    data: {
        ...postData,
        featureImage: { /* ??? What goes here ??? */ }
    },
    query: 'id',
});

否则,我如何以编程方式添加图像以便 keystonejs 知道它们?

【问题讨论】:

    标签: javascript graphql keystonejs


    【解决方案1】:

    从这里得到https://github.com/keystonejs/keystone-discussions-archive/discussions/54

    // prepareToUpload.ts
    import mime from 'mime'
    import fs from 'fs'
    import path from 'path'
    import { Upload } from 'graphql-upload'
    
    export const prepareToUpload = (filePath: string) => {
      const filename = path.basename(filePath)
    
      const createReadStream = () => fs.createReadStream(filePath)
      // @ts-ignore
      const mimetype = mime.getType(filePath)
      const encoding = 'utf-8'
    
      const image = {
        createReadStream,
        filename,
        mimetype,
        encoding,
      }
    
      const upload = new Upload()
      // @ts-ignore
      upload.resolve(image)
    
      return upload
    }
    
    
      await context.query.Film.createOne({
          data: {
            name: filmData.name,
            imagePoster: {
              // imagePoster MUST by a object with `upload` field
              upload: prepareToUpload(__dirname + '/folder_to_local_file/here.jpg'),
            },
          },
          query: 'id',
        })
    

    【讨论】:

      猜你喜欢
      • 2018-02-21
      • 2020-06-24
      • 2016-09-20
      • 2015-08-11
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      相关资源
      最近更新 更多