缺少graphql上载和apollo服务器的多部分字段“操作”

kpbwa7wx  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(222)

您好,我正在尝试使用graphql上载一个文件,为此,我有以下代码:
index.js

const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const { graphqlUploadExpress } = require('graphql-upload');

async function startApolloServer() {

  const resolvers = {
    Query: {
      hello: () => 'Hello world!',
    },
    Mutation: {
      singleUpload: async (parent, { file }) => {
        const { createReadStream, filename, mimetype, encoding } = await file;
        const stream = createReadStream();
        const out = require('fs').createWriteStream(filename);
        stream.pipe(out);
        return { filename, mimetype, encoding, url: '' }
      }
    }
  };

  const server = new ApolloServer({
    typeDefs:gql`
    type Query {
      hello: String!
    }
    type UploadedFileResponse {
      filename: String!
      mimetype: String!
      encoding: String!
      url: String!
    }
    type Mutation {
      singleUpload(file: Upload!): UploadedFileResponse!
    }`
  , resolvers },{uploads:false});
  await server.start();

  const app = express();
  app.use(graphqlUploadExpress({ maxFileSize: 1000000000, maxFiles: 10 }));

  server.applyMiddleware({ app });

  await new Promise(resolve => app.listen({ port: 4000 }, resolve));
  console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
  return { server, app };
}

startApolloServer()

为了调用端点,我使用以下curl:

curl localhost:4000/graphql \
  -F operations='{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) { filename } }", "variables": { "file": null } }' \
  -F map='{ "0": ["variables.file"] }' \
  -F 0=@/home/user/a.txt

但当我执行此操作时,我得到了以下错误:
缺少多部分字段“操作”
如果我删除 app.use(graphqlUploadExpress({ maxFileSize: 1000000000, maxFiles: 10 })); 我又犯了一个错误
函数已弃用(…args){rangeerror:超出了最大调用堆栈大小
但在这种情况下,我可以用正确的名称编写一个空文件。
你知道我如何完成这个poc以便使用apollo graphql上传文件吗。
package.json

"dependencies": {
    "apollo-server": "^2.25.2",
    "apollo-server-express": "^2.25.2",
    "graphql": "^15.5.1",
    "graphql-upload": "^12.0.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.12"
  },

但我也试过了

"resolutions": {
    "**/**/fs-capacitor": "^6.2.0",
    "**/graphql-upload": "^11.0.0"
  }

节点版本:14.16.1和14.17.3
谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题