Graphcool / graphcool-framework

Apache License 2.0
1.77k stars 131 forks source link

error: unable to resolve jtn to webtask token #524

Closed tsdexter closed 6 years ago

tsdexter commented 6 years ago

Current behavior Suddenly receiving the following error when using a custom resolver function:

{ "error": "Function returned invalid status code: 404. Raw body: {\"code\":404,\"message\":\"unable to resolve jtn to webtask token\",\"req_id\":\"1526472484268.710982\"}" }

GraphQL result:

type(pin): "APOLLO_MUTATION_ERROR"
line(pin): 2
column(pin): 3
0(pin): "authenticateEmailUser"
code(pin): 5000
message(pin): "A function returned an unhandled error. Please check the logs for executionId 'eu-west-1:simple:cjh92aytnyzgt0120rog72n4q'"
requestId(pin): "eu-west-1:simple:cjh92aytnyzgt0120rog72n4q"
networkError(pin): null
message(pin): "GraphQL error: A function returned an unhandled error. Please check the logs for executionId 'eu-west-1:simple:cjh92aytnyzgt0120rog72n4q'"
mutationId(pin): "4"

Reproduction

AuthenticateEmailUser.grapql:

type AuthenticateEmailUserPayload {
  token: String!
  id: ID!,
  email: String!,
  firstname: String!,
  lastname: String!,
  confirmed: Boolean!,
  createdAt: DateTime!,
  updatedAt: DateTime!,
}

extend type Mutation {
  authenticateEmailUser(email: String!, password: String!): AuthenticateEmailUserPayload
}

AuthenticateEmailUser.js:

// eslint-disable-next-line strict, lines-around-directive
'use strict';

const { fromEvent } = require('graphcool-lib');
const bcrypt = require('bcryptjs');

module.exports = event => {
  const {
    email,
    password,
  } = event.data;
  const graphcool = fromEvent(event);
  const api = graphcool.api('simple/v1');
  let gcUser = {};

  function getGraphcoolUser(getUserEmail) {
    return api.request(`
    query {
      User(email: "${getUserEmail}"){
        id
        password
        email
        firstname
        lastname
        confirmed
        createdAt
        updatedAt
        __typename
      }
    }`)
      .then(userQueryResult => {
        if (userQueryResult.error) {
          // eslint-disable-next-line compat/compat
          return Promise.reject(userQueryResult.error);
        }
        gcUser = userQueryResult.User;
        return userQueryResult.User;
      });
  }

  function generateGraphcoolToken(graphcoolUser) {
    const token = graphcool.generateAuthToken(graphcoolUser.id, 'User');
    return token;
  }

  return getGraphcoolUser(email)
    .then(graphcoolUser => {
      if (graphcoolUser === null) {
        // eslint-disable-next-line compat/compat, prefer-promise-reject-errors
        return Promise.reject('Invalid Credentials'); 
      }
      return bcrypt.compare(password, graphcoolUser.password)
        .then(res => {
          if (res === true) {
            return graphcoolUser;
          }
          // eslint-disable-next-line compat/compat, prefer-promise-reject-errors
          return Promise.reject('Invalid Credentials');
        });
    })
    .then(generateGraphcoolToken)
    .then(token => ({
      data: {
        token,
        id: gcUser.id,
        email: gcUser.email,
        firstname: gcUser.firstname,
        lastname: gcUser.lastname,
        confirmed: gcUser.confirmed,
        createdAt: gcUser.createdAt,
        updatedAt: gcUser.updatedAt,
      },
    }))
    .catch(error => {
      // eslint-disable-next-line no-console
      console.log(error);

      // don't expose error message to client!
      return { error: 'An unexpected error occured.' };
    });
};

Expected behavior? It should return the data like it always has until this morning

marktani commented 6 years ago

Please redeploy your function to resolve this problem 🙂 Also see https://github.com/prismagraphql/graphcool-framework/issues/523.

tsdexter commented 6 years ago

thanks for the prompt reply @marktani - I couldn't just redeploy like the other issue, I had to comment it out of graphcool.yml, deploy, add back, deploy.