7

how can i handle image uploads in graphql

Through multer using express route to handle upload and query from graphql to view the images and other data

app.use('/graphql', upload);
app.use('/graphql', getData, graphqlHTTP(tokenData => ({
    schema,
    pretty: true,
    tokenData,
    graphiql: true,
})));
terik_poe
  • 523
  • 4
  • 17
  • 3
    Possible duplicate of [How would you do file uploads in a React-Relay app?](http://stackoverflow.com/questions/33060182/how-would-you-do-file-uploads-in-a-react-relay-app) – Chris Jan 20 '17 at 08:54
  • Agree, likely a duplicate. A file is a file, image or otherwise. – Scot Matson Dec 22 '17 at 01:07

1 Answers1

3

This is a duplicate of How would you do file uploads in a React-Relay app?

In short, yes you can do a file upload in graphql with react + relay. You need to write the Relay update store action, for example:

onDrop: function(files) {
  files.forEach((file)=> {
    Relay.Store.commitUpdate(
      new AddImageMutation({
        file,
        images: this.props.User,
      }),
      {onSuccess, onFailure}
    );
  });
},

Then implement a mutation for the Relay store

class AddImageMutation extends Relay.Mutation {
   static fragments = {
     images: () => Relay.QL`
       fragment on User {
         id,
       }`,
     };

   getMutation() {
     return Relay.QL`mutation{ introduceImage }`;
   }

   getFiles() {
     return {
       file: this.props.file,
     };
   }

   getVariables() {
     return {
       imageName: this.props.file.name,
     };
   }

   getFatQuery() {
     return Relay.QL`
       fragment on IntroduceImagePayload {
         User {
           images(first: 30) {
             edges {
               node {
                 id,
               }
             }
           }
         },
         newImageEdge,
       }
     `;
   }

   getConfigs() {
     return [{
       type: 'RANGE_ADD',
       parentName: 'User',
       parentID: this.props.images.id,
       connectionName: 'images',
       edgeName: 'newImageEdge',
       rangeBehaviors: {
         '': 'prepend',
       },
     }];
   }
 }

In you server-side schema, preform update

const imageMutation = Relay.mutationWithClientMutationId({
  name: 'IntroduceImage',
  inputFields: {
    imageName: {
      type: new GraphQL.GraphQLNonNull(GraphQL.GraphQLString),
    },
  },
  outputFields: {
    newImageEdge: {
      type: ImageEdge,
      resolve: (payload, args, options) => {
        const file = options.rootValue.request.file;
        //write the image to you disk
        return uploadFile(file.buffer, filePath, filename)
        .then(() => {
          /* Find the offset for new edge*/
          return Promise.all(
            [(new myImages()).getAll(),
              (new myImages()).getById(payload.insertId)])
          .spread((allImages, newImage) => {
            const newImageStr = JSON.stringify(newImage);
            /* If edge is in list return index */
            const offset = allImages.reduce((pre, ele, idx) => {
              if (JSON.stringify(ele) === newImageStr) {
                return idx;
              }
              return pre;
            }, -1);

            return {
              cursor: offset !== -1 ? Relay.offsetToCursor(offset) : null,
              node: newImage,
            };
          });
        });
      },
    },
    User: {
      type: UserType,
      resolve: () => (new myImages()).getAll(),
    },
  },
  mutateAndGetPayload: (input) => {
    //break the names to array.
    let imageName = input.imageName.substring(0, input.imageName.lastIndexOf('.'));
    const mimeType = input.imageName.substring(input.imageName.lastIndexOf('.'));
    //wirte the image to database
    return (new myImages())
    .add(imageName)
    .then(id => {
    //prepare to wirte disk
      return {
        insertId: id,
        imgNmae: imageName,
      };
    });
  },
});

All the code above you can find them in this repo https://github.com/bfwg/relay-gallery There is also a live demo http://fanjin.io

Fan Jin
  • 2,412
  • 17
  • 25
  • 2
    If this answer is a duplicate, you should vote to mark it as such and point to the existing answer since it's basically the same. – Chris Jan 20 '17 at 08:53