Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

272
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda