Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
node manual graphql.js error handling, masking js errors as a 200 valid response and scaler mounting/usage

Leaving this here for me as well as others as there is little documentation on using graphql.js outside of apollo or express, i use in a lambda MVC so have to work out how to drive Graphql.js manually.

The graphql method from graphql.js has a habit of returning any errors of any type as a valid response when caused at any point from the resolver onwards.

I have tried graphql-errors package and that will not work due to how the resolvers are declared, as we pass these into the graphql() method after we try to bootstrap resolver wrappers in graphql-errors. Chicken and egg!

But there is a work around when running manually, passing in schema, rootValue and more into graphql directly, a got this running by simply wrapping the rosolvers in a try catch before handing them to rootValue, caching the error.

const { graphql, buildSchema } = require('graphql');
const GraphQLScalar = require('graphql-scalars');
const GraphqlAPISchema = require('../Schema/api.graphql');

// bootstrap graphql schema export
const schema = buildSchema(GraphqlAPISchema);
    
// map in any scalar types from schema, for graphql-scalers type (again little doc to explain how to do this
Object.keys(GraphQLScalar.resolvers).filter((k) => schema._typeMap[k]).map((k) =>  Object.assign(schema._typeMap[k], GraphQLScalar.resolvers[k]));
    
// map in query root values to models
let error; // catch any resolver errors and cache them (as graphqljs masks all as valid response! bundling and exposing resolvers errors to users)
const rootValue = {
    Item: async (args) => { try { return await (new (require('../Item.js'))()).graphql(args).then(([row]) => row)} catch (e) { error = e }},
    Items: async (args) => { try { return await (new (require('../Item.js'))()).graphql(args)} catch (e) { error = e }}
};

// if we threw error in resolvers... we need to re-throw it here for MVC to pick it up or it goes back as 200 masked and exposes all errors! GraphQL bundles errors as valid response!
return graphql({source: request.body.query, variableValues: request.body.variables, schema, rootValue}).then((res) => {
    if (!error) return res;
    throw error;
});

This is an example of how i would map in the same model to graphql resolvers once as a single entity and as an array of entities, caching any errors from model (lazy loaded) onwards caching back from graphql method and decided if to throw or not (for MVC to do as it wishes)

the MVC is Cerberus MVC running locally on SAM and pushable to AWS lambda via cloud formation.

The lack of documentation on graphql.js is shocking, using scalers i found nothing explaining how to mount them manually without using express or apolllo etc, so again scaler mounting is including in this example to help someone.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!