question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support multipart file uploads through remote schema

See original GitHub issue

Hi, I am having an issue to upload a file via Hasura using the custom resolvers over remote schema as i want to upload a file on the server folder directly and not any third party.

The file upload is working perfectly fine if use the “graphql-yoga” server that is exposed as “remote schema” and even the mutation is added to the Hasura but it give the following error:

{"errors":[{"extensions":{"path":"$","code":"invalid-json"},"message":"invalid json"}]}

Here is how the schema is looking on the Hasura console:

singleUpload(file: Upload!): File!

Here is the code for ‘app.js’ that is entry point of the project:

const server = require("./server");
const path = require("path");
const bodyParser = require("body-parser");

server.start(
  {
    port: process.env.PORT || 4000,
    playground: "/graphql",
    endpoint: "/graphql"
  },
  () => {
    console.log(`The server is running on port: ${server.options.port}`);
  }
);

Here is there server.js file that is referenced in the above app.js:

const GraphQLServer = require("graphql-yoga").GraphQLServer;
const Mutation = require("./src/resolvers/Mutation");

const resolvers = {
  Mutation
};

const server = new GraphQLServer({
  cors: false,
  typeDefs: "./src/schema.graphql",
  resolvers,
  context(request) {
    return {
      request
    };
  }
});
module.exports = server;

Here is the schema that in am using for this:

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

Here is the Mutation.js:

const fs = require("fs");
const createWriteStream = fs.createWriteStream;

const storeUpload = async ({ stream, filename, request }) => {
  const id = request.userId; // This will just give the current user id
  const path = `files/${id}/${filename}`;
  const dirPath = `files/${id}`;

  fs.mkdir(dirPath, { recursive: true }, err => {
    if (err) {
      throw new Error("File Upload Error.");
    }
  });
  return new Promise((resolve, reject) =>
    stream
      .pipe(createWriteStream(path))
      .on("finish", () => resolve({ id, path }))
      .on("error", reject)
  );
};

const processUpload = async (upload, request) => {
  const { stream, filename, mimetype, encoding } = await upload;
  const { id, path } = await storeUpload({ stream, filename, request });
  return { id, filename, mimetype, encoding, path };
};

module.exports = {
  singleUpload: (obj, { file }, { request }) => {
    return processUpload(file, request);
  }
};

Please let me know if anything else is required.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:44
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

26reactions
sbussardcommented, May 20, 2020

Any updates on the ability to upload files through Hasura? Base64 is not an option for one of my use cases

13reactions
maaftcommented, Jul 13, 2022

Maybe graphql doesn’t support binary.

Just support the multipart/form-data spec (like everyone else does) and its a done deal.

@vaishnavigvs @shahidhk can we please have an update on this? It should really not be hard to implement (support above mentioned multipart-spec) and would help a lot of users a ton.

Read more comments on GitHub >

github_iconTop Results From Across the Web

File Uploads with Apollo Server 2.0 - Apollo GraphQL Blog
The default option for enabling file uploads in Apollo Server 2.0 involves creating a schema and using the Upload type like so:
Read more >
20.7 Multipart (file upload) support - Spring
Spring Portlet MVC has built-in multipart support to handle file uploads in portlet applications, just like Web MVC does. The design for the...
Read more >
Spring - How to stream large multipart file uploads to database ...
Spring boot's default MultiPartResolver interface handles the uploading of multipart files by storing them on the local file system. Before the ...
Read more >
Servlet 3 File Upload - @MultipartConfig, Part | DigitalOcean
We need to annotate File Upload handler servlet with MultipartConfig annotation to handle multipart/form-data requests that is used for ...
Read more >
Spring Boot File upload example with Multipart File - BezKoder
– FileUploadExceptionAdvice handles exception when the controller processes file upload. – application.properties contains configuration for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found