Core: How would you implements "apollo-upload-server" with Adonis?

Created on 27 Mar 2018  路  8Comments  路  Source: adonisjs/core

I was trying to figure out how to implements the "apollo-upload-server" with AdonisJS Framework but without success.

How would you implements it with AdonisJS, @thetutlage ? Have you any example how to integrate? I searched all over the internet for this solution, before open this issue. I only found example how to integrate with other frameworks.

Ref. URL: https://github.com/jaydenseric/apollo-upload-server/blob/master/readme.md

Most helpful comment

Hi guys, after a little research I came up with the following. Please notice some tricky things like request.request and response.response these are the NodeJS raw req/res. Don't forget to set autoProcess to false on bodyParser files.

Middleware

const { processRequest } = require('graphql-upload');

class GraphQlUpload {
  async handle ({ request, response }, next) {
    if (!request.is('multipart/form-data')) return await next();
    const finished = new Promise(resolve => request.request.on('end', resolve));

    try {
      request.body = await processRequest(
        request.request, response.response,
        { maxFileSize: 10000000, maxFiles: 10 }
      );
      await next();
    } finally {
      await finished;
    }
  }
}

config/bodyParser.js

{
  files: {
    autoProcess: false,
  }
}

All 8 comments

Why not use the existing file upload options?

@thetutlage I'm using GraphQL Server in both API and Client side. It is an implementation of file upload for graphql. So, the API responses is only in one route for all requests. Then, after graphql processes the requisition I can use the Drive to upload it to AWS S3, for example.

I guess it needs to be loaded as a custom Middleware somehow, then be added to the GraphQL route.

Here's how the route.jslooks like using GraphQL Server:

const Route = use('Route')
const GraphqlAdonis = use('ApolloServer')
const schema = use('App/data/schema')

Route.route('/', ({ request, auth, response }) => {
    return GraphqlAdonis.graphql({
        schema,
        context: { auth }
    }, request, response)
}, ['GET', 'POST'])

Route.get('/graphiql', ({ request, response }) => {
    return GraphqlAdonis.graphiql(
        { endpointURL: '/' }, 
        request, 
        response
    )
})

It uses the Apollo implementation of GraphQL using the Adonis package.

@rafaelfesi Afraid, I am not much familiar with the package. Also just by the looks, you will have to use their bodyparser to allow file uploads. https://github.com/jaydenseric/apollo-upload-server/blob/master/readme.md

They ship for express and koa out of the box and not for Adonis. So the only option is use the custom middleware option they have in their docs

Closing since no response from the issue reporter and not actionable as well

Hi guys, after a little research I came up with the following. Please notice some tricky things like request.request and response.response these are the NodeJS raw req/res. Don't forget to set autoProcess to false on bodyParser files.

Middleware

const { processRequest } = require('graphql-upload');

class GraphQlUpload {
  async handle ({ request, response }, next) {
    if (!request.is('multipart/form-data')) return await next();
    const finished = new Promise(resolve => request.request.on('end', resolve));

    try {
      request.body = await processRequest(
        request.request, response.response,
        { maxFileSize: 10000000, maxFiles: 10 }
      );
      await next();
    } finally {
      await finished;
    }
  }
}

config/bodyParser.js

{
  files: {
    autoProcess: false,
  }
}

@adrianyg7 This information was very useful. The days was breaking his head with this.

Thanks for the tip.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blendsoft picture blendsoft  路  3Comments

milosdakic picture milosdakic  路  3Comments

dezashibi picture dezashibi  路  4Comments

itsg2jakhmola picture itsg2jakhmola  路  3Comments

codingphasedotcom picture codingphasedotcom  路  3Comments