简体   繁体   English

使用 graphql-upload 和 apollo-server 缺少多部分字段“操作”

[英]Missing multipart field ‘operations’ with graphql-upload and apollo-server

Hello I am trying to upload a file using graphql in order to do this I have the following code:您好,我正在尝试使用 graphql 上传文件以执行此操作,我有以下代码:

index.js索引.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()

And in order to call the endpoint I am using the following curl:为了调用端点,我使用了以下 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

But when I execute this I got the following error:但是当我执行此操作时,出现以下错误:

Missing multipart field 'operations'缺少多部分字段“操作”

If I remove app.use(graphqlUploadExpress({ maxFileSize: 1000000000, maxFiles: 10 }));如果我删除app.use(graphqlUploadExpress({ maxFileSize: 1000000000, maxFiles: 10 }));

I got another error我又犯了一个错误

function deprecated(...args) { RangeError: Maximum call stack size exceeded function deprecated(...args) { RangeError: 超出最大调用堆栈大小

But in this case I can write a empty file with the correct name.但在这种情况下,我可以用正确的名称编写一个空文件。

Any idea about how can I finish this poc in order to upload a file using apollo-graphql.关于如何完成这个 poc 以便使用 apollo-graphql 上传文件的任何想法。

Package.json包.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"
  },

But I tried whit this too但我也试过这个

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

Node version: 14.16.1 and 14.17.3节点版本:14.16.1 和 14.17.3

Thanks谢谢

  1. In your typDefs, add: scalar Upload在您的 typDefs 中,添加: scalar Upload
  2. In resolvers, add: Upload: GraphQLUpload在解析器中,添加: Upload: GraphQLUpload

GraphQLUpload is imported from 'graphql-upload': import { GraphQLUpload } from "graphql-upload"; GraphQLUpload 从 'graphql-upload' import { GraphQLUpload } from "graphql-upload";import { GraphQLUpload } from "graphql-upload";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM