I am creating my Express-GraphQL api where I can store everything in MongoDB. I am at the point where I have something like this:
Project Mongo model:
const { Schema, model } = require("mongoose");
const projectSchema = new Schema({
name: {
type: String,
require: true,
},
offer: [{ type: String }],
});
module.exports = model("Project", projectSchema);
I deleted all fields except these 2. The field offer must be an array of files - for example you can have there 2-3 pdfs.
In my types I created Project type:
const Project = new GraphQLObjectType({
name: "Project",
fields: () => ({
_id: { type: GraphQLNonNull(GraphQLString) },
name: { type: GraphQLNonNull(GraphQLString) },
offer: { type: GraphQLList(GraphQLString) },
}),
});
I am wondering is there a way of creating a mutation that you can add/upload files in this offer field?
editProject: {
type: Project,
args: {
projectId: { type: GraphQLNonNull(GraphQLString) },
offer: { type: GraphQLList(GraphQLString) },
},
async resolve(parentValue, args) {
//some code here
},
},
If I have some mutation like that and how should I pass the files? If anybody have been doing something like this before I would be very thankful to share experience as I am:
Thanks a lot!
I have been working with graphql for almost 2 years now. And as you are facing this problem we also scratched our head about this quite a lot until we figured out that you can't really pass files in a mutation query but what you can do instead is upload your files from your frontend directly to a cloud service like AWS S3 and pass the object key in your mutation query to save it in your db. And to fetch it again from S3 you can use the same key. This way you are not handling your files in your backend and still keeping them safe with an efficient way to render or download at your frontend.