graphql-interceptor
v0.2.0
Published
Intercept GraphQL resolver functions for logging or whatever you want
Downloads
36
Readme
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);