Giter Site home page Giter Site logo

postgraphile-upload-example's Introduction

postgraphile-upload-example

This app demonstrates how to add file upload support to PostGraphile using the GraphQL Multipart Request Spec.

Server:

Client:

Quick Start

Clone this repo.

In one terminal:

cd server
createdb upload_example
psql -d upload_example -f schema.sql
yarn
npm start

In another terminal:

cd client
yarn
npm start

The app should now be fully functional at localhost:3000. Uploaded files will be stored locally in /server/uploads.

How does it work?

The server code should be relatively straightforward if you're familiar with PostGraphile. The graphql-upload middleware handles the multipart requests using busboy. The postgraphile-plugin-upload-field plugin for PostGraphile is minimally documented, but briefly, match is a function used to specify the file upload metadata columns and resolve is a function that handles the actual file upload stream.

The client is full of React/Apollo boilerplate. The unique parts are:

Preserving metadata

By default, the example app only stores the local file path to Postgres. To preserve additional metadata, change the header_image_file column type to JSONB and replace the resolveUpload function with the following:

async function resolveUpload(upload) {
  const { filename, mimetype, encoding, createReadStream } = upload;
  const stream = createReadStream();
  // Save file to the local filesystem
  const { id, path } = await saveLocal({ stream, filename });
  // Return metadata to save it to Postgres
  return {
    id,
    path,
    filename,
    mimetype,
    encoding
  };
}

After making this change, you'll also need to update the client app to use the path property of the object.

For a more robust solution, consider using something like postgraphile-plugin-derived-field to expose URLs through GraphQL instead of exposing the raw path/metadata.

If you're streaming file uploads to an object storage service such as S3, you can also use the derived field plugin to generate pre-signed URLs for clients.

postgraphile-upload-example's People

Contributors

mattbretl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.