Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
How to return a rejected Promise to Graphql?

I've got a login method that returns a Promise to GraphQL to resolve. If the user is found and authenticated it resolves the user and works as it should. However, if the account doesn't exist or couldn't be resolved, the promise is rejected with a message Invalid credentials. This is fine but it seems I'm getting a GraphQL error when this happens so I think I'm sending GraphQL something its not expecting.

Here's my code: mutations.js:

login: {
  type: UserType,
  args: {
    email: { type: GraphQLString },
    password: { type: GraphQLString }
  },
  resolve(parentValue, { email, password }, req) {
    return AuthService.login({ email, password, req })
  }
}

AuthService.login:

function login({ email, password, req }) {
  return new Promise((resolve, reject) => {
    passport.authenticate('local', (err, user) => {
      if (!user) { reject('Invalid credentials.') }

      req.login(user, () => resolve(user));
    })({ body: { email, password } });
  });
}

The GraphQL error I'm getting back is this:

{
  "errors": [
    {
      "message": "Unexpected error value: \"Invalid credentials.\"",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "login"
      ]
    }
  ],
  "data": {
    "login": null
  }
}

But I think if I was returning a proper GraphQLFieldResolver wouldn't I get a much simpler error result? Like just an array or (in this case an array of one) error messages?

For example, lets say I change the resolver to this:

resolve(parentValue, { email, password }, req) {
  throw "Error!"
  // return AuthService.login({ email, password, req })
}

I'm getting the error message "message": "Unexpected error value: \"Error!\"". Shouldn't I be getting "message": "Error!" instead? Is that a GraphQL standard to return error messages with Unexpected error value: X?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is that you're returning a promise that is rejected with a string, instead of an Error object. Or in your simpler version, you're throwing a string instead of an Error object.

Just don't do that.

I doubt there's something in the GraphQL standard about this (after all, this is the implementation not the API protocol), it's just graphql.js expecting Error objects (so that it can send stacktraces etc in development mode) and complaining about non-Error exceptions with an "Unexpected error value"message.

To fix your code, use

function login({ email, password, req }) {
  return new Promise((resolve, reject) => {
    passport.authenticate('local', (err, user) => {
      if (!user) { reject(new Error('Invalid credentials.')) }
//                        ^^^^^^^^^^                      ^

      req.login(user, () => resolve(user));
    })({ body: { email, password } });
  });
}

or even better just reject(err).

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda