Repository: mcollina/fastify-gql
Pull request #22 (2018-12-23 03:06:01)

fix(routes): graphiql redirection if a prefix have been set
The redirection is incorrect if the plugin is registered with a prefix. 
This PR fixes the issue ;)


Patch:
diff --git a/README.md b/README.md
index 20b6cb2..1766bbb 100644
--- a/README.md
+++ b/README.md
@@ -103,6 +103,7 @@ __fastify-gql__ supports the following options:
   executed before being jit'ed.
 * `routes`: boolean. Serves the Default: `true`. A graphql endpoint is
   exposed at `/graphql`.
+* `prefix`: String. Change the route prefix of the graphql endpoint if enabled.
 
 ### HTTP endpoints
 
diff --git a/index.js b/index.js
index 5143286..96f3005 100644
--- a/index.js
+++ b/index.js
@@ -75,7 +75,8 @@ module.exports = fp(async function (app, opts) {
 
   if (opts.routes !== false) {
     app.register(routes, {
-      graphiql: opts.graphiql
+      graphiql: opts.graphiql,
+      prefix: opts.prefix
     })
   }
 
diff --git a/routes.js b/routes.js
index 04a2d2b..14a37db 100644
--- a/routes.js
+++ b/routes.js
@@ -107,7 +107,7 @@ module.exports = async function (app, opts) {
     })
 
     app.get('/graphiql', (req, reply) => {
-      reply.redirect('/graphiql.html')
+      reply.redirect(`${app.basePath}/graphiql.html`)
     })
   }
 }


Test patch:
diff --git a/test/routes.js b/test/routes.js
index ff6c3f5..8d49b61 100644
--- a/test/routes.js
+++ b/test/routes.js
@@ -555,3 +555,103 @@ test('error if there are functions defined in the root object', async (t) => {
     t.is(err.message, 'jit is not possible if there are root functions')
   }
 })
+
+test('GET graphiql endpoint', async (t) => {
+  const app = Fastify()
+  app.register(GQL, {
+    graphiql: true
+  })
+
+  const res = await app.inject({
+    method: 'GET',
+    url: '/graphiql'
+  })
+  t.strictEqual(res.statusCode, 302)
+  t.strictEqual(res.headers.location, '/graphiql.html')
+})
+
+test('GET graphiql endpoint with prefix', async (t) => {
+  const app = Fastify()
+  app.register(GQL, {
+    graphiql: true,
+    prefix: '/test-prefix'
+  })
+
+  const res = await app.inject({
+    method: 'GET',
+    url: '/test-prefix/graphiql'
+  })
+
+  t.strictEqual(res.statusCode, 302)
+  t.strictEqual(res.headers.location, '/test-prefix/graphiql.html')
+})
+
+test('GET graphiql endpoint with prefixed wrapper', async (t) => {
+  const app = Fastify()
+  app.register(async function (app, opts) {
+    app.register(GQL, {
+      graphiql: true
+    })
+  }, { prefix: '/test-wrapper-prefix' })
+
+  const res = await app.inject({
+    method: 'GET',
+    url: '/test-wrapper-prefix/graphiql'
+  })
+
+  t.strictEqual(res.statusCode, 302)
+  t.strictEqual(res.headers.location, '/test-wrapper-prefix/graphiql.html')
+})
+
+test('GET graphql endpoint with prefix', async (t) => {
+  const app = Fastify()
+  const schema = `
+    type Query {
+      add(x: Int, y: Int): Int
+    }
+  `
+  const resolvers = {
+    add: async ({ x, y }) => x + y
+  }
+
+  app.register(GQL, {
+    schema,
+    resolvers,
+    prefix: '/test-prefix'
+  })
+
+  const res = await app.inject({
+    method: 'GET',
+    url: '/test-prefix/graphql?query={add(x:2,y:2)}'
+  })
+
+  t.strictEqual(res.statusCode, 200)
+})
+
+test('GET graphql endpoint with prefixed wrapper', async (t) => {
+  const app = Fastify()
+
+  app.register(async function (app, opts) {
+    const schema = `
+    type Query {
+      add(x: Int, y: Int): Int
+    }
+    `
+
+    const resolvers = {
+      add: async ({ x, y }) => x + y
+    }
+
+    app.register(GQL, {
+      schema,
+      resolvers
+    })
+  }, { prefix: '/test-wrapper-prefix' })
+
+  const res = await app.inject({
+    method: 'GET',
+    url: '/test-wrapper-prefix/graphql?query={add(x:2,y:2)}'
+  })
+
+  t.strictEqual(res.statusCode, 200)
+})