0
votes

Creating my first User Register resolver for the GraphQL API.

I'm having trouble getting the error response from the Cognito API when I hit it from within the resolver.

The error log looks like this and will correspond to the resolver code below.

{ data: { registerUser: null } }
error is here
{ code: 'UsernameExistsException',
  name: 'UsernameExistsException',
  message: 'An account with the given email already exists.' }
error is here
{ code: 'UsernameExistsException',
  name: 'UsernameExistsException',
  statusCode: 400,
  message: '400' }

I've made sure of the following: - The resolver is being hit from the sandbox and client. - I've configured ApolloServer to formatResponses and Errors

CONFIGURATION CODE

const server = new ApolloServer({
typeDefs: [rootSchema, ...schemaTypes],
resolvers: merge({}, user),
async context({ req }) {
  const user = await authenticate(req)
  return { user }
},
formatResponse: response => {
        console.log(response);
        return response;
 },
formatError: error => ({
         message: error.message,
     state: error.originalError && error.originalError.state,
        locations: error.locations,
        path: error.path,
    })

});

const registerUser = (_, args, ctx) => {
var attributeList = [];
console.log('args')
console.log(args)

var params = {
    ClientId: "config.clientId",
    Password: args.user.password,
    Username: args.user.username,
    UserAttributes: attributeList
}
    userPool.signUp(args.user.email, args.user.password, attributeList, null, function(err, result){
        if (err) {
            console.log('error is here')
            console.log(err);
            throw new Error(err.message);
        }

        if(result) {
            console.log('result is here')
            console.log(result)
            cognitoUser = result.user;

        }

    }).catch(function(error) {
        console.log('error is below');
        console.log(error);
        return error;
    })

}

Two things are preventing me from moving forwards. When I leave out the catch block off the userPool promise I simply get no error at all, the error is not being thrown from inside the signUp function.

If I throw any error at all from within my resolver (whether its within the callback or the catch block) I get the following error.

at process._tickCallback (internal/process/next_tick.js:160:7)
(node:28342) UnhandledPromiseRejectionWarning: Unhandled promise 
rejection. This error originated either by throwing inside of an async 
function without a catch block, or by rejecting a promise which was not 
handled with .catch(). (rejection id: 1)
(node:28342) [DEP0018] DeprecationWarning: Unhandled promise rejections 
are deprecated. In the future, promise rejections that are not handled 
will terminate the Node.js process with a non-zero exit code.

I would like to get the following error to be returned to the Sandbox/Requestor

{ data: { registerUser: null } }
error is here
{ code: 'UsernameExistsException',
  name: 'UsernameExistsException',
  message: 'An account with the given email already exists.' }

Thanks for your help.

1
Welcome to Stack Overflow. I don't think you need to throw an error, you should return data with an error codeMikkel
@Mikkel - thank you! when I simply return the err, it doesn't make its way to the response.techromantic

1 Answers

0
votes

Figured it out. AWS APIs are limited to Callback implementation. Wrap your callback as a promise - then chain that promise to throw as needed.