0.2.0 • Published 8 years ago

graphql-interceptor v0.2.0

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

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);