Repository: mercurius-js/mercurius
Pull request #840 (2022-08-15 16:16:25)

build(deps): bump fastify-plugin from 3.0.1 to 4.2.0
Bumps [fastify-plugin](https://github.com/fastify/fastify-plugin) from 3.0.1 to 4.2.0.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a href="https://github.com/fastify/fastify-plugin/releases">fastify-plugin's releases</a>.</em></p>
<blockquote>
<h2>v4.2.0</h2>
<p>📚 PR:</p>
<ul>
<li>feat: Add Generics RawServer and TypeProvider to fp and tests. (<a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/issues/192">#192</a>)</li>
</ul>
<h2>v4.1.0</h2>
<p>📚 PR:</p>
<ul>
<li>ci: enable license checking (<a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/issues/188">#188</a>)</li>
<li>feat: handle <code>@​fastify/x</code> names to export (<a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/issues/190">#190</a>)</li>
</ul>
<h2>v4.0.0</h2>
<h2>What's Changed</h2>
<ul>
<li>build: reduce dependabot update frequency by <a href="https://github.com/Fdawgs"><code>@​Fdawgs</code></a> in <a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/pull/171">fastify/fastify-plugin#171</a></li>
<li>build(deps): bump actions/setup-node from 2 to 3 by <a href="https://github.com/dependabot"><code>@​dependabot</code></a> in <a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/pull/172">fastify/fastify-plugin#172</a></li>
<li>build(deps-dev): bump tap from 15.2.3 to 16.0.1 by <a href="https://github.com/dependabot"><code>@​dependabot</code></a> in <a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/pull/174">fastify/fastify-plugin#174</a></li>
<li>build(deps): bump actions/checkout from 2 to 3 by <a href="https://github.com/dependabot"><code>@​dependabot</code></a> in <a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/pull/175">fastify/fastify-plugin#175</a></li>
<li>build(deps-dev): bump tsd from 0.19.1 to 0.20.0 by <a href="https://github.com/dependabot"><code>@​dependabot</code></a> in <a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/pull/176">fastify/fastify-plugin#176</a></li>
<li>build(deps-dev): bump standard from 16.0.4 to 17.0.0 by <a href="https://github.com/dependabot"><code>@​dependabot</code></a> in <a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/pull/177">fastify/fastify-plugin#177</a></li>
<li>ci: migrate to fastify reusable workflow by <a href="https://github.com/Fdawgs"><code>@​Fdawgs</code></a> in <a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/pull/180">fastify/fastify-plugin#180</a></li>
<li>chore(.gitignore): use updated skeleton template by <a href="https://github.com/Fdawgs"><code>@​Fdawgs</code></a> in <a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/pull/181">fastify/fastify-plugin#181</a></li>
<li>build(deps-dev): bump fastify from 3.29.0 to 4.0.1 by <a href="https://github.com/dependabot"><code>@​dependabot</code></a> in <a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/pull/182">fastify/fastify-plugin#182</a></li>
<li>build(deps-dev): bump <code>@​types/node</code> from 17.0.45 to 18.0.0 by <a href="https://github.com/dependabot"><code>@​dependabot</code></a> in <a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/pull/184">fastify/fastify-plugin#184</a></li>
<li>build(deps-dev): bump tsd from 0.20.0 to 0.21.0 by <a href="https://github.com/dependabot"><code>@​dependabot</code></a> in <a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/pull/185">fastify/fastify-plugin#185</a></li>
<li>build(deps-dev): bump tsd from 0.21.0 to 0.22.0 by <a href="https://github.com/dependabot"><code>@​dependabot</code></a> in <a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/pull/186">fastify/fastify-plugin#186</a></li>
</ul>
<p><strong>Full Changelog</strong>: <a href="https://github.com/fastify/fastify-plugin/compare/v3.0.1...v4.0.0">https://github.com/fastify/fastify-plugin/compare/v3.0.1...v4.0.0</a></p>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/fastify/fastify-plugin/commit/f197c55a4354276c5b194d3155d06de14b4705f3"><code>f197c55</code></a> Bumped v4.2.0</li>
<li><a href="https://github.com/fastify/fastify-plugin/commit/b532e76b4e63e6d05bc70cc016de1486bd6bd7e6"><code>b532e76</code></a> feat: Add Generics RawServer and TypeProvider to fp and tests. (<a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/issues/192">#192</a>)</li>
<li><a href="https://github.com/fastify/fastify-plugin/commit/3a04d1a868c1030cfe8b32e72292a12271672b08"><code>3a04d1a</code></a> Bumped v4.1.0</li>
<li><a href="https://github.com/fastify/fastify-plugin/commit/36e927114027141ff670e2eaed2c12932c34a503"><code>36e9271</code></a> feat: handle <code>@​fastify/x</code> names to export (<a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/issues/190">#190</a>)</li>
<li><a href="https://github.com/fastify/fastify-plugin/commit/e692cc6e9d110ef95248e5d8666a6f345f32e88d"><code>e692cc6</code></a> ci: enable license checking (<a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/issues/188">#188</a>)</li>
<li><a href="https://github.com/fastify/fastify-plugin/commit/4b875641416066154ec27793a33ff315c7fd627c"><code>4b87564</code></a> Bumped v4.0.0</li>
<li><a href="https://github.com/fastify/fastify-plugin/commit/61be20de511af93bbbdac7d52f592ef1ef726a28"><code>61be20d</code></a> build(deps-dev): bump tsd from 0.21.0 to 0.22.0 (<a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/issues/186">#186</a>)</li>
<li><a href="https://github.com/fastify/fastify-plugin/commit/31db33fe58a791546dd16cb0fb0b449a8b966f80"><code>31db33f</code></a> build(deps-dev): bump tsd from 0.20.0 to 0.21.0 (<a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/issues/185">#185</a>)</li>
<li><a href="https://github.com/fastify/fastify-plugin/commit/64a57b618d12861719fd5e8720bd88beb4257231"><code>64a57b6</code></a> build(deps-dev): bump <code>@​types/node</code> from 17.0.45 to 18.0.0 (<a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/issues/184">#184</a>)</li>
<li><a href="https://github.com/fastify/fastify-plugin/commit/711d006bf2bcbe74e8ffec6bac041847c9c8765c"><code>711d006</code></a> build(deps-dev): bump fastify from 3.29.0 to 4.0.1 (<a href="https://github-redirect.dependabot.com/fastify/fastify-plugin/issues/182">#182</a>)</li>
<li>Additional commits viewable in <a href="https://github.com/fastify/fastify-plugin/compare/v3.0.1...v4.2.0">compare view</a></li>
</ul>
</details>
<details>
<summary>Maintainer changes</summary>
<p>This version was pushed to npm by <a href="https://www.npmjs.com/~eomm">eomm</a>, a new releaser for fastify-plugin since your current version.</p>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=fastify-plugin&package-manager=npm_and_yarn&previous-version=3.0.1&new-version=4.2.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>

mcollina: @dependabot rebase

Patch:
diff --git a/docs/plugins.md b/docs/plugins.md
index d429942..9e4cfa2 100644
--- a/docs/plugins.md
+++ b/docs/plugins.md
@@ -10,6 +10,7 @@ Related plugins for mercurius
 - [mercurius-apollo-registry](#mercurius-apollo-registry)
 - [mercurius-apollo-tracing](#mercurius-apollo-tracing)
 - [mercurius-postgraphile](#mercurius-postgraphile)
+- [mercurius-logging](#mercurius-logging)
 
 ## mercurius-auth
 
@@ -124,3 +125,23 @@ app.register(mercuriusTracing, {
 A Mercurius plugin for integrating PostGraphile schemas with Mercurius
 
 Check [https://github.com/autotelic/mercurius-postgraphile](https://github.com/autotelic/mercurius-postgraphile) for usage and readme.
+
+## mercurius-logging
+A Mercurius plugin to enhance the GQL request logging adding useful insights:
+
+```json
+{
+  "level": 30,
+  "time": 1660395516406,
+  "hostname": "eomm",
+  "reqId": "req-1",
+  "graphql": {
+    "queries": [
+      "firstQuery:myTeam",
+      "secondQuery:myTeam"
+    ]
+  }
+}
+```
+
+Check the [`mercurius-logging`](https://github.com/Eomm/mercurius-logging) documentation for usage and settings.
diff --git a/lib/subscription-connection.js b/lib/subscription-connection.js
index d088e66..f08b290 100644
--- a/lib/subscription-connection.js
+++ b/lib/subscription-connection.js
@@ -271,6 +271,7 @@ module.exports = class SubscriptionConnection {
     }
 
     this.sendMessage(this.protocolMessageTypes.GQL_COMPLETE, id, null)
+    this.handleGQLComplete(id)
   }
 
   async _executeQueryOrMutation ({ query, context, variables, operationName, id }) {
@@ -284,6 +285,19 @@ module.exports = class SubscriptionConnection {
     this.sendMessage(this.protocolMessageTypes.GQL_COMPLETE, id, null)
   }
 
+  handleGQLComplete (id) {
+    const sc = this.subscriptionContexts.get(id)
+    if (sc) {
+      sc.close && sc.close()
+      this.subscriptionContexts.delete(id)
+    }
+    const subIter = this.subscriptionIters.get(id)
+    if (subIter) {
+      subIter.return && subIter.return()
+      this.subscriptionIters.delete(id)
+    }
+  }
+
   async handleGQLStop (data) {
     if (this.context.onSubscriptionEnd) {
       try {
@@ -293,16 +307,8 @@ module.exports = class SubscriptionConnection {
         return this.handleConnectionClose()
       }
     }
-    const sc = this.subscriptionContexts.get(data.id)
-    if (sc) {
-      sc.close && sc.close()
-      this.subscriptionContexts.delete(data.id)
-    }
-    const subIter = this.subscriptionIters.get(data.id)
-    if (subIter) {
-      subIter.return && subIter.return()
-      this.subscriptionIters.delete(data.id)
-    }
+
+    this.handleGQLComplete(data.id)
   }
 
   handleConnectionClose () {
diff --git a/package.json b/package.json
index 240aba6..544ba07 100644
--- a/package.json
+++ b/package.json
@@ -58,7 +58,7 @@
     "@fastify/static": "^6.0.0",
     "@fastify/websocket": "^7.0.0",
     "events.on": "^1.0.1",
-    "fastify-plugin": "^3.0.1",
+    "fastify-plugin": "^4.2.0",
     "graphql": "^16.0.0",
     "graphql-jit": "^0.7.3",
     "mqemitter": "^5.0.0",


Test patch:
diff --git a/test/subscription-connection.js b/test/subscription-connection.js
index fa98e9b..323543d 100644
--- a/test/subscription-connection.js
+++ b/test/subscription-connection.js
@@ -5,6 +5,7 @@ const sinon = require('sinon')
 const WebSocket = require('ws')
 const fastify = require('fastify')
 const mq = require('mqemitter')
+const { makeExecutableSchema } = require('@graphql-tools/schema')
 const SubscriptionConnection = require('../lib/subscription-connection')
 const { PubSub } = require('../lib/subscriber')
 const { GRAPHQL_WS, GRAPHQL_TRANSPORT_WS } = require('../lib/subscription-protocol')
@@ -643,3 +644,60 @@ test('subscription connection handles mutation when fullWsTransport: true', asyn
     ).calledOnce
   )
 })
+
+test('subscription data is released right after it ends', async (t) => {
+  const sc = new SubscriptionConnection({
+    on () {},
+    close () {},
+    send () {},
+    protocol: GRAPHQL_TRANSPORT_WS
+  }, {
+    context: { preSubscriptionParsing: null, preSubscriptionExecution: null },
+    fastify: {
+      graphql: {
+        schema: makeExecutableSchema({
+          typeDefs: ['type Query { blah: String! }', 'type Subscription { onMessage: String! }'],
+          resolvers: {
+            Query: {},
+            Subscription: {
+              onMessage: {
+                async * subscribe () {
+                  return 'blah'
+                }
+              }
+            }
+          }
+        })
+      }
+    }
+  })
+
+  sc.isReady = true
+
+  t.equal(sc.subscriptionIters.size, 0)
+  t.equal(sc.subscriptionContexts.size, 0)
+
+  await sc.handleMessage(JSON.stringify({
+    id: 1,
+    type: 'subscribe',
+    payload: {
+      query: 'subscription { onMessage } '
+    }
+  }))
+
+  await new Promise(resolve => {
+    sc.sendMessage = (type, id, payload) => {
+      t.equal(id, 1)
+      t.equal(type, 'complete')
+      t.equal(payload, null)
+
+      t.equal(sc.subscriptionIters.size, 1)
+      t.equal(sc.subscriptionContexts.size, 1)
+
+      resolve()
+    }
+  })
+
+  t.equal(sc.subscriptionIters.size, 0)
+  t.equal(sc.subscriptionContexts.size, 0)
+})