I did not get any proper example to upload a file via GraphQL, Apollo Server in NodeJS.
I have written a mutation to send the message that is perfectly working. Here is the code sendMessage.js Mutation
const sendMessage = {
type: ChatType,
args: {
input: {
type: new GraphQLNonNull(new GraphQLInputObjectType({
name: 'ChatMessageInput',
fields: {
toUserId:{
name:'To User ID',
type: new GraphQLNonNull(GraphQLID)
},
message:{
name:'Message',
type: new GraphQLNonNull(GraphQLString)
}
}
}))
},
file:{
name: "File",
type: uploadType
}
},
async resolve(_, input, context) {
}
};
module.exports = sendMessage;
Here the code I have written for creating the GraphQl End Point.
app.use('/api', bodyParser.json(), jwtCheck,
apolloUploadExpress({ uploadDir: "./uploads",maxFileSize: 10000000, maxFiles: 10 }),
graphqlExpress(req => ({
schema,
context: {
user: req.user,
header:req.headers
},
formatError: error => ({
status:error.message[0].status,
message: error.message[0].message,
state: error.message
})
})),function (err, req, res, next) {
if (err.name === 'UnauthorizedError') { // Send the error rather than to show it on the console
//console.log(err);
res.status(401).send({errors:[err]});
}
else {
next(err);
}
}
);
Now, I want to add upload file functionality in the same mutation. Please help me how can I do that.
Thanks in advance.
Here is client/server demo: https://github.com/mrdulin/react-apollo-graphql
Here is server demo: https://github.com/mrdulin/apollo-server-express-starter/tree/master/src/fileupload
我相信你会在apollo-universal-starter-kit项目中找到所有需要的部件(客户端+服务器)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.