apollo-logger
Advanced tools
Comparing version 0.0.6 to 0.1.0
@@ -1,3 +0,10 @@ | ||
export declare const enableApolloLogging: () => boolean; | ||
export declare const disableApolloLogging: () => boolean; | ||
export declare const addApolloLogging: (obj: any) => any; | ||
import { ApolloLink } from 'apollo-link'; | ||
export declare class LoggingLink extends ApolloLink { | ||
request(operation: any, forward: 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; | ||
}; |
176
lib/index.js
@@ -1,2 +0,11 @@ | ||
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -37,63 +46,16 @@ return new (P || (P = Promise))(function (resolve, reject) { | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var apolloLogging = true; | ||
exports.enableApolloLogging = function () { return apolloLogging = true; }; | ||
exports.disableApolloLogging = function () { return apolloLogging = false; }; | ||
import { ApolloLink } from 'apollo-link'; | ||
function debug() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
console.log.apply(null, args); | ||
} | ||
var formatRequest = function (req) { | ||
return !req.variables ? req.operationName : req.operationName + "(" + JSON.stringify(req.variables) + ")"; | ||
return (!req.variables || Object.keys(req.variables).length === 0) | ||
? req.operationName | ||
: req.operationName + "(" + JSON.stringify(req.variables) + ")"; | ||
}; | ||
var addNetworkInterfaceLogger = function (netIfc) { | ||
return { | ||
query: function (request) { | ||
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, netIfc.query(request)]; | ||
case 1: | ||
result = _a.sent(); | ||
return [3, 3]; | ||
case 2: | ||
if (apolloLogging) { | ||
console.log(formatRequest(request), '=>', JSON.stringify(result)); | ||
} | ||
return [7]; | ||
case 3: return [2, result]; | ||
} | ||
}); | ||
}); | ||
}, | ||
subscribe: function (request, handler) { | ||
var result; | ||
try { | ||
var logHandler = function (err, res) { | ||
if (apolloLogging) { | ||
console.log(err ? "error caught: " + JSON.stringify(err) : JSON.stringify(res)); | ||
} | ||
return handler(err, res); | ||
}; | ||
result = netIfc.subscribe(request, logHandler); | ||
} | ||
finally { | ||
if (apolloLogging) { | ||
console.log(formatRequest(request), '=> subscription:', result); | ||
} | ||
} | ||
return result; | ||
}, | ||
unsubscribe: function (subId) { | ||
try { | ||
netIfc.unsubscribe(subId); | ||
} | ||
finally { | ||
if (apolloLogging) { | ||
console.log('unsubscribe from subscription:', subId); | ||
} | ||
} | ||
}, | ||
}; | ||
}; | ||
var addPubSubLogging = function (pubsub) { return ({ | ||
var addPubSubLogger = function (pubsub) { return ({ | ||
publish: function () { | ||
@@ -104,3 +66,3 @@ var args = []; | ||
} | ||
console.log('pubsub publish', args); | ||
debug('pubsub publish', args); | ||
return pubsub.publish.apply(pubsub, args); | ||
@@ -115,4 +77,4 @@ }, | ||
_a.trys.push([0, , 2, 3]); | ||
logHandler = !apolloLogging ? handler : function (msg) { | ||
console.log('pubsub msg', opName + "(" + JSON.stringify(msg) + ")"); | ||
logHandler = function (msg) { | ||
debug('pubsub msg', opName + "(" + JSON.stringify(msg) + ")"); | ||
return handler(msg); | ||
@@ -125,5 +87,3 @@ }; | ||
case 2: | ||
if (apolloLogging) { | ||
console.log('pubsub subscribe', opName, '=>', result); | ||
} | ||
debug('pubsub subscribe', opName, '=>', result); | ||
return [7]; | ||
@@ -140,3 +100,3 @@ case 3: return [2, result]; | ||
} | ||
console.log('pubsub unsubscribe', args); | ||
debug('pubsub unsubscribe', args); | ||
return pubsub.unsubscribe.apply(pubsub, args); | ||
@@ -164,5 +124,3 @@ }, | ||
case 2: | ||
if (apolloLogging) { | ||
console.log(trigger + "->next =>", JSON.stringify(result)); | ||
} | ||
debug(JSON.stringify(result), "<= " + trigger + "->next"); | ||
return [7]; | ||
@@ -180,5 +138,3 @@ case 3: return [2, result]; | ||
finally { | ||
if (apolloLogging) { | ||
console.log(trigger + ("->throw(\"" + JSON.stringify(error) + "\") =>"), JSON.stringify(result)); | ||
} | ||
debug(trigger + ("->throw(\"" + JSON.stringify(error) + "\") =>"), JSON.stringify(result)); | ||
} | ||
@@ -190,45 +146,45 @@ return result; | ||
}); }; | ||
var addSubscriptionManagerLogger = function (manager) { | ||
var setupFunctions = manager.setupFunctions; | ||
manager.setupFunctions = {}; | ||
var _loop_1 = function (func) { | ||
manager.setupFunctions[func] = function (opts, args, name) { | ||
var triggerMap = setupFunctions[func](opts, args, name); | ||
var loggedMap = {}; | ||
var _loop_2 = function (key) { | ||
loggedMap[key] = Object.assign({}, triggerMap[key]); | ||
var originalFilter = triggerMap[key].filter; | ||
if (originalFilter) { | ||
loggedMap[key].filter = function (val, ctx) { | ||
var result; | ||
try { | ||
result = originalFilter(val, ctx); | ||
var LoggingLink = (function (_super) { | ||
__extends(LoggingLink, _super); | ||
function LoggingLink() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
LoggingLink.prototype.request = function (operation, forward) { | ||
var observable = forward(operation); | ||
if (observable.map) { | ||
return observable.map(function (result) { | ||
debug(JSON.stringify(result) + " <= " + formatRequest(operation)); | ||
return result; | ||
}); | ||
} | ||
else { | ||
observable.subscribe(function (result) { | ||
debug(JSON.stringify(result) + " <= " + formatRequest(operation)); | ||
return result; | ||
}); | ||
return { | ||
subscribe: function () { | ||
debug("subscribe <= " + formatRequest(operation)); | ||
var result = observable.subscribe.apply(observable, arguments); | ||
return { | ||
unsubscribe: function () { | ||
debug("unsubscribe <= " + formatRequest(operation)); | ||
return result.unsubscribe.apply(observable, arguments); | ||
} | ||
finally { | ||
if (apolloLogging) { | ||
console.log("pubsub filter " + key + "(opts = " + JSON.stringify(opts) + ", args = " + JSON.stringify(args) + ", name = " + name + ")"); | ||
console.log("." + key + "(val = " + JSON.stringify(val) + ", ctx = " + JSON.stringify(ctx) + ") =>", result); | ||
} | ||
} | ||
return result; | ||
}; | ||
} | ||
}, | ||
}; | ||
for (var _i = 0, _a = Object.keys(triggerMap); _i < _a.length; _i++) { | ||
var key = _a[_i]; | ||
_loop_2(key); | ||
} | ||
return loggedMap; | ||
}; | ||
} | ||
}; | ||
for (var _i = 0, _a = Object.keys(setupFunctions); _i < _a.length; _i++) { | ||
var func = _a[_i]; | ||
_loop_1(func); | ||
return LoggingLink; | ||
}(ApolloLink)); | ||
export { LoggingLink }; | ||
export var addApolloLogging = function (obj) { | ||
if (obj.publish) { | ||
return addPubSubLogger(obj); | ||
} | ||
return manager; | ||
else { | ||
throw new Error("Unknown object passed to Apollo Logger:" + JSON.stringify(obj)); | ||
} | ||
}; | ||
exports.addApolloLogging = function (obj) { return obj.query ? | ||
addNetworkInterfaceLogger(obj) : (obj.setupFunctions ? | ||
addSubscriptionManagerLogger(obj) : | ||
addPubSubLogging(obj)); }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "apollo-logger", | ||
"version": "0.0.6", | ||
"version": "0.1.0", | ||
"description": "Apollo GraphQL Logger", | ||
"main": "lib/index.js", | ||
"main": "./lib/bundle.umd.js", | ||
"module": "./lib/index.js", | ||
"jsnext:main": "./lib/index.js", | ||
"typings": "lib/index.d.ts", | ||
"scripts": { | ||
"clean": "rm -rf ./lib", | ||
"compile": "tsc", | ||
"clean": "rimraf lib/*", | ||
"build:browser": "browserify ./lib/bundle.umd.js -o=./lib/bundle.js && npm run minify:browser", | ||
"bundle": "rollup -c", | ||
"build": "tsc -p .", | ||
"watch": "tsc -w", | ||
"lint": "tslint 'src/*.ts*' -p tsconfig.json --type-check && tslint 'test/*.ts*' -p tsconfig.json --type-check", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"prepublish": "npm run clean && npm run compile" | ||
"minify:browser": "uglifyjs -c -m -o ./lib/bundle.min.js -- ./lib/bundle.js", | ||
"postbuild": "npm run bundle", | ||
"prebuild": "npm run clean", | ||
"prepublishOnly": "npm run clean && npm run build" | ||
}, | ||
@@ -38,5 +45,16 @@ "repository": { | ||
"@types/node": "^7.0.18", | ||
"apollo-link": "^0.6.1-beta.4", | ||
"graphql": "^0.10.5", | ||
"rimraf": "^2.6.2", | ||
"rollup": "^0.50.0", | ||
"tslint": "^5.2.0", | ||
"typescript": "^2.3.2" | ||
}, | ||
"peerDependencies": { | ||
"apollo-link": "^0.6.1-beta.4", | ||
"graphql": ">=0.10.0" | ||
}, | ||
"dependencies": { | ||
"debug": "^3.0.1" | ||
} | ||
} |
@@ -14,16 +14,14 @@ ## Apollo GraphQL Logger | ||
For full logging you will need to attach Apollo Logger to: | ||
- Network interface | ||
- Subscription manager | ||
- Apollo Link | ||
- And PubSub | ||
``` js | ||
import { addApolloLogging } from 'apollo-logger'; | ||
import { LoggingLink, addApolloLogging } from 'apollo-logger'; | ||
let networkInterface = addApolloLogging(createBatchingNetworkInterface({ | ||
... | ||
})); | ||
const link = ApolloLink.from([ | ||
new LoggingLink(), | ||
new HttpLink({uri: ...}) | ||
); | ||
const pubsub = addApolloLogging(new PubSub()); | ||
const subscriptionManager = addApolloLogging(new SubscriptionManager(...)); | ||
``` | ||
@@ -37,3 +35,3 @@ | ||
``` js | ||
getPost({"id":"3"}) => {"data":{"post":{"id":"3","title":"Post title 3", "__typename":"Post"}}} | ||
{"data":{"post":{"id":20,"title":"Post title 20","content":"Post content 20","__typename":"Post","comments":[{"id":39,"content":"Comment title 1 for post 20","__typename":"Comment"},{"id":40,"content":"Comment title 2 for post 20","__typename":"Comment"}]}}} <= post({"id":"20"}) | ||
``` | ||
@@ -43,3 +41,3 @@ | ||
``` js | ||
addCount({"amount":1}) => {"data":{"addCount":{"amount":17,"__typename":"Count"}}} | ||
{"data":{"addCounter":{"amount":21,"__typename":"Counter"}}} <= addCounter({"amount":1}) | ||
``` | ||
@@ -49,3 +47,3 @@ | ||
``` js | ||
onCountUpdated({}) => subscription: 2 | ||
subscribe <= onPostUpdated({"endCursor":11}) | ||
``` | ||
@@ -55,3 +53,3 @@ | ||
``` js | ||
{"commentUpdated":{"mutation":"CREATED","id":"3003", ... ,"__typename":"UpdateCommentPayload"}} | ||
{"data":{"counterUpdated":{"amount":21,"__typename":"Counter"}}} <= onCounterUpdated | ||
``` | ||
@@ -61,3 +59,3 @@ | ||
``` js | ||
unsubscribe from subscription: 2 | ||
unsubscribe <= onPostUpdated({"endCursor":11}) | ||
``` | ||
@@ -86,8 +84,2 @@ | ||
- PubSub filter check on a server: | ||
``` js | ||
pubsub filter postsUpdated(opts = {"query":...,"context":{}}, args = {"endCursor":10}, name = postsUpdated) | ||
.postsUpdated(val = {"mutation":"CREATED","id":21,"node":{"id":21,"title":"New post 1"}}, ctx = {}) => true | ||
``` | ||
## License | ||
@@ -94,0 +86,0 @@ Copyright © 2017 [SysGears INC]. This source code is licensed under the [MIT] license. |
134
src/index.ts
@@ -1,48 +0,16 @@ | ||
let apolloLogging = true; | ||
import { print } from 'graphql'; | ||
import { ApolloLink } from 'apollo-link'; | ||
export const enableApolloLogging = () => apolloLogging = true; | ||
export const disableApolloLogging = () => apolloLogging = false; | ||
function debug(...args) { | ||
console.log.apply(null, args); | ||
} | ||
const formatRequest = req => | ||
!req.variables ? req.operationName : `${req.operationName}(${JSON.stringify(req.variables)})`; | ||
(!req.variables || Object.keys(req.variables).length === 0) | ||
? req.operationName | ||
: `${req.operationName}(${JSON.stringify(req.variables)})`; | ||
const addNetworkInterfaceLogger = netIfc => { | ||
return { | ||
async query(request) { | ||
let result; | ||
try { | ||
result = await netIfc.query(request); | ||
} finally { | ||
if (apolloLogging) { console.log(formatRequest(request), '=>', JSON.stringify(result)); } | ||
} | ||
return result; | ||
}, | ||
subscribe(request, handler) { | ||
let result; | ||
try { | ||
const logHandler = (err, res) => { | ||
if (apolloLogging) { | ||
console.log(err ? "error caught: " + JSON.stringify(err) : JSON.stringify(res)); | ||
} | ||
return handler(err, res); | ||
}; | ||
result = netIfc.subscribe(request, logHandler); | ||
} finally { | ||
if (apolloLogging) { console.log(formatRequest(request), '=> subscription:', result); } | ||
} | ||
return result; | ||
}, | ||
unsubscribe(subId) { | ||
try { | ||
netIfc.unsubscribe(subId); | ||
} finally { | ||
if (apolloLogging) { console.log('unsubscribe from subscription:', subId); } | ||
} | ||
}, | ||
}; | ||
}; | ||
const addPubSubLogging = pubsub => ({ | ||
const addPubSubLogger = pubsub => ({ | ||
publish(...args) { | ||
console.log('pubsub publish', args); | ||
debug('pubsub publish', args); | ||
return pubsub.publish(...args); | ||
@@ -53,4 +21,4 @@ }, | ||
try { | ||
const logHandler = !apolloLogging ? handler : (msg) => { | ||
console.log('pubsub msg', `${opName}(${JSON.stringify(msg)})`); | ||
const logHandler = (msg) => { | ||
debug('pubsub msg', `${opName}(${JSON.stringify(msg)})`); | ||
return handler(msg); | ||
@@ -60,3 +28,3 @@ }; | ||
} finally { | ||
if (apolloLogging) { console.log('pubsub subscribe', opName, '=>', result); } | ||
debug('pubsub subscribe', opName, '=>', result); | ||
} | ||
@@ -66,3 +34,3 @@ return result; | ||
unsubscribe(...args) { | ||
console.log('pubsub unsubscribe', args); | ||
debug('pubsub unsubscribe', args); | ||
return pubsub.unsubscribe(...args); | ||
@@ -79,3 +47,3 @@ }, | ||
} finally { | ||
if (apolloLogging) { console.log(trigger + "->next =>", JSON.stringify(result)); } | ||
debug(JSON.stringify(result), "<= " + trigger + "->next"); | ||
} | ||
@@ -89,3 +57,3 @@ return result; | ||
} finally { | ||
if (apolloLogging) { console.log(trigger + `->throw("${JSON.stringify(error)}") =>`, JSON.stringify(result)); } | ||
debug(trigger + `->throw("${JSON.stringify(error)}") =>`, JSON.stringify(result)); | ||
} | ||
@@ -98,38 +66,40 @@ return result; | ||
const addSubscriptionManagerLogger = manager => { | ||
const setupFunctions = manager.setupFunctions; | ||
manager.setupFunctions = {}; | ||
for (let func of Object.keys(setupFunctions)) { | ||
manager.setupFunctions[func] = (opts, args, name) => { | ||
let triggerMap = setupFunctions[func](opts, args, name); | ||
const loggedMap = {}; | ||
for (let key of Object.keys(triggerMap)) { | ||
loggedMap[key] = Object.assign({}, triggerMap[key]); | ||
const originalFilter = triggerMap[key].filter; | ||
if (originalFilter) { | ||
loggedMap[key].filter = (val, ctx) => { | ||
let result; | ||
try { | ||
result = originalFilter(val, ctx); | ||
} finally { | ||
if (apolloLogging) { | ||
console.log(`pubsub filter ${key}(opts = ${JSON.stringify(opts)}, args = ${JSON.stringify(args)}, name = ${name})`); | ||
console.log(`.${key}(val = ${JSON.stringify(val)}, ctx = ${JSON.stringify(ctx)}) =>`, result); | ||
} | ||
export class LoggingLink extends ApolloLink { | ||
request(operation, forward) { | ||
const observable = forward(operation); | ||
if (observable.map) { | ||
return observable.map(result => { | ||
debug(`${JSON.stringify(result)} <= ${formatRequest(operation)}`); | ||
return result; | ||
}); | ||
} else { | ||
observable.subscribe(result => { | ||
debug(`${JSON.stringify(result)} <= ${formatRequest(operation)}`); | ||
return result; | ||
}); | ||
return { | ||
subscribe() { | ||
debug(`subscribe <= ${formatRequest(operation)}`); | ||
const result = observable.subscribe.apply(observable, arguments); | ||
return { | ||
unsubscribe() { | ||
debug(`unsubscribe <= ${formatRequest(operation)}`); | ||
return result.unsubscribe.apply(observable, arguments); | ||
} | ||
return result; | ||
}; | ||
} | ||
} | ||
return loggedMap; | ||
}; | ||
} | ||
}, | ||
}; | ||
} | ||
} | ||
return manager; | ||
} | ||
export const addApolloLogging = obj => { | ||
if (obj.publish) { | ||
return addPubSubLogger(obj) | ||
} else { | ||
throw new Error("Unknown object passed to Apollo Logger:" + JSON.stringify(obj)); | ||
} | ||
}; | ||
export const addApolloLogging = obj => obj.query ? | ||
addNetworkInterfaceLogger(obj) : ( | ||
obj.setupFunctions ? | ||
addSubscriptionManagerLogger(obj) : | ||
addPubSubLogging(obj) | ||
); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
44885
9
471
3
7
83
1
+ Addeddebug@^3.0.1
+ Addedapollo-link@0.6.1-beta.6(transitive)
+ Addedapollo-utilities@0.2.0-rc.3(transitive)
+ Addeddebug@3.2.7(transitive)
+ Addedgraphql@0.11.716.10.0(transitive)
+ Addediterall@1.1.3(transitive)
+ Addedms@2.1.3(transitive)
+ Addedzen-observable-ts@0.4.4(transitive)