graphql-interceptor

0.2.0 • Public • Published

GraphQL Interceptor

Intercepts all upper level resolvers on a graphql-js server. Useful for logging, metrics, modify responses, etc.

Forked from https://github.com/kadirahq/graphql-errors

const express = require('express');
const { GraphQLSchema, GraphQLObjectType, GraphQLString } = require('graphql');
const graphqlHTTP = require('express-graphql');
const { interceptResolvers } = require('graphql-interceptor');
 
const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      test: {
        type: GraphQLString,
        resolve() {
          throw new Error('it failed');
        },
      },
    },
  }),
});
 
function errorHandler(error) {
  console.error(error)
}
 
function successHandler() {
  console.log('OK')
}
 
// wrap upper level resolvers
interceptResolvers(schema, errorHandler, successHandler);
 
const app = express();
app.use('/', graphqlHTTP({ schema }));
app.listen(3000);

Readme

Keywords

Package Sidebar

Install

npm i graphql-interceptor

Weekly Downloads

12

Version

0.2.0

License

MIT

Last publish

Collaborators

  • restorando