Skip to content

Commit ddff564

Browse files
authored
feat: mercurius-logging first release (#1)
* feat: basic impl * add: test * add: docs * feat: prependAlias
1 parent d8cf7fe commit ddff564

File tree

7 files changed

+428
-1
lines changed

7 files changed

+428
-1
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extends": "standard"}

.github/workflows/ci.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- 'docs/**'
7+
- '*.md'
8+
pull_request:
9+
paths-ignore:
10+
- 'docs/**'
11+
- '*.md'
12+
13+
jobs:
14+
test:
15+
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
16+
with:
17+
lint: true

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
.vscode/
106+
package-lock.json

README.md

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,132 @@
11
# mercurius-logging
2-
Log the GraphQL details
2+
3+
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
4+
[![ci](https://github.com/Eomm/mercurius-logging/actions/workflows/ci.yml/badge.svg)](https://github.com/Eomm/mercurius-logging/actions/workflows/ci.yml)
5+
6+
This plugin add a Log with all the GraphQL details you need.
7+
8+
## The issue
9+
10+
By default, Fastify logs a simple request that shows always the same `url`:
11+
12+
```json
13+
{
14+
"level": 30,
15+
"time": 1660395516356,
16+
"pid": 83316,
17+
"hostname": "eomm",
18+
"name": "gateway",
19+
"reqId": "req-1",
20+
"req": {
21+
"method": "POST",
22+
"url": "/graphql",
23+
"hostname": "localhost:60767",
24+
"remoteAddress": "127.0.0.1",
25+
"remotePort": 60769
26+
},
27+
"msg": "incoming request"
28+
}
29+
```
30+
31+
This output does not let you know which queries or mutations are being executed,
32+
unless you print or inspect the GQL payload.
33+
34+
This plugin adds this log output to your application:
35+
36+
```json
37+
{
38+
"level": 30,
39+
"time": 1660395516406,
40+
"pid": 83316,
41+
"hostname": "eomm",
42+
"name": "gateway",
43+
"reqId": "req-1",
44+
"graphql": {
45+
"queries": [
46+
"myTeam",
47+
"myTeam"
48+
]
49+
}
50+
}
51+
```
52+
53+
When the request contains some mutations:
54+
55+
```json
56+
{
57+
"level": 30,
58+
"time": 1660395516406,
59+
"pid": 83316,
60+
"hostname": "eomm",
61+
"name": "gateway",
62+
"reqId": "req-1",
63+
"graphql": {
64+
"mutations": [
65+
"resetCounter"
66+
]
67+
}
68+
}
69+
```
70+
71+
## Install
72+
73+
```
74+
npm install mercurius-logging
75+
```
76+
77+
## Usage
78+
79+
```js
80+
const Fastify = require('fastify')
81+
const mercurius = require('mercurius')
82+
const mercuriusLogging = require('mercurius-logging')
83+
84+
const app = Fastify({
85+
logger: true,
86+
disableRequestLogging: true
87+
})
88+
t.teardown(app.close.bind(app))
89+
90+
app.register(mercurius, {
91+
schema: yourSchema,
92+
resolvers: yourResolvers
93+
})
94+
app.register(mercuriusLogging)
95+
```
96+
97+
## Options
98+
99+
You can customize the output of the plugin by passing an options object:
100+
101+
```js
102+
app.register(mercuriusLogging, {
103+
logLevel: 'debug', // default: 'info'
104+
prependAlias: true, // default: false
105+
})
106+
```
107+
108+
### logLevel
109+
110+
The log level of the plugin. Note that the `request` logger is used, so you will get the additional
111+
[request data](https://www.fastify.io/docs/latest/Reference/Logging/#usage) such as the `reqId`.
112+
113+
### prependAlias
114+
115+
Queries and mutations may have an alias. If you want to append the alias to the log, set this option to `true`.
116+
You will get the following output:
117+
118+
```json
119+
{
120+
"level": 30,
121+
"graphql": {
122+
"queries": [
123+
"firstQuery:myTeam",
124+
"secondQuery:myTeam"
125+
]
126+
}
127+
}
128+
```
129+
130+
## License
131+
132+
Copyright [Manuel Spigolon](https://github.com/Eomm), Licensed under [MIT](./LICENSE).

index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
3+
const fp = require('fastify-plugin')
4+
5+
function mercuriusLogging (app, opts, next) {
6+
const options = Object.assign({}, {
7+
logLevel: 'info',
8+
prependAlias: false
9+
}, opts)
10+
11+
app.graphql.addHook('preExecution', logGraphQLDetails.bind(null, options))
12+
next()
13+
}
14+
15+
function logGraphQLDetails (opts, schema, document, context) {
16+
const queryOps = readOps(document, 'query', opts)
17+
const mutationOps = readOps(document, 'mutation', opts)
18+
19+
context.reply.request.log[opts.logLevel]({
20+
graphql: {
21+
queries: queryOps.length > 0 ? queryOps : undefined,
22+
mutations: mutationOps.length > 0 ? mutationOps : undefined
23+
}
24+
})
25+
}
26+
27+
function readOps (document, operation, opts) {
28+
return document.definitions
29+
.filter(d => d.kind === 'OperationDefinition' && d.operation === operation)
30+
.flatMap(d => d.selectionSet.selections)
31+
.map(selectionSet => {
32+
const opName = selectionSet.name.value
33+
34+
if (opts.prependAlias && selectionSet.alias) {
35+
return selectionSet.alias.value + ':' + opName
36+
}
37+
38+
return opName
39+
})
40+
}
41+
42+
module.exports = fp(mercuriusLogging,
43+
{
44+
name: 'mercurius-logging',
45+
fastify: '4.x',
46+
dependencies: ['mercurius']
47+
}
48+
)

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "mercurius-logging",
3+
"version": "0.0.0",
4+
"description": "Log the GraphQL details",
5+
"main": "index.js",
6+
"scripts": {
7+
"lint": "standard",
8+
"lint:fix": "standard --fix",
9+
"test": "npm run lint && npm run test:unit",
10+
"test:unit": "tap test/**/*.test.js"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/Eomm/mercurius-logging.git"
15+
},
16+
"keywords": [],
17+
"author": "Manuel Spigolon <behemoth89@gmail.com> (https://github.com/Eomm)",
18+
"license": "MIT",
19+
"bugs": {
20+
"url": "https://github.com/Eomm/mercurius-logging/issues"
21+
},
22+
"homepage": "https://github.com/Eomm/mercurius-logging#readme",
23+
"devDependencies": {
24+
"fastify": "^4.4.0",
25+
"mercurius": "^10.1.0",
26+
"split2": "^4.1.0",
27+
"standard": "^17.0.0",
28+
"tap": "^16.3.0"
29+
},
30+
"dependencies": {
31+
"fastify-plugin": "^4.2.0"
32+
}
33+
}

0 commit comments

Comments
 (0)