|
| 1 | +#  |
| 2 | + |
| 3 | +   |
| 4 | + |
| 5 | +In combination with [Apollo upload client](https://github.com/jaydenseric/apollo-upload-client), enhances [Apollo](http://apollodata.com) for intuitive file uploads via GraphQL mutations or queries. |
| 6 | + |
| 7 | +- Node >= 6.4 supported. |
| 8 | +- [MIT license](https://en.wikipedia.org/wiki/MIT_License). |
| 9 | + |
| 10 | +## Setup |
| 11 | + |
| 12 | +Install with [Yarn](https://yarnpkg.com): |
| 13 | + |
| 14 | +``` |
| 15 | +yarn add apollo-upload-server |
| 16 | +``` |
| 17 | + |
| 18 | +Add the server middleware just before `graphqlExpress`: |
| 19 | + |
| 20 | +```js |
| 21 | +import {apolloUploadExpress} from 'apollo-upload-server' |
| 22 | +``` |
| 23 | + |
| 24 | +```js |
| 25 | +app.use( |
| 26 | + '/graphql', |
| 27 | + bodyParser.json(), |
| 28 | + apolloUploadExpress({ |
| 29 | + // Optional, defaults to OS temp directory |
| 30 | + uploadDir: '/tmp/uploads' |
| 31 | + }), |
| 32 | + graphqlExpress() |
| 33 | +) |
| 34 | +``` |
| 35 | + |
| 36 | +Add this type to your schema: |
| 37 | + |
| 38 | +```graphql |
| 39 | +input File { |
| 40 | + name: String!, |
| 41 | + type: String!, |
| 42 | + size: Int!, |
| 43 | + path: String! |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +Also setup [Apollo upload client](https://github.com/jaydenseric/apollo-upload-client). |
| 48 | + |
| 49 | +## Usage |
| 50 | + |
| 51 | +Once setup, you will be able to use [`File`](https://developer.mozilla.org/en/docs/Web/API/File) objects, [`FileList`](https://developer.mozilla.org/en/docs/Web/API/FileList) objects, or `File` arrays within query or mutation input variables. See the [client usage](https://github.com/jaydenseric/apollo-upload-client#Usage). |
| 52 | + |
| 53 | +The files upload to a temp directory. The file path and metadata will be avalable under the variable name in the resolver in the shape of the input `File` type in the GraphQL schema. |
| 54 | + |
| 55 | +The resolver variable will hold an array if it is populated as a list (`FileList` or `File` array) on the client – even if the list has only 1 file. |
| 56 | + |
| 57 | +### Single file |
| 58 | + |
| 59 | +In types: |
| 60 | + |
| 61 | +```graphql |
| 62 | +type Mutation { |
| 63 | + updateUserAvatar (userId: String!, avatar: File!): User! |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +In resolvers: |
| 68 | + |
| 69 | +```js |
| 70 | +updateUserAvatar (root, {userId, avatar}) { |
| 71 | + // Auth… |
| 72 | + // Update avatar… |
| 73 | + console.log(`New avatar for user ${userId} is ${avatar.size} bytes`) |
| 74 | + // Return fresh user data… |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +See [client usage for this example](https://github.com/jaydenseric/apollo-upload-client#Single-file). |
| 79 | + |
| 80 | +### Multiple files |
| 81 | + |
| 82 | +In types: |
| 83 | + |
| 84 | +```graphql |
| 85 | +type Mutation { |
| 86 | + updateGallery (galleryId: String!, images: [File!]!): Gallery! |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +In resolvers: |
| 91 | + |
| 92 | +```js |
| 93 | +updateGallery (root, {galleryId, images}) { |
| 94 | + // Auth… |
| 95 | + // Update gallery… |
| 96 | + console.log(`New images for gallery ${userId}:`) |
| 97 | + images.forEach((image, index) => console.log(`Image ${index} is ${image.size} bytes`)) |
| 98 | + // Return fresh gallery data… |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +See [client usage for this example](https://github.com/jaydenseric/apollo-upload-client#Multiple-files). |
| 103 | + |
| 104 | +## Caveats |
| 105 | + |
| 106 | +- No max upload file size option yet. |
| 107 | + |
| 108 | +## Inspiration |
| 109 | + |
| 110 | +- [@HriBB](https://github.com/HriBB)’s [apollo-upload-network-interface](https://github.com/HriBB/apollo-upload-network-interface) and [graphql-server-express-upload](https://github.com/HriBB/graphql-server-express-upload) projects. |
| 111 | +- [@danielbuechele](https://github.com/danielbuechele)’s [Medium article](https://medium.com/@danielbuechele/file-uploads-with-graphql-and-apollo-5502bbf3941e). |
| 112 | +- [@jessedvrs](https://github.com/jessedvrs)’s [example code](https://github.com/HriBB/apollo-upload-network-interface/issues/5#issuecomment-280018715). |
0 commit comments