|
1 | 1 | # graphql-server-express-upload |
2 | | -Graphql Server Express file upload middleware |
| 2 | + |
| 3 | +Graphql Server Express file upload middleware. Used together with [UploadNetworkInterface](https://github.com/HriBB/apollo-upload-network-interface/releases). |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +#### 1. Add `graphqlExpressUpload` middleware to your Express GraphQL endpoint |
| 8 | + |
| 9 | +``` |
| 10 | +import { graphqlExpress, graphiqlExpress } from 'graphql-server-express' |
| 11 | +import graphqlExpressUpload from 'graphql-server-express-upload' |
| 12 | +import multer from 'multer' |
| 13 | +
|
| 14 | +import schema from './schema' |
| 15 | +
|
| 16 | +const upload = multer({ |
| 17 | + dest: config.tmp.path, |
| 18 | +}) |
| 19 | +
|
| 20 | +app.use('/graphql', |
| 21 | + upload.array('files'), |
| 22 | + // after multer and before graphqlExpress |
| 23 | + graphqlExpressUpload({ endpointURL: '/graphql' }), |
| 24 | + graphqlExpress((req) => { |
| 25 | + return { |
| 26 | + schema, |
| 27 | + context: {} |
| 28 | + } |
| 29 | + }) |
| 30 | +) |
| 31 | +
|
| 32 | +app.use('/graphiql', graphiqlExpress({ |
| 33 | + endpointURL: '/graphql', |
| 34 | +})) |
| 35 | +``` |
| 36 | + |
| 37 | +#### 2. Add `UploadedFile` scalar to your schema |
| 38 | + |
| 39 | +``` |
| 40 | +scalar UploadedFile |
| 41 | +``` |
| 42 | + |
| 43 | +#### 3. Add `UploadedFile` resolver |
| 44 | + |
| 45 | +For now we simply use JSON. In the future we should improve this. |
| 46 | + |
| 47 | +``` |
| 48 | +const resolvers = { |
| 49 | + UploadedFile: { |
| 50 | + __parseLiteral: parseJSONLiteral, |
| 51 | + __serialize: value => value, |
| 52 | + __parseValue: value => value, |
| 53 | + } |
| 54 | + ... |
| 55 | +} |
| 56 | +
|
| 57 | +function parseJSONLiteral(ast) { |
| 58 | + switch (ast.kind) { |
| 59 | + case Kind.STRING: |
| 60 | + case Kind.BOOLEAN: |
| 61 | + return ast.value; |
| 62 | + case Kind.INT: |
| 63 | + case Kind.FLOAT: |
| 64 | + return parseFloat(ast.value); |
| 65 | + case Kind.OBJECT: { |
| 66 | + const value = Object.create(null); |
| 67 | + ast.fields.forEach(field => { |
| 68 | + value[field.name.value] = parseJSONLiteral(field.value); |
| 69 | + }); |
| 70 | +
|
| 71 | + return value; |
| 72 | + } |
| 73 | + case Kind.LIST: |
| 74 | + return ast.values.map(parseJSONLiteral); |
| 75 | + default: |
| 76 | + return null; |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +#### 4. Add mutation on the server |
| 82 | + |
| 83 | +Schema definition |
| 84 | + |
| 85 | +``` |
| 86 | +uploadProfilePicture(id: Int!, files: [UploadedFile!]!): ProfilePicture |
| 87 | +``` |
| 88 | + |
| 89 | +And the mutation function |
| 90 | + |
| 91 | +``` |
| 92 | +async uploadProfilePicture(root, { id, files }, context) { |
| 93 | + // you can now access files parameter from variables |
| 94 | + console.log('uploadProfilePicture', { id, files }) |
| 95 | + //... |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +#### 5. Add mutation on the client |
| 100 | + |
| 101 | +Example using `react-apollo`. Don't forget that you need to be using [UploadNetworkInterface](https://github.com/HriBB/apollo-upload-network-interface/releases), because `apollo-client` does not support `multipart/form-data` out of the box. |
| 102 | + |
| 103 | +``` |
| 104 | +import React, { Component, PropTypes } from 'react' |
| 105 | +import { graphql } from 'react-apollo' |
| 106 | +import gql from 'graphql-tag' |
| 107 | +
|
| 108 | +class UploadProfilePicture extends Component { |
| 109 | +
|
| 110 | + onSubmit = (fields) => { |
| 111 | + const { user, uploadProfilePicture } = this.props |
| 112 | + // fields.files is an instance of FileList |
| 113 | + uploadProfilePicture(user.id, fields.files) |
| 114 | + .then(({ data }) => { |
| 115 | + console.log('data', data); |
| 116 | + }) |
| 117 | + .catch(error => { |
| 118 | + console.log('error', error.message); |
| 119 | + }) |
| 120 | + } |
| 121 | +
|
| 122 | + render() { |
| 123 | + return ( |
| 124 | + //... |
| 125 | + ) |
| 126 | + } |
| 127 | +
|
| 128 | +} |
| 129 | +
|
| 130 | +const ADD_SALON_RESOURCE_PICTURE = gql` |
| 131 | + mutation uploadProfilePicture($id: Int!, $files: [UploadedFile!]!) { |
| 132 | + uploadProfilePicture(id: $id, files: $files) { |
| 133 | + id url thumb square small medium large full |
| 134 | + } |
| 135 | + }` |
| 136 | +
|
| 137 | +const withFileUpload = graphql(ADD_SALON_RESOURCE_PICTURE, { |
| 138 | + props: ({ ownProps, mutate }) => ({ |
| 139 | + uploadProfilePicture: (id, files) => mutate({ |
| 140 | + variables: { id, files }, |
| 141 | + }), |
| 142 | + }), |
| 143 | +}) |
| 144 | +
|
| 145 | +export default withFileUpload(UploadProfilePicture) |
| 146 | +``` |
0 commit comments