ant*_*ñoz 6 javascript node.js apollo graphql nestjs
我正在使用 nestjs graphql 框架,我想使用 apollo 标量上传
我已经能够在另一个不包含 nestjs 的项目中使用标量。
schema.graphql App.module.ts 注册 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
}
}),
Run Code Online (Sandbox Code Playgroud)
pets.resolver.ts 变异 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
}
}
Run Code Online (Sandbox Code Playgroud)
pets.type.graphql
type Mutation {
uploadFile(fileUploadInput: FileUploadInput!): File!
}
input FileUploadInput{
file: Upload!
}
type File {
id: String!
path: String!
filename: String!
mimetype: String!
}
Run Code Online (Sandbox Code Playgroud)
我希望标量适用于 nestjs,但我的实际结果是
{"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}
Run Code Online (Sandbox Code Playgroud)
使用import {GraphQLUpload} from " apollo-server-express "
Not from 'graphql-upload'
import { Resolver, Mutation, Args } from '@nestjs/graphql';
import { createWriteStream } from 'fs';
import {GraphQLUpload} from "apollo-server-express"
@Resolver('Download')
export class DownloadResolver {
@Mutation(() => Boolean)
async uploadFile(@Args({name: 'file', type: () => GraphQLUpload})
{
createReadStream,
filename
}): Promise<boolean> {
return new Promise(async (resolve, reject) =>
createReadStream()
.pipe(createWriteStream(`./uploads/${filename}`))
.on('finish', () => resolve(true))
.on('error', () => reject(false))
);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 4
我通过使用graphql-upload库解决了它。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);
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序模块中添加了
@Module({
imports: [
...
DateScalar,
Upload,
GraphQLModule.forRoot({
typePaths: ['./**/*.graphql'],
...
uploads: {
maxFileSize: 10000000, // 10 MB
maxFiles: 5,
},
}),
...
],
...
})
export class ApplicationModule {}
Run Code Online (Sandbox Code Playgroud)
我还在我的 graphql 中添加了上传标量
scalar Upload
...
type Mutation {
uploadFile(file: Upload!): String
}
Run Code Online (Sandbox Code Playgroud)
并在我的解析器中工作,我可以访问上传的文件。
@Mutation()
async uploadFile(@Args('file') file,) {
console.log('Hello file',file)
return "Nice !";
}
Run Code Online (Sandbox Code Playgroud)
(旁注:我使用https://github.com/jaydenseric/apollo-upload-client#function-createuploadlink上传文件,在解析器中它是一个节点流)