Nestjs Apollo graphql 上传标量 [英] Nestjs Apollo graphql upload scalar

查看:60
本文介绍了Nestjs Apollo graphql 上传标量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 nestjs graphql 框架,我想使用 apollo 标量上传

I'm using nestjs graphql framework and I want to use apollo scalar upload

我已经能够在另一个不包含 nestjs 的项目中使用标量.

I have been able to use the scalar in another project that did not include nestjs.

schema.graphqlApp.module.ts 注册 graphql

schema.graphql App.module.ts register graphql

    GraphQLModule.forRoot({
      typePaths: ['./**/*.graphql'],
      resolvers: { Upload: GraphQLUpload },
      installSubscriptionHandlers: true,
      context: ({ req }) => ({ req }),
      playground: true,
      definitions: {
        path: join(process.cwd(), './src/graphql.classes.ts'),
        outputAs: 'class',
      },
      uploads: {
        maxFileSize: 10000000, // 10 MB
        maxFiles: 5
      }
    }),

pets.resolver.ts 变异 createPet

pets.resolver.ts mutation createPet

@Mutation('uploadFile')
    async uploadFile(@Args('fileUploadInput') fileUploadInput: FileUploadInput) {
        console.log("TCL: PetsResolver -> uploadFile -> file", fileUploadInput);
        return {
            id: '123454',
            path: 'www.wtf.com',
            filename: fileUploadInput.file.filename,
            mimetype: fileUploadInput.file.mimetype
        }
    }

pets.type.graphql

pets.type.graphql

type Mutation {
        uploadFile(fileUploadInput: FileUploadInput!): File!
}
input FileUploadInput{
    file: Upload!
}

type File {
        id: String!
        path: String!
        filename: String!
        mimetype: String!
}

我希望标量适用于 nestjs,但我的实际结果是

I expect that scalar works with nestjs but my actual result is

{"errors":[{"message":"Promise resolver undefined is not a function","locations":[{"line":2,"column":3}],"path":["createPet"],"extensions":{"code":"INTERNAL_SERVER_ERROR","exception":{"stacktrace":["TypeError: Promise resolver undefined is not a function","    at new Promise (<anonymous>)","    at TransformOperationExecutor.transform (E:\\projectos\\Gitlab\\latineo\\latineo-api\\src\\TransformOperationExecutor.ts:119:32)","    at E:\\projectos\\Gitlab\\latineo\\latineo-api\\src\\TransformOperationExecutor.ts:62:40","    at Array.forEach (<anonymous>)","    at TransformOperationExecutor.transform (E:\\projectos\\Gitlab\\latineo\\latineo-api\\src\\TransformOperationExecutor.ts:41:30)","    at _loop_1 (E:\\projectos\\Gitlab\\latineo\\latineo-api\\src\\TransformOperationExecutor.ts:226:43)","    at TransformOperationExecutor.transform (E:\\projectos\\Gitlab\\latineo\\latineo-api\\node_modules\\class-transformer\\TransformOperationExecutor.js:240:17)","    at ClassTransformer.plainToClass (E:\\projectos\\Gitlab\\latineo\\latineo-api\\src\\ClassTransformer.ts:43:25)","    at Object.plainToClass (E:\\projectos\\Gitlab\\latineo\\latineo-api\\src\\index.ts:37:29)","    at ValidationPipe.transform (E:\\projectos\\Gitlab\\latineo\\latineo-api\\node_modules\\@nestjs\\common\\pipes\\validation.pipe.js:50:41)","    at transforms.reduce (E:\\projectos\\Gitlab\\latineo\\latineo-api\\node_modules\\@nestjs\\core\\pipes\\pipes-consumer.js:15:28)","    at process._tickCallback (internal/process/next_tick.js:68:7)"]}}}],"data":null}

推荐答案

我使用 graphql-upload 库解决了这个问题.首先,我使用 graphql-upload

I solved it by using graphql-upload library. First i created a class for my scalar using GraphQLUpload from graphql-upload

import { Scalar } from '@nestjs/graphql';

import { GraphQLUpload } from 'graphql-upload';

@Scalar('Upload')
export class Upload {
  description = 'Upload custom scalar type';

  parseValue(value) {
    return GraphQLUpload.parseValue(value);
  }

  serialize(value: any) {
    return GraphQLUpload.serialize(value);
  }

  parseLiteral(ast) {
    return GraphQLUpload.parseLiteral(ast);
  }
}

我在应用程序模块中添加的

That i added in my Application module

@Module({
  imports: [
  ...
    DateScalar,
    Upload,
    GraphQLModule.forRoot({
      typePaths: ['./**/*.graphql'],
     ...
      uploads: {
        maxFileSize: 10000000, // 10 MB
        maxFiles: 5,
      },
    }),
  ...
  ],
...
})
export class ApplicationModule {}

我还在我的 graphql 中添加了上传标量

i also added Upload scalar in my graphql

scalar Upload
...
type Mutation {
  uploadFile(file: Upload!): String
}

并且在我的解析器中工作,我可以访问上传的文件.

and is worked in my resolver i had acces to the uploaded file.

  @Mutation()
  async uploadFile(@Args('file') file,) {
    console.log('Hello file',file)
    return "Nice !";
  }

(旁注:我使用了 https://github.com/jaydenseric/apollo-upload-client#function-createuploadlink 上传文件,在解析器中它是一个节点流)

(side note : I used https://github.com/jaydenseric/apollo-upload-client#function-createuploadlink to upload the file, in the resolver it is a Node Stream)

这篇关于Nestjs Apollo graphql 上传标量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆