From 1ad951ca60d7dfc764938b647a423729a581492f Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Tue, 15 Jan 2019 14:27:15 +0100 Subject: [PATCH] Add graph-handling for different environments --- package.json | 1 + utils/graph-client/utils.js | 30 ++++++++++++++++++++++++++++-- yarn.lock | 13 +++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index b19be76..7de1b1a 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "lodash": "^4.17.10", "node-sass": "^4.9.0", "nuxt": "^2.0.0", + "query-string": "^6.2.0", "s-ago": "^1.3.0", "sass-loader": "^7.0.3", "vue": "^2.5.22", diff --git a/utils/graph-client/utils.js b/utils/graph-client/utils.js index 908d5c8..eb77f54 100644 --- a/utils/graph-client/utils.js +++ b/utils/graph-client/utils.js @@ -1,10 +1,36 @@ +const queryString = require('query-string'); const { createApolloFetch } = require('apollo-fetch'); const { includeCredentials } = require('./middleware'); -const defaultGraphUri = '/graph'; +const defaultGraphUri = 'https://dancefinder.unbound.se/graph'; + +const getGraphURI = () => { + let graphUri = defaultGraphUri; + + try { + const loc = location; // eslint-disable-line + const search = (loc && loc.search) || ''; + const host = (loc && loc.host) || ''; + const query = queryString.parse(search); + const isLocal = host.includes('localhost'); + + if (query.graph) { + // if a query param is provided - always use that. + graphUri = query.graph; + } else if (isLocal) { + // If a query param is not provided - check if we're running locally or on heroku, + // and if so default to /graphql for mocking purposes + graphUri = '/graph'; + } + } catch (err) { + // just suppress this madness + } + + return graphUri; +}; export const createQuery = (tokenFn, query, variables) => { // eslint-disable-line - const apollo = createApolloFetch({ uri: defaultGraphUri }); + const apollo = createApolloFetch({ uri: getGraphURI() }); apollo.use(includeCredentials(tokenFn)); // apollo.useAfter(trackErrors); diff --git a/yarn.lock b/yarn.lock index 69a859f..eecb6a7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6708,6 +6708,14 @@ qs@^6.4.0, qs@^6.5.1: resolved "https://registry.yarnpkg.com/qs/-/qs-6.6.0.tgz#a99c0f69a8d26bf7ef012f871cdabb0aee4424c2" integrity sha512-KIJqT9jQJDQx5h5uAVPimw6yVg2SekOKu959OCtktD3FjzbpvaPr8i4zzg07DOMz+igA4W/aNM7OV8H37pFYfA== +query-string@^6.2.0: + version "6.2.0" + resolved "https://registry.yarnpkg.com/query-string/-/query-string-6.2.0.tgz#468edeb542b7e0538f9f9b1aeb26f034f19c86e1" + integrity sha512-5wupExkIt8RYL4h/FE+WTg3JHk62e6fFPWtAZA9J5IWK1PfTfKkMS93HBUHcFpeYi9KsY5pFbh+ldvEyaz5MyA== + dependencies: + decode-uri-component "^0.2.0" + strict-uri-encode "^2.0.0" + querystring-es3@^0.2.0: version "0.2.1" resolved "https://registry.yarnpkg.com/querystring-es3/-/querystring-es3-0.2.1.tgz#9ec61f79049875707d69414596fd907a4d711e73" @@ -7592,6 +7600,11 @@ stream-to-observable@^0.1.0: resolved "https://registry.yarnpkg.com/stream-to-observable/-/stream-to-observable-0.1.0.tgz#45bf1d9f2d7dc09bed81f1c307c430e68b84cffe" integrity sha1-Rb8dny19wJvtgfHDB8Qw5ouEz/4= +strict-uri-encode@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz#b9c7330c7042862f6b142dc274bbcc5866ce3546" + integrity sha1-ucczDHBChi9rFC3CdLvMWGbONUY= + string-width@^1.0.1, string-width@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"