From 15d4c1300a895451309e2a3bd03fb2026e27af16 Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Thu, 19 Sep 2024 14:48:56 +0200 Subject: [PATCH] major: upgrade to fastify v5 (#63) --- .github/workflows/ci.yml | 2 +- .gitignore | 5 +- .npmignore | 4 +- README.md | 10 ++- index.js | 2 +- package.json | 17 ++--- test/handle-reply-undefined.test.js | 12 +-- test/issue-22.test.js | 18 ++--- test/logMessage.test.js | 62 +++++++-------- test/plugin.test.js | 112 ++++++++++++++-------------- 10 files changed, 126 insertions(+), 118 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d84fadc..1e07ba0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,6 +12,6 @@ on: jobs: test: - uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3 + uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5.0.0 with: lint: true diff --git a/.gitignore b/.gitignore index d3ce8fa..002981c 100644 --- a/.gitignore +++ b/.gitignore @@ -104,6 +104,5 @@ dist .tern-port .vscode/ package-lock.json - -# Wallaby.js configuration -wallaby.conf.js +.tap +asd* diff --git a/.npmignore b/.npmignore index b552336..f032011 100644 --- a/.npmignore +++ b/.npmignore @@ -109,4 +109,6 @@ package-lock.json wallaby.conf.js test/* .github/* -.eslintrc \ No newline at end of file +.eslintrc +.tap +asd* diff --git a/README.md b/README.md index c40afed..db3eae4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # mercurius-logging -[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) [![ci](https://github.com/Eomm/mercurius-logging/actions/workflows/ci.yml/badge.svg)](https://github.com/Eomm/mercurius-logging/actions/workflows/ci.yml) +[![npm](https://img.shields.io/npm/v/mercurius-logging)](https://www.npmjs.com/package/mercurius-logging) +[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) This plugin add a Log with all the GraphQL details you need. @@ -112,6 +113,13 @@ Otherwise, this plugin will ignore the request. npm install mercurius-logging ``` +### Compatibility + +| Plugin version | Fastify version | +| ------------- |:---------------:| +| `^2.0.0` | `^5.0.0` | +| `^1.0.0` | `^4.0.0` | + ## Usage ```js diff --git a/index.js b/index.js index 908e01e..aec30ac 100644 --- a/index.js +++ b/index.js @@ -126,7 +126,7 @@ function readOps (document, operation, opts) { const plugin = fp(mercuriusLogging, { name: 'mercurius-logging', - fastify: '4.x', + fastify: '^5.x', dependencies: ['mercurius'] } ) diff --git a/package.json b/package.json index b21e9a3..f59a4e1 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,8 @@ "lint": "standard && npm run lint:typescript", "lint:fix": "standard --fix", "lint:typescript": "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin types/**/*.ts", - "test": "c8 --100 ava && tsd", - "test:unit": "ava", + "test": "tap --show-full-coverage && tsd", + "test:unit": "tap --show-full-coverage", "test:types": "tsd" }, "repository": { @@ -35,18 +35,17 @@ "homepage": "https://github.com/Eomm/mercurius-logging#readme", "devDependencies": { "@types/node": "^22.0.0", - "@typescript-eslint/eslint-plugin": "^6.4.0", - "@typescript-eslint/parser": "^6.4.0", - "ava": "^5.3.1", - "c8": "^10.1.2", - "fastify": "^4.4.0", + "@typescript-eslint/eslint-plugin": "^7.0.0", + "@typescript-eslint/parser": "^7.0.0", + "fastify": "^5.0.0", "graphql": "^16.8.0", - "mercurius": "^14.0.0", + "mercurius": "^15.0.0", "split2": "^4.1.0", "standard": "^17.0.0", + "tap": "^21.0.0", "tsd": "^0.31.0" }, "dependencies": { - "fastify-plugin": "^4.2.0" + "fastify-plugin": "^5.0.0" } } diff --git a/test/handle-reply-undefined.test.js b/test/handle-reply-undefined.test.js index 72f79c0..cf5e477 100644 --- a/test/handle-reply-undefined.test.js +++ b/test/handle-reply-undefined.test.js @@ -1,6 +1,6 @@ 'use strict' -const test = require('ava') +const { test } = require('tap') const { buildApp, jsonLogger } = require('./_helper') test('should handle without logging when context.reply is undefined', async (t) => { @@ -24,7 +24,7 @@ test('should handle without logging when context.reply is undefined', async (t) url: '/custom-endpoint' }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { four: 4, six: 6, @@ -39,9 +39,9 @@ test('should log when using graphql mercurius decorator providing reply object i const stream = jsonLogger( line => { - t.is(line.req, undefined) - t.is(line.reqId, 'req-1') - t.deepEqual(line.graphql, { + t.same(line.req, undefined) + t.same(line.reqId, 'req-1') + t.same(line.graphql, { queries: ['add', 'add', 'echo', 'counter'] }) }) @@ -64,7 +64,7 @@ test('should log when using graphql mercurius decorator providing reply object i url: '/custom-endpoint' }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { four: 4, six: 6, diff --git a/test/issue-22.test.js b/test/issue-22.test.js index 5278c1f..f7ba78e 100644 --- a/test/issue-22.test.js +++ b/test/issue-22.test.js @@ -1,6 +1,6 @@ 'use strict' -const test = require('ava') +const { test } = require('tap') const { buildApp, jsonLogger } = require('./_helper') test('should deal GET request', async (t) => { @@ -8,9 +8,9 @@ test('should deal GET request', async (t) => { const stream = jsonLogger( line => { - t.is(line.req, undefined) - t.is(line.reqId, 'req-1') - t.deepEqual(line.graphql, { + t.same(line.req, undefined) + t.same(line.reqId, 'req-1') + t.same(line.graphql, { queries: ['add', 'add', 'echo', 'counter'] }) }) @@ -30,7 +30,7 @@ test('should deal GET request', async (t) => { url: '/graphql', query: { query } }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { four: 4, six: 6, @@ -40,7 +40,7 @@ test('should deal GET request', async (t) => { }) }) -test('should log the whole request when operationName is set', async (t) => { +test('should log the whole request when operationName same set', async (t) => { t.plan(3) const query = ` @@ -56,8 +56,8 @@ test('should log the whole request when operationName is set', async (t) => { const stream = jsonLogger( line => { - t.is(line.reqId, 'req-1') - t.deepEqual(line.graphql, { + t.same(line.reqId, 'req-1') + t.same(line.graphql, { queries: ['add', 'add', 'add', 'add'], operationName: 'baam', body: query, @@ -81,5 +81,5 @@ test('should log the whole request when operationName is set', async (t) => { } }) - t.deepEqual(response.json(), { data: { c: 5, d: 5 } }) + t.same(response.json(), { data: { c: 5, d: 5 } }) }) diff --git a/test/logMessage.test.js b/test/logMessage.test.js index 21edcfc..e6057f5 100644 --- a/test/logMessage.test.js +++ b/test/logMessage.test.js @@ -1,6 +1,6 @@ 'use strict' -const test = require('ava') +const { test } = require('tap') const { buildApp, jsonLogger } = require('./_helper') test('should log without msg when logMessage is undefined', async (t) => { @@ -8,10 +8,10 @@ test('should log without msg when logMessage is undefined', async (t) => { const stream = jsonLogger( line => { - t.is(line.req, undefined) - t.is(line.reqId, 'req-1') - t.deepEqual(line.msg, undefined) - t.deepEqual(line.graphql, { + t.equal(line.req, undefined) + t.equal(line.reqId, 'req-1') + t.same(line.msg, undefined) + t.same(line.graphql, { operationName: 'logMe', queries: ['add', 'add', 'echo', 'counter'] }) @@ -32,7 +32,7 @@ test('should log without msg when logMessage is undefined', async (t) => { url: '/graphql', body: JSON.stringify({ query }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { four: 4, six: 6, @@ -47,10 +47,10 @@ test('should log without msg when logMessage is\'nt a valid function', async (t) const stream = jsonLogger( line => { - t.is(line.req, undefined) - t.is(line.reqId, 'req-1') - t.deepEqual(line.msg, undefined) - t.deepEqual(line.graphql, { + t.equal(line.req, undefined) + t.equal(line.reqId, 'req-1') + t.same(line.msg, undefined) + t.same(line.graphql, { operationName: 'logMe', queries: ['add', 'add', 'echo', 'counter'] }) @@ -71,7 +71,7 @@ test('should log without msg when logMessage is\'nt a valid function', async (t) url: '/graphql', body: JSON.stringify({ query }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { four: 4, six: 6, @@ -88,10 +88,10 @@ test('should log without msg using a logMessage function returning an undefined const stream = jsonLogger( line => { - t.is(line.req, undefined) - t.is(line.reqId, 'req-1') - t.deepEqual(line.msg, undefined) - t.deepEqual(line.graphql, { + t.equal(line.req, undefined) + t.equal(line.reqId, 'req-1') + t.same(line.msg, undefined) + t.same(line.graphql, { operationName: 'logMe', queries: ['add', 'add', 'echo', 'counter'] }) @@ -112,7 +112,7 @@ test('should log without msg using a logMessage function returning an undefined url: '/graphql', body: JSON.stringify({ query }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { four: 4, six: 6, @@ -129,10 +129,10 @@ test('should log without msg using a logMessage function throwing an error', asy const stream = jsonLogger( line => { - t.is(line.req, undefined) - t.is(line.reqId, 'req-1') - t.deepEqual(line.msg, undefined) - t.deepEqual(line.graphql, { + t.equal(line.req, undefined) + t.equal(line.reqId, 'req-1') + t.same(line.msg, undefined) + t.same(line.graphql, { operationName: 'logMe', queries: ['add', 'add', 'echo', 'counter'] }) @@ -153,7 +153,7 @@ test('should log without msg using a logMessage function throwing an error', asy url: '/graphql', body: JSON.stringify({ query }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { four: 4, six: 6, @@ -170,10 +170,10 @@ test('should log with msg using a logMessage function returning a string', async const stream = jsonLogger( line => { - t.is(line.req, undefined) - t.is(line.reqId, 'req-1') - t.deepEqual(line.msg, 'This is a request made with method POST') - t.deepEqual(line.graphql, { + t.equal(line.req, undefined) + t.equal(line.reqId, 'req-1') + t.same(line.msg, 'This is a request made with method POST') + t.same(line.graphql, { operationName: 'logMe', queries: ['add', 'add', 'echo', 'counter'] }) @@ -194,7 +194,7 @@ test('should log with msg using a logMessage function returning a string', async url: '/graphql', body: JSON.stringify({ query }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { four: 4, six: 6, @@ -211,10 +211,10 @@ test('should log with msg using a logMessage function returning an array', async const stream = jsonLogger( line => { - t.is(line.req, undefined) - t.is(line.reqId, 'req-1') - t.deepEqual(line.msg, 'This is a request made with method POST by foobar') - t.deepEqual(line.graphql, { + t.equal(line.req, undefined) + t.equal(line.reqId, 'req-1') + t.same(line.msg, 'This is a request made with method POST by foobar') + t.same(line.graphql, { operationName: 'logMe', queries: ['add', 'add', 'echo', 'counter'] }) @@ -235,7 +235,7 @@ test('should log with msg using a logMessage function returning an array', async url: '/graphql', body: JSON.stringify({ query }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { four: 4, six: 6, diff --git a/test/plugin.test.js b/test/plugin.test.js index 46923c3..46cf0ac 100644 --- a/test/plugin.test.js +++ b/test/plugin.test.js @@ -1,6 +1,6 @@ 'use strict' -const test = require('ava') +const { test } = require('tap') const { buildApp, jsonLogger } = require('./_helper') test('should log every query', async (t) => { @@ -8,9 +8,9 @@ test('should log every query', async (t) => { const stream = jsonLogger( line => { - t.is(line.req, undefined) - t.is(line.reqId, 'req-1') - t.deepEqual(line.graphql, { + t.equal(line.req, undefined) + t.equal(line.reqId, 'req-1') + t.same(line.graphql, { queries: ['add', 'add', 'echo', 'counter'] }) }) @@ -30,7 +30,7 @@ test('should log every query', async (t) => { url: '/graphql', body: JSON.stringify({ query }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { four: 4, six: 6, @@ -44,17 +44,17 @@ test('should log batched queries when logBody is false', async (t) => { t.plan(6) const stream = jsonLogger( (line) => { - t.is(line.reqId, 'req-1') + t.equal(line.reqId, 'req-1') switch (line.graphql?.operationName) { case 'QueryOne': - t.deepEqual(line.graphql, { + t.same(line.graphql, { operationName: 'QueryOne', queries: ['counter'], variables: null }) break case 'QueryTwo': - t.deepEqual(line.graphql, { + t.same(line.graphql, { operationName: 'QueryTwo', queries: ['add'], variables: { y: 2 } @@ -83,7 +83,7 @@ test('should log batched queries when logBody is false', async (t) => { { operationName: 'BadQuery', query: 'query DoubleQuery ($x: Int!) {---' } // Malformed query ]) }) - t.deepEqual(response.json(), [ + t.same(response.json(), [ { data: { counter: 0 @@ -106,14 +106,14 @@ test('should log batched queries when logBody is false', async (t) => { ]) }) -test('should log batched queries when logBody is true', async (t) => { +test('should log batched queries when logBody equal true', async (t) => { t.plan(6) const stream = jsonLogger( (line) => { - t.is(line.reqId, 'req-1') + t.equal(line.reqId, 'req-1') switch (line.graphql?.operationName) { case 'QueryOne': - t.deepEqual(line.graphql, { + t.same(line.graphql, { operationName: 'QueryOne', queries: ['counter'], body: 'query QueryOne {\n counter\n }', @@ -121,7 +121,7 @@ test('should log batched queries when logBody is true', async (t) => { }) break case 'QueryTwo': - t.deepEqual(line.graphql, { + t.same(line.graphql, { operationName: 'QueryTwo', queries: ['add'], body: 'query QueryTwo($y: Int!) {\n four: add(x: 2, y: $y)\n }', @@ -151,7 +151,7 @@ test('should log batched queries when logBody is true', async (t) => { { operationName: 'BadQuery', query: 'query DoubleQuery ($x: Int!) {---' } // Malformed query ]) }) - t.deepEqual(response.json(), [ + t.same(response.json(), [ { data: { counter: 0 @@ -174,21 +174,21 @@ test('should log batched queries when logBody is true', async (t) => { ]) }) -test('should log batched queries when logBody is a custom function', async (t) => { +test('should log batched queries when logBody equal a custom function', async (t) => { t.plan(6) const stream = jsonLogger( (line) => { - t.is(line.reqId, 'req-1') + t.equal(line.reqId, 'req-1') switch (line.graphql?.operationName) { case 'QueryOne': - t.deepEqual(line.graphql, { + t.same(line.graphql, { operationName: 'QueryOne', queries: ['counter'], variables: null }) break case 'QueryTwo': - t.deepEqual(line.graphql, { + t.same(line.graphql, { operationName: 'QueryTwo', queries: ['add'], body: 'query QueryTwo($y: Int!) {\n four: add(x: 2, y: $y)\n }', @@ -224,7 +224,7 @@ test('should log batched queries when logBody is a custom function', async (t) = { operationName: 'BadQuery', query: 'query DoubleQuery ($x: Int!) {---' } // Malformed query ]) }) - t.deepEqual(response.json(), [ + t.same(response.json(), [ { data: { counter: 0 @@ -252,8 +252,8 @@ test('should log prepend the alias', async (t) => { const stream = jsonLogger( line => { - t.is(line.reqId, 'req-1') - t.deepEqual(line.graphql, { + t.equal(line.reqId, 'req-1') + t.same(line.graphql, { queries: ['four:add', 'six:add', 'echo', 'counter'] }) }) @@ -273,7 +273,7 @@ test('should log prepend the alias', async (t) => { url: '/graphql', body: JSON.stringify({ query }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { four: 4, six: 6, @@ -288,8 +288,8 @@ test('should log every mutation', async (t) => { const stream = jsonLogger( line => { - t.is(line.reqId, 'req-1') - t.deepEqual(line.graphql, { + t.equal(line.reqId, 'req-1') + t.same(line.graphql, { mutations: ['plusOne', 'minusOne', 'plusOne'] }) }) @@ -308,7 +308,7 @@ test('should log every mutation', async (t) => { body: JSON.stringify({ query }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { plusOne: 1, minusOne: 0, @@ -322,9 +322,9 @@ test('should log at debug level', async (t) => { const stream = jsonLogger( line => { - t.is(line.level, 20) - t.is(line.reqId, 'req-1') - t.deepEqual(line.graphql, { + t.equal(line.level, 20) + t.equal(line.reqId, 'req-1') + t.same(line.graphql, { mutations: ['plusOne'] }) }) @@ -338,7 +338,7 @@ test('should log at debug level', async (t) => { body: JSON.stringify({ query }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { plusOne: 2 } @@ -356,8 +356,8 @@ test('should log the request body', async (t) => { const stream = jsonLogger( line => { - t.is(line.reqId, 'req-1') - t.deepEqual(line.graphql, { + t.equal(line.reqId, 'req-1') + t.same(line.graphql, { queries: ['add', 'add', 'echo'], body: query }) @@ -371,7 +371,7 @@ test('should log the request body', async (t) => { url: '/graphql', body: JSON.stringify({ query }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { four: 4, six: 6, echo: 'hellohello' } }) }) @@ -387,20 +387,20 @@ test('should log the request body based on the function', async (t) => { line => { switch (line.reqId) { case 'req-1': - t.deepEqual(line.graphql, { + t.same(line.graphql, { operationName: 'logMe', queries: ['echo'] }) break case 'req-2': - t.deepEqual(line.graphql, { + t.same(line.graphql, { operationName: 'logMe', queries: ['echo'], body: query }) break case 'req-3': - t.deepEqual(line.graphql, { + t.same(line.graphql, { operationName: 'logMe', queries: ['echo'] }) @@ -429,7 +429,7 @@ test('should log the request body based on the function', async (t) => { url: '/graphql', body: JSON.stringify({ query, variables: { txt: 'false' } }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { echo: 'falsefalse' } }) } @@ -441,7 +441,7 @@ test('should log the request body based on the function', async (t) => { url: '/graphql', body: JSON.stringify({ query, variables: { txt: 'true' } }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { echo: 'truetrue' } }) } @@ -453,7 +453,7 @@ test('should log the request body based on the function', async (t) => { url: '/graphql', body: JSON.stringify({ query, variables: { txt: 'err' } }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { echo: 'errerr' } }) } @@ -470,8 +470,8 @@ test('should log the request variables', async (t) => { const stream = jsonLogger( line => { - t.is(line.reqId, 'req-1') - t.deepEqual(line.graphql, { + t.equal(line.reqId, 'req-1') + t.same(line.graphql, { operationName: 'boom', queries: ['add', 'add', 'echo'], variables: { num: 2 } @@ -487,12 +487,12 @@ test('should log the request variables', async (t) => { body: JSON.stringify({ query, variables: { num: 2 } }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { a: 4, b: 4, echo: 'hellohello' } }) }) -test('should log the request variables as null when missing', async (t) => { +test('should log the request variables as null when mequalsing', async (t) => { t.plan(3) const query = `query { @@ -502,8 +502,8 @@ test('should log the request variables as null when missing', async (t) => { const stream = jsonLogger( line => { - t.is(line.reqId, 'req-1') - t.deepEqual(line.graphql, { + t.equal(line.reqId, 'req-1') + t.same(line.graphql, { queries: ['add', 'echo'], variables: null }) @@ -518,12 +518,12 @@ test('should log the request variables as null when missing', async (t) => { body: JSON.stringify({ query }) }) - t.deepEqual(response.json(), { + t.same(response.json(), { data: { add: 4, echo: 'hellohello' } }) }) -test('should log the whole request when operationName is set', async (t) => { +test('should log the whole request when operationName equal set', async (t) => { t.plan(3) const query = ` @@ -539,8 +539,8 @@ test('should log the whole request when operationName is set', async (t) => { const stream = jsonLogger( line => { - t.is(line.reqId, 'req-1') - t.deepEqual(line.graphql, { + t.equal(line.reqId, 'req-1') + t.same(line.graphql, { queries: ['add', 'add', 'add', 'add'], operationName: 'baam', body: query, @@ -564,10 +564,10 @@ test('should log the whole request when operationName is set', async (t) => { }) }) - t.deepEqual(response.json(), { data: { c: 5, d: 5 } }) + t.same(response.json(), { data: { c: 5, d: 5 } }) }) -test('should log the request object when logRequest is true', async (t) => { +test('should log the request object when logRequest equal true', async (t) => { t.plan(2) const query = ` @@ -583,11 +583,11 @@ test('should log the request object when logRequest is true', async (t) => { const stream = jsonLogger( line => { - t.is(line.reqId, 'req-1') - t.deepEqual(line.req, { + t.equal(line.reqId, 'req-1') + t.same(line.req, { method: 'POST', url: '/graphql', - hostname: 'localhost:80', + host: 'localhost:80', remoteAddress: '127.0.0.1' }) }) @@ -609,7 +609,7 @@ test('should log the request object when logRequest is true', async (t) => { }) }) -test('user can customize the log the request object when logRequest is true', async (t) => { +test('user can customize the log the request object when logRequest equal true', async (t) => { t.plan(3) const query = ` @@ -621,8 +621,8 @@ test('user can customize the log the request object when logRequest is true', as const stream = jsonLogger( line => { - t.is(line.reqId, 'req-1') - t.deepEqual(line.req, { + t.equal(line.reqId, 'req-1') + t.same(line.req, { headers: { 'content-length': '131', 'content-type': 'application/json',