apollo-logger
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -54,5 +54,11 @@ (function (global, factory) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var apollo_link_1 = require("apollo-link"); | ||
var graphql_1 = require("graphql"); | ||
var apollo_link_1 = require("apollo-link"); | ||
function debug() { | ||
var iterall_1 = require("iterall"); | ||
exports.defaultFormatter = function (req) { | ||
return !req.variables || Object.keys(req.variables).length === 0 | ||
? req.operationName | ||
: req.operationName + "(" + JSON.stringify(req.variables) + ")"; | ||
}; | ||
exports.defaultLogger = function () { | ||
var args = []; | ||
@@ -62,20 +68,26 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
} | ||
console.log.apply(null, args); | ||
} | ||
var formatRequest = function (req) { | ||
return (!req.variables || Object.keys(req.variables).length === 0) | ||
? req.operationName | ||
: req.operationName + "(" + JSON.stringify(req.variables) + ")"; | ||
return console.log.apply(null, args); | ||
}; | ||
var addPubSubLogger = function (pubsub) { return ({ | ||
publish: function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
debug('pubsub publish', args); | ||
return pubsub.publish.apply(pubsub, args); | ||
}, | ||
subscribe: function (opName, handler) { | ||
var getDefaultLogOptions = function (options) { | ||
var result = options; | ||
if (!result.formatter) { | ||
result.formatter = exports.defaultFormatter; | ||
} | ||
if (!result.logger) { | ||
result.logger = exports.defaultLogger; | ||
} | ||
return result; | ||
}; | ||
var LoggedPubSub = (function () { | ||
function LoggedPubSub(pubsub, options) { | ||
this.pubsub = pubsub; | ||
this.options = getDefaultLogOptions(options); | ||
} | ||
LoggedPubSub.prototype.publish = function (triggerName, payload) { | ||
this.options.logger('pubsub publish', triggerName, payload); | ||
return this.pubsub.publish(triggerName, payload); | ||
}; | ||
LoggedPubSub.prototype.subscribe = function (triggerName, onMessage, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
var result, logHandler; | ||
@@ -86,7 +98,11 @@ return __generator(this, function (_a) { | ||
_a.trys.push([0, , 2, 3]); | ||
logHandler = function (msg) { | ||
debug('pubsub msg', opName + "(" + JSON.stringify(msg) + ")"); | ||
return handler(msg); | ||
logHandler = function () { | ||
var msgArgs = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
msgArgs[_i] = arguments[_i]; | ||
} | ||
_this.options.logger('pubsub msg', triggerName + "(" + JSON.stringify(msgArgs) + ")"); | ||
onMessage(msgArgs); | ||
}; | ||
return [4, pubsub.subscribe(opName, logHandler)]; | ||
return [4, this.pubsub.subscribe(triggerName, logHandler, options)]; | ||
case 1: | ||
@@ -96,3 +112,3 @@ result = _a.sent(); | ||
case 2: | ||
debug('pubsub subscribe', opName, '=>', result); | ||
this.options.logger('pubsub subscribe', triggerName, '=>', result); | ||
return [7]; | ||
@@ -103,57 +119,84 @@ case 3: return [2, result]; | ||
}); | ||
}, | ||
unsubscribe: function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
debug('pubsub unsubscribe', args); | ||
return pubsub.unsubscribe.apply(pubsub, args); | ||
}, | ||
asyncIterator: function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var asyncIter = pubsub.asyncIterator.apply(pubsub, args); | ||
var trigger = args[0]; | ||
return Object.assign({}, asyncIter, { | ||
next: function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, , 2, 3]); | ||
return [4, asyncIter.next()]; | ||
case 1: | ||
result = _a.sent(); | ||
return [3, 3]; | ||
case 2: | ||
debug(JSON.stringify(result), '<= ' + trigger + '->next'); | ||
return [7]; | ||
case 3: return [2, result]; | ||
} | ||
}); | ||
}); | ||
}, | ||
throw: function (error) { | ||
var result; | ||
try { | ||
result = asyncIter.throw(error); | ||
}; | ||
LoggedPubSub.prototype.unsubscribe = function (subId) { | ||
this.options.logger('pubsub unsubscribe', subId); | ||
this.pubsub.unsubscribe(subId); | ||
}; | ||
LoggedPubSub.prototype.asyncIterator = function (triggers) { | ||
return new PubSubAsyncIterator(this.pubsub, triggers, this.options); | ||
}; | ||
return LoggedPubSub; | ||
}()); | ||
exports.LoggedPubSub = LoggedPubSub; | ||
var PubSubAsyncIterator = (function () { | ||
function PubSubAsyncIterator(pubsub, triggers, options) { | ||
this.asyncIter = pubsub.asyncIterator(triggers); | ||
this.triggers = typeof triggers === 'string' ? [triggers] : triggers; | ||
this.options = getDefaultLogOptions(options); | ||
} | ||
PubSubAsyncIterator.prototype.next = function (value) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, , 2, 3]); | ||
return [4, this.asyncIter.next()]; | ||
case 1: | ||
result = _a.sent(); | ||
return [3, 3]; | ||
case 2: | ||
this.options.logger(JSON.stringify(result), '<= ' + JSON.stringify(this.triggers) + '->next'); | ||
return [7]; | ||
case 3: return [2, result]; | ||
} | ||
finally { | ||
debug(trigger + ("->throw('" + JSON.stringify(error) + "') =>"), JSON.stringify(result)); | ||
}); | ||
}); | ||
}; | ||
PubSubAsyncIterator.prototype.return = function (value) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, , 2, 3]); | ||
return [4, this.asyncIter.next()]; | ||
case 1: | ||
result = _a.sent(); | ||
return [3, 3]; | ||
case 2: | ||
this.options.logger(JSON.stringify(result), '<= ' + JSON.stringify(this.triggers) + '->next'); | ||
return [7]; | ||
case 3: return [2, result]; | ||
} | ||
return result; | ||
}, | ||
}); | ||
}); | ||
}, | ||
}); }; | ||
}; | ||
PubSubAsyncIterator.prototype.throw = function (e) { | ||
var result; | ||
try { | ||
result = this.asyncIter.throw(e); | ||
} | ||
finally { | ||
this.options.logger(JSON.stringify(this.triggers) + ("->throw('" + JSON.stringify(e) + "') =>"), JSON.stringify(result)); | ||
} | ||
return result; | ||
}; | ||
PubSubAsyncIterator.prototype[iterall_1.$$asyncIterator] = function () { | ||
return this; | ||
}; | ||
return PubSubAsyncIterator; | ||
}()); | ||
exports.wrapPubSub = function (pubsub, options) { | ||
return new LoggedPubSub(pubsub, options); | ||
}; | ||
var LoggingLink = (function (_super) { | ||
__extends(LoggingLink, _super); | ||
function LoggingLink() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
function LoggingLink(options) { | ||
var _this = _super.call(this) || this; | ||
_this.options = getDefaultLogOptions(options); | ||
return _this; | ||
} | ||
LoggingLink.prototype.request = function (operation, forward) { | ||
var _this = this; | ||
var operationAST = graphql_1.getOperationAST(operation.query, operation.operationName); | ||
@@ -163,18 +206,18 @@ var isSubscription = !!operationAST && operationAST.operation === 'subscription'; | ||
if (isSubscription) { | ||
debug("subscribe <= " + formatRequest(operation)); | ||
_this.options.logger("subscribe <= " + _this.options.formatter(operation)); | ||
} | ||
var sub = forward(operation).subscribe({ | ||
next: function (result) { | ||
debug(JSON.stringify(result) + " <= " + formatRequest(operation)); | ||
_this.options.logger(JSON.stringify(result) + " <= " + _this.options.formatter(operation)); | ||
observer.next(result); | ||
}, | ||
error: function (error) { | ||
debug(JSON.stringify(error) + " <=e " + formatRequest(operation)); | ||
_this.options.logger(JSON.stringify(error) + " <=e " + _this.options.formatter(operation)); | ||
observer.error(error); | ||
}, | ||
complete: observer.complete.bind(observer), | ||
complete: observer.complete.bind(observer) | ||
}); | ||
return function () { | ||
if (isSubscription) { | ||
debug("unsubscribe <= " + formatRequest(operation)); | ||
_this.options.logger("unsubscribe <= " + _this.options.formatter(operation)); | ||
} | ||
@@ -188,16 +231,14 @@ sub.unsubscribe(); | ||
exports.LoggingLink = LoggingLink; | ||
exports.formatResponse = function (response, options) { | ||
debug(JSON.stringify(response) + " <= " + formatRequest(options)); | ||
exports.formatResponse = function (logOptions, response, options) { | ||
var logOpts = getDefaultLogOptions(logOptions); | ||
logOpts.logger(JSON.stringify(response) + " <= " + logOpts.formatter(options)); | ||
return response; | ||
}; | ||
exports.addApolloLogging = function (obj) { | ||
if (obj.publish) { | ||
return addPubSubLogger(obj); | ||
} | ||
else { | ||
throw new Error('Unknown object passed to Apollo Logger:' + JSON.stringify(obj)); | ||
} | ||
}; | ||
exports.default = function (options) { return ({ | ||
link: new LoggingLink(options), | ||
formatResponse: exports.formatResponse.bind(options), | ||
wrapPubSub: function (pubsub) { return exports.wrapPubSub(pubsub, options); } | ||
}); }; | ||
}))); | ||
//# sourceMappingURL=bundle.umd.js.map |
@@ -1,11 +0,35 @@ | ||
import { ApolloLink, Observable } from 'apollo-link'; | ||
import { ApolloLink, Observable, Operation } from 'apollo-link'; | ||
import { PubSubEngine } from 'graphql-subscriptions'; | ||
export interface LogOptions { | ||
logger?: (...args: any[]) => void; | ||
formatter?: (input: FormatterInput) => string; | ||
} | ||
export declare const defaultFormatter: (req: any) => any; | ||
export declare const defaultLogger: (...args: any[]) => void; | ||
export declare class LoggedPubSub implements PubSubEngine { | ||
private pubsub; | ||
private options; | ||
constructor(pubsub: PubSubEngine, options?: LogOptions); | ||
publish(triggerName: string, payload: any): boolean; | ||
subscribe(triggerName: string, onMessage: (...args: any[]) => void, options: any): Promise<number>; | ||
unsubscribe(subId: number): void; | ||
asyncIterator<T>(triggers: string | string[]): AsyncIterator<T>; | ||
} | ||
export declare const wrapPubSub: (pubsub: PubSubEngine, options: LogOptions) => PubSubEngine; | ||
export declare class LoggingLink extends ApolloLink { | ||
private options; | ||
constructor(options?: LogOptions); | ||
request(operation: any, forward: any): Observable<{}>; | ||
} | ||
export declare const formatResponse: (response: any, options: any) => any; | ||
export declare const addApolloLogging: (obj: any) => { | ||
publish(...args: any[]): any; | ||
subscribe(opName: any, handler: any): Promise<any>; | ||
unsubscribe(...args: any[]): any; | ||
asyncIterator(...args: any[]): any; | ||
export interface FormatterInput { | ||
operation: Operation; | ||
result?: any; | ||
error?: any; | ||
} | ||
export declare const formatResponse: (logOptions: LogOptions, response: any, options: any) => any; | ||
declare var _default: (options?: LogOptions) => { | ||
link: LoggingLink; | ||
formatResponse: any; | ||
wrapPubSub: (pubsub: PubSubEngine) => PubSubEngine; | ||
}; | ||
export default _default; |
213
lib/index.js
@@ -48,5 +48,11 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var apollo_link_1 = require("apollo-link"); | ||
var graphql_1 = require("graphql"); | ||
var apollo_link_1 = require("apollo-link"); | ||
function debug() { | ||
var iterall_1 = require("iterall"); | ||
exports.defaultFormatter = function (req) { | ||
return !req.variables || Object.keys(req.variables).length === 0 | ||
? req.operationName | ||
: req.operationName + "(" + JSON.stringify(req.variables) + ")"; | ||
}; | ||
exports.defaultLogger = function () { | ||
var args = []; | ||
@@ -56,20 +62,26 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
} | ||
console.log.apply(null, args); | ||
} | ||
var formatRequest = function (req) { | ||
return (!req.variables || Object.keys(req.variables).length === 0) | ||
? req.operationName | ||
: req.operationName + "(" + JSON.stringify(req.variables) + ")"; | ||
return console.log.apply(null, args); | ||
}; | ||
var addPubSubLogger = function (pubsub) { return ({ | ||
publish: function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
debug('pubsub publish', args); | ||
return pubsub.publish.apply(pubsub, args); | ||
}, | ||
subscribe: function (opName, handler) { | ||
var getDefaultLogOptions = function (options) { | ||
var result = options; | ||
if (!result.formatter) { | ||
result.formatter = exports.defaultFormatter; | ||
} | ||
if (!result.logger) { | ||
result.logger = exports.defaultLogger; | ||
} | ||
return result; | ||
}; | ||
var LoggedPubSub = (function () { | ||
function LoggedPubSub(pubsub, options) { | ||
this.pubsub = pubsub; | ||
this.options = getDefaultLogOptions(options); | ||
} | ||
LoggedPubSub.prototype.publish = function (triggerName, payload) { | ||
this.options.logger('pubsub publish', triggerName, payload); | ||
return this.pubsub.publish(triggerName, payload); | ||
}; | ||
LoggedPubSub.prototype.subscribe = function (triggerName, onMessage, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
var result, logHandler; | ||
@@ -80,7 +92,11 @@ return __generator(this, function (_a) { | ||
_a.trys.push([0, , 2, 3]); | ||
logHandler = function (msg) { | ||
debug('pubsub msg', opName + "(" + JSON.stringify(msg) + ")"); | ||
return handler(msg); | ||
logHandler = function () { | ||
var msgArgs = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
msgArgs[_i] = arguments[_i]; | ||
} | ||
_this.options.logger('pubsub msg', triggerName + "(" + JSON.stringify(msgArgs) + ")"); | ||
onMessage(msgArgs); | ||
}; | ||
return [4, pubsub.subscribe(opName, logHandler)]; | ||
return [4, this.pubsub.subscribe(triggerName, logHandler, options)]; | ||
case 1: | ||
@@ -90,3 +106,3 @@ result = _a.sent(); | ||
case 2: | ||
debug('pubsub subscribe', opName, '=>', result); | ||
this.options.logger('pubsub subscribe', triggerName, '=>', result); | ||
return [7]; | ||
@@ -97,57 +113,84 @@ case 3: return [2, result]; | ||
}); | ||
}, | ||
unsubscribe: function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
debug('pubsub unsubscribe', args); | ||
return pubsub.unsubscribe.apply(pubsub, args); | ||
}, | ||
asyncIterator: function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var asyncIter = pubsub.asyncIterator.apply(pubsub, args); | ||
var trigger = args[0]; | ||
return Object.assign({}, asyncIter, { | ||
next: function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, , 2, 3]); | ||
return [4, asyncIter.next()]; | ||
case 1: | ||
result = _a.sent(); | ||
return [3, 3]; | ||
case 2: | ||
debug(JSON.stringify(result), '<= ' + trigger + '->next'); | ||
return [7]; | ||
case 3: return [2, result]; | ||
} | ||
}); | ||
}); | ||
}, | ||
throw: function (error) { | ||
var result; | ||
try { | ||
result = asyncIter.throw(error); | ||
}; | ||
LoggedPubSub.prototype.unsubscribe = function (subId) { | ||
this.options.logger('pubsub unsubscribe', subId); | ||
this.pubsub.unsubscribe(subId); | ||
}; | ||
LoggedPubSub.prototype.asyncIterator = function (triggers) { | ||
return new PubSubAsyncIterator(this.pubsub, triggers, this.options); | ||
}; | ||
return LoggedPubSub; | ||
}()); | ||
exports.LoggedPubSub = LoggedPubSub; | ||
var PubSubAsyncIterator = (function () { | ||
function PubSubAsyncIterator(pubsub, triggers, options) { | ||
this.asyncIter = pubsub.asyncIterator(triggers); | ||
this.triggers = typeof triggers === 'string' ? [triggers] : triggers; | ||
this.options = getDefaultLogOptions(options); | ||
} | ||
PubSubAsyncIterator.prototype.next = function (value) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, , 2, 3]); | ||
return [4, this.asyncIter.next()]; | ||
case 1: | ||
result = _a.sent(); | ||
return [3, 3]; | ||
case 2: | ||
this.options.logger(JSON.stringify(result), '<= ' + JSON.stringify(this.triggers) + '->next'); | ||
return [7]; | ||
case 3: return [2, result]; | ||
} | ||
finally { | ||
debug(trigger + ("->throw('" + JSON.stringify(error) + "') =>"), JSON.stringify(result)); | ||
}); | ||
}); | ||
}; | ||
PubSubAsyncIterator.prototype.return = function (value) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, , 2, 3]); | ||
return [4, this.asyncIter.next()]; | ||
case 1: | ||
result = _a.sent(); | ||
return [3, 3]; | ||
case 2: | ||
this.options.logger(JSON.stringify(result), '<= ' + JSON.stringify(this.triggers) + '->next'); | ||
return [7]; | ||
case 3: return [2, result]; | ||
} | ||
return result; | ||
}, | ||
}); | ||
}); | ||
}, | ||
}); }; | ||
}; | ||
PubSubAsyncIterator.prototype.throw = function (e) { | ||
var result; | ||
try { | ||
result = this.asyncIter.throw(e); | ||
} | ||
finally { | ||
this.options.logger(JSON.stringify(this.triggers) + ("->throw('" + JSON.stringify(e) + "') =>"), JSON.stringify(result)); | ||
} | ||
return result; | ||
}; | ||
PubSubAsyncIterator.prototype[iterall_1.$$asyncIterator] = function () { | ||
return this; | ||
}; | ||
return PubSubAsyncIterator; | ||
}()); | ||
exports.wrapPubSub = function (pubsub, options) { | ||
return new LoggedPubSub(pubsub, options); | ||
}; | ||
var LoggingLink = (function (_super) { | ||
__extends(LoggingLink, _super); | ||
function LoggingLink() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
function LoggingLink(options) { | ||
var _this = _super.call(this) || this; | ||
_this.options = getDefaultLogOptions(options); | ||
return _this; | ||
} | ||
LoggingLink.prototype.request = function (operation, forward) { | ||
var _this = this; | ||
var operationAST = graphql_1.getOperationAST(operation.query, operation.operationName); | ||
@@ -157,18 +200,18 @@ var isSubscription = !!operationAST && operationAST.operation === 'subscription'; | ||
if (isSubscription) { | ||
debug("subscribe <= " + formatRequest(operation)); | ||
_this.options.logger("subscribe <= " + _this.options.formatter(operation)); | ||
} | ||
var sub = forward(operation).subscribe({ | ||
next: function (result) { | ||
debug(JSON.stringify(result) + " <= " + formatRequest(operation)); | ||
_this.options.logger(JSON.stringify(result) + " <= " + _this.options.formatter(operation)); | ||
observer.next(result); | ||
}, | ||
error: function (error) { | ||
debug(JSON.stringify(error) + " <=e " + formatRequest(operation)); | ||
_this.options.logger(JSON.stringify(error) + " <=e " + _this.options.formatter(operation)); | ||
observer.error(error); | ||
}, | ||
complete: observer.complete.bind(observer), | ||
complete: observer.complete.bind(observer) | ||
}); | ||
return function () { | ||
if (isSubscription) { | ||
debug("unsubscribe <= " + formatRequest(operation)); | ||
_this.options.logger("unsubscribe <= " + _this.options.formatter(operation)); | ||
} | ||
@@ -182,14 +225,12 @@ sub.unsubscribe(); | ||
exports.LoggingLink = LoggingLink; | ||
exports.formatResponse = function (response, options) { | ||
debug(JSON.stringify(response) + " <= " + formatRequest(options)); | ||
exports.formatResponse = function (logOptions, response, options) { | ||
var logOpts = getDefaultLogOptions(logOptions); | ||
logOpts.logger(JSON.stringify(response) + " <= " + logOpts.formatter(options)); | ||
return response; | ||
}; | ||
exports.addApolloLogging = function (obj) { | ||
if (obj.publish) { | ||
return addPubSubLogger(obj); | ||
} | ||
else { | ||
throw new Error('Unknown object passed to Apollo Logger:' + JSON.stringify(obj)); | ||
} | ||
}; | ||
exports.default = function (options) { return ({ | ||
link: new LoggingLink(options), | ||
formatResponse: exports.formatResponse.bind(options), | ||
wrapPubSub: function (pubsub) { return exports.wrapPubSub(pubsub, options); } | ||
}); }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "apollo-logger", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Apollo GraphQL Logger", | ||
@@ -47,6 +47,8 @@ "main": "./lib/bundle.umd.js", | ||
"apollo-link": "^1.0.0", | ||
"graphql": "^0.11.7", | ||
"graphql-subscriptions": "^0.5.8", | ||
"husky": "^0.14.3", | ||
"iterall": "^1.2.2", | ||
"lint-staged": "^4.1.3", | ||
"prettier": "^1.6.1", | ||
"graphql": "^0.11.7", | ||
"rimraf": "^2.6.2", | ||
@@ -61,3 +63,4 @@ "rollup": "^0.50.0", | ||
"apollo-link": ">= 0.5.0", | ||
"graphql": ">= 0.10.0" | ||
"graphql": ">= 0.10.0", | ||
"iterall": ">= 1.0.0" | ||
}, | ||
@@ -64,0 +67,0 @@ "dependencies": { |
@@ -19,6 +19,8 @@ ## Apollo GraphQL Logger | ||
``` js | ||
import { LoggingLink, addApolloLogging, formatResponse } from 'apollo-logger'; | ||
import { LoggingLink, wrapPubSub, formatResponse } from 'apollo-logger'; | ||
const logOptions = { logger: console.log }; | ||
const link = ApolloLink.from([ | ||
new LoggingLink(), | ||
new LoggingLink(logOptions), | ||
new HttpLink({uri: ...}) | ||
@@ -29,7 +31,7 @@ ); | ||
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: myGraphQLSchema, formatResponse }); | ||
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: myGraphQLSchema, formatResponse: formatResponse.bind(logOptions) }); | ||
... | ||
const pubsub = addApolloLogging(new PubSub()); | ||
const pubsub = wrapPubSub(new PubSub(), logOptions); | ||
``` | ||
@@ -36,0 +38,0 @@ |
199
src/index.ts
@@ -1,63 +0,125 @@ | ||
import { print, getOperationAST } from 'graphql'; | ||
import { ApolloLink, Observable } from 'apollo-link'; | ||
import { ApolloLink, Observable, Operation } from 'apollo-link'; | ||
import { getOperationAST, print } from 'graphql'; | ||
import { PubSubEngine } from 'graphql-subscriptions'; | ||
import { $$asyncIterator } from 'iterall'; | ||
function debug(...args) { | ||
console.log.apply(null, args); | ||
export interface LogOptions { | ||
logger?: (...args: any[]) => void; | ||
formatter?: (input: FormatterInput) => string; | ||
} | ||
const formatRequest = req => | ||
(!req.variables || Object.keys(req.variables).length === 0) | ||
? req.operationName | ||
: `${req.operationName}(${JSON.stringify(req.variables)})`; | ||
export const defaultFormatter = req => | ||
!req.variables || Object.keys(req.variables).length === 0 | ||
? req.operationName | ||
: `${req.operationName}(${JSON.stringify(req.variables)})`; | ||
const addPubSubLogger = pubsub => ({ | ||
publish(...args) { | ||
debug('pubsub publish', args); | ||
return pubsub.publish(...args); | ||
}, | ||
async subscribe(opName, handler) { | ||
export const defaultLogger = (...args: any[]): void => console.log.apply(null, args); | ||
const getDefaultLogOptions = (options: LogOptions): LogOptions => { | ||
const result = options; | ||
if (!result.formatter) { | ||
result.formatter = defaultFormatter; | ||
} | ||
if (!result.logger) { | ||
result.logger = defaultLogger; | ||
} | ||
return result; | ||
}; | ||
export class LoggedPubSub implements PubSubEngine { | ||
private pubsub: PubSubEngine; | ||
private options: LogOptions; | ||
constructor(pubsub: PubSubEngine, options?: LogOptions) { | ||
this.pubsub = pubsub; | ||
this.options = getDefaultLogOptions(options); | ||
} | ||
public publish(triggerName: string, payload: any): boolean { | ||
this.options.logger('pubsub publish', triggerName, payload); | ||
return this.pubsub.publish(triggerName, payload); | ||
} | ||
public async subscribe(triggerName: string, onMessage: (...args: any[]) => void, options: any): Promise<number> { | ||
let result; | ||
try { | ||
const logHandler = (msg) => { | ||
debug('pubsub msg', `${opName}(${JSON.stringify(msg)})`); | ||
return handler(msg); | ||
const logHandler = (...msgArgs: any[]) => { | ||
this.options.logger('pubsub msg', `${triggerName}(${JSON.stringify(msgArgs)})`); | ||
onMessage(msgArgs); | ||
}; | ||
result = await pubsub.subscribe(opName, logHandler); | ||
result = await this.pubsub.subscribe(triggerName, logHandler, options); | ||
} finally { | ||
debug('pubsub subscribe', opName, '=>', result); | ||
this.options.logger('pubsub subscribe', triggerName, '=>', result); | ||
} | ||
return result; | ||
}, | ||
unsubscribe(...args) { | ||
debug('pubsub unsubscribe', args); | ||
return pubsub.unsubscribe(...args); | ||
}, | ||
asyncIterator(...args) { | ||
const asyncIter = pubsub.asyncIterator(...args); | ||
const trigger = args[0]; | ||
return Object.assign({}, asyncIter, { | ||
async next() { | ||
let result; | ||
try { | ||
result = await asyncIter.next(); | ||
} finally { | ||
debug(JSON.stringify(result), '<= ' + trigger + '->next'); | ||
} | ||
return result; | ||
}, | ||
throw(error) { | ||
let result; | ||
try { | ||
result = asyncIter.throw(error); | ||
} finally { | ||
debug(trigger + `->throw('${JSON.stringify(error)}') =>`, JSON.stringify(result)); | ||
} | ||
return result; | ||
}, | ||
}); | ||
}, | ||
}); | ||
} | ||
public unsubscribe(subId: number): void { | ||
this.options.logger('pubsub unsubscribe', subId); | ||
this.pubsub.unsubscribe(subId); | ||
} | ||
public asyncIterator<T>(triggers: string | string[]): AsyncIterator<T> { | ||
return new PubSubAsyncIterator<T>(this.pubsub, triggers, this.options); | ||
} | ||
} | ||
class PubSubAsyncIterator<T> implements AsyncIterator<T> { | ||
private triggers: string[]; | ||
private asyncIter: AsyncIterator<T>; | ||
private options: LogOptions; | ||
constructor(pubsub: PubSubEngine, triggers: string | string[], options?: LogOptions) { | ||
this.asyncIter = pubsub.asyncIterator<T>(triggers); | ||
this.triggers = typeof triggers === 'string' ? [triggers] : triggers; | ||
this.options = getDefaultLogOptions(options); | ||
} | ||
public async next(value?: any): Promise<IteratorResult<T>> { | ||
let result; | ||
try { | ||
result = await this.asyncIter.next(); | ||
} finally { | ||
this.options.logger(JSON.stringify(result), '<= ' + JSON.stringify(this.triggers) + '->next'); | ||
} | ||
return result; | ||
} | ||
public async return?(value?: any): Promise<IteratorResult<T>> { | ||
let result; | ||
try { | ||
result = await this.asyncIter.next(); | ||
} finally { | ||
this.options.logger(JSON.stringify(result), '<= ' + JSON.stringify(this.triggers) + '->next'); | ||
} | ||
return result; | ||
} | ||
public throw?(e?: any): Promise<IteratorResult<T>> { | ||
let result; | ||
try { | ||
result = this.asyncIter.throw(e); | ||
} finally { | ||
this.options.logger(JSON.stringify(this.triggers) + `->throw('${JSON.stringify(e)}') =>`, JSON.stringify(result)); | ||
} | ||
return result; | ||
} | ||
public [$$asyncIterator]() { | ||
return this; | ||
} | ||
} | ||
export const wrapPubSub = (pubsub: PubSubEngine, options: LogOptions): PubSubEngine => | ||
new LoggedPubSub(pubsub, options); | ||
export class LoggingLink extends ApolloLink { | ||
request(operation, forward) { | ||
private options: LogOptions; | ||
constructor(options?: LogOptions) { | ||
super(); | ||
this.options = getDefaultLogOptions(options); | ||
} | ||
public request(operation, forward) { | ||
const operationAST = getOperationAST(operation.query, operation.operationName); | ||
@@ -67,7 +129,7 @@ const isSubscription = !!operationAST && operationAST.operation === 'subscription'; | ||
if (isSubscription) { | ||
debug(`subscribe <= ${formatRequest(operation)}`); | ||
this.options.logger(`subscribe <= ${this.options.formatter(operation)}`); | ||
} | ||
const sub = forward(operation).subscribe({ | ||
next: result => { | ||
debug(`${JSON.stringify(result)} <= ${formatRequest(operation)}`); | ||
this.options.logger(`${JSON.stringify(result)} <= ${this.options.formatter(operation)}`); | ||
@@ -77,14 +139,14 @@ observer.next(result); | ||
error: error => { | ||
debug(`${JSON.stringify(error)} <=e ${formatRequest(operation)}`); | ||
this.options.logger(`${JSON.stringify(error)} <=e ${this.options.formatter(operation)}`); | ||
observer.error(error); | ||
}, | ||
complete: observer.complete.bind(observer), | ||
complete: observer.complete.bind(observer) | ||
}); | ||
return () => { | ||
if (isSubscription) { | ||
debug(`unsubscribe <= ${formatRequest(operation)}`); | ||
this.options.logger(`unsubscribe <= ${this.options.formatter(operation)}`); | ||
} | ||
sub.unsubscribe(); | ||
} | ||
}; | ||
}); | ||
@@ -94,14 +156,19 @@ } | ||
export const formatResponse = (response, options) => { | ||
debug(`${JSON.stringify(response)} <= ${formatRequest(options)}`); | ||
export interface FormatterInput { | ||
operation: Operation; | ||
result?: any; | ||
error?: any; | ||
} | ||
export const formatResponse = (logOptions: LogOptions, response: any, options: any) => { | ||
const logOpts = getDefaultLogOptions(logOptions); | ||
logOpts.logger(`${JSON.stringify(response)} <= ${logOpts.formatter(options)}`); | ||
return response; | ||
} | ||
}; | ||
export const addApolloLogging = obj => { | ||
if (obj.publish) { | ||
return addPubSubLogger(obj); | ||
} else { | ||
throw new Error('Unknown object passed to Apollo Logger:' + JSON.stringify(obj)); | ||
} | ||
}; | ||
export default (options?: LogOptions) => ({ | ||
link: new LoggingLink(options), | ||
formatResponse: formatResponse.bind(options), | ||
wrapPubSub: (pubsub: PubSubEngine) => wrapPubSub(pubsub, options) | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60990
643
92
4
15