├── .gitignore ├── index.js ├── result.gif ├── LICENSE ├── package.json ├── lib └── tracer.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | module.exports = require('./lib/tracer') 4 | -------------------------------------------------------------------------------- /result.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EasyGraphQL/easygraphql-tracing/HEAD/result.gif -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ###The MIT License 2 | 3 | Copyright (c) 2019 EasyGraphQL 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "easygraphql-tracing", 3 | "version": "0.0.3", 4 | "description": "Get a tracing of your GraphQL resolvers", 5 | "main": "index.js", 6 | "scripts": { 7 | "prettier:check": "prettier --write './{lib,test,utils}/**/*.{js,graphql}'", 8 | "prettier:format": "prettier --write './{lib,test,utils}/**/*.{js,graphql}'", 9 | "test": "echo \"Error: no test specified\" && exit 1" 10 | }, 11 | "husky": { 12 | "hooks": { 13 | "pre-commit": "npm run prettier:format && git add ." 14 | } 15 | }, 16 | "keywords": [], 17 | "homepage": "https://github.com/EasyGraphQL/easygraphql-tracing", 18 | "author": { 19 | "name": "EasyGraphQL", 20 | "url": "https://github.com/EasyGraphQL" 21 | }, 22 | "repository": { 23 | "type": "git", 24 | "url": "https://github.com/EasyGraphQL/easygraphql-tracing" 25 | }, 26 | "bugs": { 27 | "url": "https://github.com/EasyGraphQL/easygraphql-tracing" 28 | }, 29 | "license": "MIT", 30 | "peerDependencies": { 31 | "graphql": "^14.0.0" 32 | }, 33 | "dependencies": { 34 | "graphql": "^14.0.2", 35 | "cli-table": "^0.3.1" 36 | }, 37 | "devDependencies": { 38 | "husky": "^1.2.1", 39 | "prettier": "^1.18.2" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /lib/tracer.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const { responsePathAsArray, defaultFieldResolver } = require("graphql"); 4 | const Table = require("cli-table"); 5 | 6 | class Tracer { 7 | constructor() { 8 | this.table = new Table({ 9 | head: [ 10 | "Field name", 11 | "Parent Type", 12 | "Return Type", 13 | "Path", 14 | "Duration (ns)", 15 | "Duration (ms)" 16 | ] 17 | }); 18 | this.tracingResolvers = []; 19 | } 20 | 21 | fieldResolver() { 22 | const self = this; 23 | return function(value, args, ctx, info) { 24 | const tracerResult = {}; 25 | 26 | tracerResult.fieldName = info.fieldName; 27 | tracerResult.path = [...responsePathAsArray(info.path)]; 28 | tracerResult.parentType = info.parentType; 29 | tracerResult.returnType = info.returnType; 30 | tracerResult.startTime = duration(process.hrtime()); 31 | 32 | self.tracingResolvers.push(tracerResult); 33 | 34 | const result = defaultFieldResolver(value, args, ctx, info); 35 | 36 | tracerResult.endTime = duration(process.hrtime()); 37 | 38 | return result; 39 | }; 40 | } 41 | 42 | tracing() { 43 | const self = this; 44 | return function(req, res, next) { 45 | res.once("finish", function() { 46 | self.tracingResolvers = self.tracingResolvers.map(tracingResolver => { 47 | const duration = tracingResolver.endTime - tracingResolver.startTime; 48 | self.table.push([ 49 | tracingResolver.fieldName, 50 | tracingResolver.parentType, 51 | tracingResolver.returnType, 52 | tracingResolver.path.join(" - "), 53 | duration, 54 | duration / 1e6 55 | ]); 56 | 57 | return { ...tracingResolver, duration }; 58 | }); 59 | 60 | if (self.tracingResolvers.length) { 61 | console.log(self.table.toString()); 62 | } 63 | }); 64 | 65 | self.table = new Table({ 66 | head: [ 67 | "Field name", 68 | "Parent Type", 69 | "Return Type", 70 | "Path", 71 | "Duration (ns)", 72 | "Duration (ms)" 73 | ] 74 | }); 75 | self.tracingResolvers = []; 76 | 77 | next(); 78 | }; 79 | } 80 | } 81 | 82 | function duration(hrtime) { 83 | return hrtime[0] * 1e9 + hrtime[1]; 84 | } 85 | 86 | const tracer = new Tracer(); 87 | 88 | module.exports = { 89 | tracing: tracer.tracing(), 90 | fieldResolver: tracer.fieldResolver() 91 | }; 92 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
3 |
69 |
70 |
71 | ## License
72 | ### The MIT License
73 |
74 | Copyright (c) 2019 EasyGraphQL
75 |
76 | Permission is hereby granted, free of charge, to any person obtaining a copy
77 | of this software and associated documentation files (the "Software"), to deal
78 | in the Software without restriction, including without limitation the rights
79 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
80 | copies of the Software, and to permit persons to whom the Software is
81 | furnished to do so, subject to the following conditions:
82 |
83 | The above copyright notice and this permission notice shall be included in
84 | all copies or substantial portions of the Software.
85 |
86 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
87 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
88 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
89 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
90 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
91 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
92 | THE SOFTWARE.
93 |
--------------------------------------------------------------------------------