apollographql / meteor-integration

🚀 meteor add apollo
http://dev.apollodata.com/core/meteor.html
108 stars 45 forks source link

Best practice for setting ROOT_URL when you wont know it until app is deployed? #97

Closed acomito closed 7 years ago

acomito commented 7 years ago

I'm pushing my app to a staging server where I have no idea what URL will be generated until after it's deployed. Is there a good way to reset the ROOT_URL? I've tried a few strategies but the app always seems to point to the ROOT_URL I set in my settings.json (which is just some arbitrary URL).

One thing I tried was the below, but no luck:

import ApolloClient, { createNetworkInterface } from 'apollo-client';
import { meteorClientConfig } from 'meteor/apollo';
import { ApolloProvider } from 'react-apollo';
import { Meteor } from 'meteor/meteor'

let uri;

if (Meteor.isServer) {
  Meteor.onConnection(function(result){
    uri = `${result.httpHeaders.referer}/graphql`; //This returns http://foo.example.com/graphql
    process.env.ROOT_URL = result.httpHeaders.referer;
  });
}

const networkInterface = { 
    uri: uri,
    useMeteorAccounts: true 
}

const client = new ApolloClient(meteorClientConfig(networkInterface));

export default client;

I'm not sure if the uri is set in meteorClientConfig or if I can overwrite it in the network interface object. Right now this file is not in the client folder, it is in imports. I guess maybe I could move it to the client and use window.location.hostname?

I'll report back if I find something, but if anyone has tackled this before-- your hints are welcomed.