Как загрузить файл с помощью GraphQL и Apollo Upload Server в NodeJS? - PullRequest
0 голосов
/ 24 августа 2018

Я не получил ни одного правильного примера для загрузки файла через GraphQL, сервер Apollo в NodeJS.

Я написал мутацию, чтобы отправить сообщение, которое работает отлично.Вот код 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;

Вот код, который я написал для создания конечной точки GraphQl.

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);
  }
}
);

Теперь я хочу добавить функцию загрузки файла вта же мутация.Пожалуйста, помогите мне, как я могу это сделать.

Заранее спасибо.

Ответы [ 2 ]

0 голосов
/ 07 сентября 2018
0 голосов
/ 04 сентября 2018

Я уверен, что вы найдете все необходимые детали (клиент + сервер) в проекте apollo-universal-starter-kit .

...