Repository: mercurius-js/mercurius (js)

Issue:
Disabling cache breaks replaceSchema
Passing `cache: false` as Plugin option makes `buildCache` return `null`, so LRUs instances are null.

But the `replaceSchema` method does not check if LRUs are null and throws
```
TypeError: Cannot read property 'clear' of null
```

It's necessary to wrap each LRU call with an if :
```
if (lru) {
  lru.clear()
}
```

Pull request description:
fix: disabling cache breaks replaceSchema
Fixes #398 

Patch:
diff --git a/index.js b/index.js
index b88d12b..9ca7bb1 100644
--- a/index.js
+++ b/index.js
@@ -256,9 +256,15 @@ const plugin = fp(async function (app, opts) {
 
     fastifyGraphQl.schema = s
 
-    lru.clear()
-    lruErrors.clear()
-    lruGatewayResolvers.clear()
+    if (lru) {
+      lru.clear()
+    }
+    if (lruErrors) {
+      lruErrors.clear()
+    }
+    if (lruGatewayResolvers) {
+      lruGatewayResolvers.clear()
+    }
   }
 
   fastifyGraphQl.extendSchema = function (s) {


Test patch:
diff --git a/test/app-decorator.js b/test/app-decorator.js
index f537ee6..7745e63 100644
--- a/test/app-decorator.js
+++ b/test/app-decorator.js
@@ -253,6 +253,64 @@ test('replaceSchema (clearing cache)', async (t) => {
   }
 })
 
+test('replaceSchema (without cache)', async (t) => {
+  const app = Fastify()
+
+  app.register(GQL, {
+    cache: false,
+    schema: makeExecutableSchema({
+      typeDefs: `
+      type Query {
+        add(x: Int, y: Int): Int
+        subtract(x: Int, y: Int): Int
+      }
+    `,
+      resolvers: {
+        Query: {
+          add: async (_, { x, y }) => x + y,
+          subtract: async (_, { x, y }) => x - y
+        }
+      }
+    })
+  })
+
+  // needed so that graphql is defined
+  await app.ready()
+
+  let query
+
+  query = '{ subtract(x: 4, y: 2) }'
+  const res = await app.graphql(query)
+
+  t.deepEqual(res, {
+    data: {
+      subtract: 2
+    }
+  })
+
+  app.graphql.replaceSchema(
+    makeExecutableSchema({
+      typeDefs: `
+      type Query {
+        add(x: Int, y: Int): Int
+      }
+    `,
+      resolvers: {
+        Query: {
+          add: async (_, { x, y }) => x + y
+        }
+      }
+    })
+  )
+
+  query = '{ subtract(x: 4, y: 2) }'
+  try {
+    await app.graphql(query)
+  } catch (err) {
+    t.equal(err.errors[0].message, 'Cannot query field "subtract" on type "Query".')
+  }
+})
+
 test('replaceSchema with makeSchemaExecutable (schema should be provided)', async (t) => {
   const app = Fastify()