Add graph-handling for different environments

This commit is contained in:
2019-01-15 14:27:15 +01:00
parent 5852ab4b56
commit 1ad951ca60
3 changed files with 42 additions and 2 deletions
+28 -2
View File
@@ -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);