8

使用以下软件包/技术组合(以及未列出的相应依赖项)从服务器上的客户端接收文件上传的正确实现是什么:

  • graphql-upload
  • apollo-server-fastify
  • @nestjs/platform-fastify(代码优先方法)

理想情况下,我们正在尝试使用 NestJS GraphQL Code First 技术实现以下(来自apollo 服务器文件上传)(除了多个文件而不是一个文件)

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type File {
    filename: String!
    mimetype: String!
    encoding: String!
  }

  type Query {
    uploads: [File]
  }

  type Mutation {
    singleUpload(file: Upload!): File!
  }
`;

const resolvers = {
  Query: {
    uploads: (parent, args) => {},
  },
  Mutation: {
    singleUpload: (parent, args) => {
      return args.file.then(file => {
        //Contents of Upload scalar: https://github.com/jaydenseric/graphql-upload#class-graphqlupload
        //file.stream is a node stream that contains the contents of the uploaded file
        //node stream api: https://nodejs.org/api/stream.html
        return file;
      });
    },
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
});

server.listen().then(({ url }) => {
  console.log(` Server ready at ${url}`);
});

现在,我的前端使用这个突变正确地将文件发送到我的服务器:

gql`
  mutation uploadObjects($files: [Upload!]!) {
    uploadObjects(files: $files) {
      bucketKey
    }
  }
`;

以下是按预期与文件二进制文件一起发送的请求图像的链接:

4

0 回答 0