From c9f603e4446c9c79b4b63b1723e439abe5b69c4f Mon Sep 17 00:00:00 2001 From: Fernando Rodrigues Date: Sun, 19 Nov 2023 03:39:46 -0300 Subject: [PATCH] feat: adding validation to verify if baseUrl is already present in the GRAPHQL_ENDPOINT variable to prevent GrahphIql's request get error 404 Not Found --- package.json | 2 +- static/main.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 41aa151b..281ab66f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mercurius", - "version": "13.3.0", + "version": "13.3.1", "description": "Fastify GraphQL adapter with subscription support", "main": "index.js", "types": "index.d.ts", diff --git a/static/main.js b/static/main.js index ce3d4257..cdf443b4 100644 --- a/static/main.js +++ b/static/main.js @@ -95,6 +95,17 @@ function fetcherWrapper (fetcher, cbs = []) { } } +/** + * Verify if the baseUrl is already present in the first part of GRAPHQL_ENDPOINT url + * to avoid unexpected duplication of paths + * @param {string} baseUrl [comes from {@link render} function] + * @returns boolean + */ +function isDuplicatedUrlArg (baseUrl) { + const checker = window.GRAPHQL_ENDPOINT.split('/') + return (checker[1] === baseUrl) +} + function render () { const host = window.location.host const websocketProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:' @@ -109,6 +120,10 @@ function render () { if (baseUrl !== 'graphiql') { url = `${window.location.protocol}//${host}/${baseUrl}${window.GRAPHQL_ENDPOINT}` subscriptionUrl = `${websocketProtocol}//${host}/${baseUrl}${window.GRAPHQL_ENDPOINT}` + if (isDuplicatedUrlArg(baseUrl)) { + url = `${window.location.protocol}//${host}${window.GRAPHQL_ENDPOINT}` + subscriptionUrl = `${websocketProtocol}//${host}${window.GRAPHQL_ENDPOINT}` + } } else { url = `${window.location.protocol}//${host}${window.GRAPHQL_ENDPOINT}` subscriptionUrl = `${websocketProtocol}//${host}${window.GRAPHQL_ENDPOINT}`