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

271
Visualizações
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 à 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