const queryString = require('query-string'); const { createApolloFetch } = require('apollo-fetch'); const { includeCredentials } = require('./middleware'); 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: getGraphURI() }); apollo.use(includeCredentials(tokenFn)); // apollo.useAfter(trackErrors); return apollo({ query, variables }); };