Repository: mcollina/fastify-gql
Pull request #102 (2020-01-09 15:09:50)

Refactoring: improve ide usage clarity
I wanted to improve the clarity on the ide feature.

dougg0k: We could also have graphiql as the default option, without having to set anything in the ide.

Patch:
diff --git a/.gitignore b/.gitignore
index 9b00a76..f519a3b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -63,3 +63,6 @@ typings/
 # clinic
 *clinic*
 package-lock.json
+
+# vscode
+.vscode/
diff --git a/README.md b/README.md
index fb25b86..4e6d9f3 100644
--- a/README.md
+++ b/README.md
@@ -307,7 +307,7 @@ __fastify-gql__ supports the following options:
 * `graphiql`: boolean | string. Serve
   [GraphiQL](https://www.npmjs.com/package/graphiql) on `/graphiql` if `true` or `'graphiql'`, or
   [GraphQL IDE](https://www.npmjs.com/package/graphql-playground-react) on `/playground` if `'playground'`
-  if `routes` is `true`.
+  and if `routes` is `true`. Leave empty or `false` to disable.
 * `jit`: Integer. The minimum number of execution a query needs to be
   executed before being jit'ed.
 * `routes`: boolean. Serves the Default: `true`. A graphql endpoint is
diff --git a/index.d.ts b/index.d.ts
index 5867a27..c3db60d 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -74,9 +74,10 @@ declare namespace FastifyGQL {
       }
     },
     /**
-     * Serve GraphiQL on /graphiql if true or 'graphiql', or GraphQL IDE on /playground if 'playground' if routes is true
+     * Serve GraphiQL on /graphiql if true or 'graphiql', or GraphQL IDE on /playground if 'playground' and if routes is true
      */
-    graphiql?: boolean | string,
+		graphiql?: boolean | string,
+		ide?: boolean | string,
     /**
      * The minimum number of execution a query needs to be executed before being jit'ed.
      * @default true
diff --git a/index.js b/index.js
index e6026c9..8108d6f 100644
--- a/index.js
+++ b/index.js
@@ -86,9 +86,10 @@ module.exports = fp(async function (app, opts) {
   const graphqlCtx = Symbol('ctx')
 
   if (opts.routes !== false) {
+    const optsIde = opts.graphiql || opts.ide
     app.register(routes, {
       errorHandler: opts.errorHandler,
-      graphiql: opts.graphiql,
+      ide: optsIde,
       prefix: opts.prefix,
       context: opts.context,
       schema,
diff --git a/lib/routes.js b/lib/routes.js
index 07858ed..738bfb9 100644
--- a/lib/routes.js
+++ b/lib/routes.js
@@ -187,17 +187,17 @@ module.exports = async function (app, opts) {
     return reply.graphql(query, { pubsub: subscriber, ...context }, variables, operationName)
   })
 
-  if (opts.graphiql) {
+  if (opts.ide) {
     app.register(Static, {
       root: join(__dirname, '../static'),
       wildcard: false
     })
-    if (opts.graphiql === true || opts.graphiql === 'graphiql') {
+    if (opts.ide === true || opts.ide === 'graphiql') {
       app.get('/graphiql', (req, reply) => {
         reply.redirect(`${app.prefix}/graphiql.html`)
       })
     }
-    if (opts.graphiql === 'playground') {
+    if (opts.ide === 'playground') {
       app.get('/playground', (req, reply) => {
         reply.redirect(`${app.prefix}/playground.html`)
       })


Test patch:
diff --git a/test/routes.js b/test/routes.js
index e735a15..1bf7ea9 100644
--- a/test/routes.js
+++ b/test/routes.js
@@ -560,7 +560,7 @@ test('error if there are functions defined in the root object', async (t) => {
 test('GET graphiql endpoint', async (t) => {
   const app = Fastify()
   app.register(GQL, {
-    graphiql: true
+    ide: 'graphiql'
   })
 
   const res = await app.inject({
@@ -571,10 +571,86 @@ test('GET graphiql endpoint', async (t) => {
   t.strictEqual(res.headers.location, '/graphiql.html')
 })
 
+test('GET graphiql endpoint with boolean', async (t) => {
+  const app = Fastify()
+  app.register(GQL, {
+    ide: true
+  })
+
+  const res = await app.inject({
+    method: 'GET',
+    url: '/graphiql'
+  })
+  t.strictEqual(res.statusCode, 302)
+  t.strictEqual(res.headers.location, '/graphiql.html')
+  const res2 = await app.inject({
+    method: 'GET',
+    url: '/playground'
+  })
+  t.strictEqual(res2.statusCode, 404)
+})
+
+test('GET graphiql endpoint with property priority', async (t) => {
+  const app = Fastify()
+  app.register(GQL, {
+    ide: 'graphiql',
+    graphiql: 'playground',
+    routes: true
+  })
+
+  const res = await app.inject({
+    method: 'GET',
+    url: '/playground'
+  })
+  t.strictEqual(res.statusCode, 302)
+  t.strictEqual(res.headers.location, '/playground.html')
+
+  const res2 = await app.inject({
+    method: 'GET',
+    url: '/graphiql'
+  })
+  t.strictEqual(res2.statusCode, 404)
+  t.notStrictEqual(res2.headers.location, '/graphiql.html')
+})
+
+test('Disable ide endpoint', async (t) => {
+  const app = Fastify()
+  app.register(GQL, {
+    ide: false
+  })
+
+  const res = await app.inject({
+    method: 'GET',
+    url: '/graphiql'
+  })
+  const res2 = await app.inject({
+    method: 'GET',
+    url: '/playground'
+  })
+  t.strictEqual(res.statusCode, 404)
+  t.strictEqual(res2.statusCode, 404)
+})
+
+test('Disable ide endpoint by leaving empty', async (t) => {
+  const app = Fastify()
+  app.register(GQL, {})
+
+  const res = await app.inject({
+    method: 'GET',
+    url: '/graphiql'
+  })
+  const res2 = await app.inject({
+    method: 'GET',
+    url: '/playground'
+  })
+  t.strictEqual(res.statusCode, 404)
+  t.strictEqual(res2.statusCode, 404)
+})
+
 test('GET graphiql endpoint with prefix', async (t) => {
   const app = Fastify()
   app.register(GQL, {
-    graphiql: 'graphiql',
+    ide: 'graphiql',
     prefix: '/test-prefix'
   })
 
@@ -591,7 +667,7 @@ test('GET graphiql endpoint with prefixed wrapper', async (t) => {
   const app = Fastify()
   app.register(async function (app, opts) {
     app.register(GQL, {
-      graphiql: true
+      ide: 'graphiql'
     })
   }, { prefix: '/test-wrapper-prefix' })
 
@@ -607,7 +683,7 @@ test('GET graphiql endpoint with prefixed wrapper', async (t) => {
 test('GET graphql playground endpoint', async (t) => {
   const app = Fastify()
   app.register(GQL, {
-    graphiql: 'playground'
+    ide: 'playground'
   })
 
   const res = await app.inject({
@@ -621,7 +697,7 @@ test('GET graphql playground endpoint', async (t) => {
 test('GET graphql playground endpoint with prefix', async (t) => {
   const app = Fastify()
   app.register(GQL, {
-    graphiql: 'playground',
+    ide: 'playground',
     prefix: '/test-prefix'
   })
 
@@ -638,7 +714,7 @@ test('GET graphql playground endpoint with prefixed wrapper', async (t) => {
   const app = Fastify()
   app.register(async function (app, opts) {
     app.register(GQL, {
-      graphiql: 'playground'
+      ide: 'playground'
     })
   }, { prefix: '/test-wrapper-prefix' })
 
diff --git a/test/types/index.ts b/test/types/index.ts
index ef855b6..76f72e2 100644
--- a/test/types/index.ts
+++ b/test/types/index.ts
@@ -41,7 +41,7 @@ app.register(GQL, {
   schema: schema,
   resolvers,
   loaders: {},
-  graphiqls: true,
+  ide: false,
   jit: 1,
   routes: true,
   prefix: '/prefix',