4

This may be a strange question, but I'm having trouble connecting to a local running app from a hosted website that attempts to POST a form to my local URL/endpoint. I'm sure it is something simple that I'm missing -- or just a lack of knowledge -- but any help would be appreciated.

The way it works is this:

  • I have a Vue Apollo app running on https://localhost:8080, and the graphql server runs on http://localhost:4000/graphql. This is only for dev/test purposes, of course. Eventually, it will be hosted.

  • In a third-party app hosted by someone else, they have a launch service that will launch my app in a modal (like a plug-in). That launch POSTS some form data which I would use to consume various bits of user info to authenticate, etc.

Whenever they attempt to POST (or I attempt through Postman) to my localhost:8080 clientside, it terminates in a 404. I can POST to the localhost:4000/graphql endpoint just fine on the server. So, I guess my question is this:

  1. Is there a way to receive POST requests on the clientside to a vue-router endpoint? I haven't had much luck with my googling.
  2. If I instead POST to the graphql server, it would need to be over https (requirement of the third-party host environment). I haven't been able to find a definitive answer on how to get the graphql server to serve over https. And then how do I create a custom endpoint to receive the POST? Do I use express middleware, or is there a more standard method?

This is my vue-apollo.js:

import Vue from 'vue'
import VueApollo from 'vue-apollo'
import { createApolloClient, restartWebsockets } from 'vue-cli-plugin-apollo/graphql-client'

// Install the vue plugin
Vue.use(VueApollo)

// Name of the localStorage item
const AUTH_TOKEN = 'apollo-token';
const TOKEN_TYPE = 'token-type';
const CLIENT_ID = 'client-id';
var APOLLO_CLIENT;

// Http endpoint
const httpEndpoint = process.env.VUE_APP_GRAPHQL_HTTP || 'http://localhost:4000/graphql'

// Config
const defaultOptions = {
  // You can use `https` for secure connection (recommended in production)
  httpEndpoint,
  // You can use `wss` for secure connection (recommended in production)
  // Use `null` to disable subscriptions
  wsEndpoint: process.env.VUE_APP_GRAPHQL_WS || 'ws://localhost:4000/graphql',
  // LocalStorage token
  tokenName: AUTH_TOKEN,
  // Enable Automatic Query persisting with Apollo Engine
  persisting: false,
  // Use websockets for everything (no HTTP)
  // You need to pass a `wsEndpoint` for this to work
  websocketsOnly: false,
  // Is being rendered on the server?
  ssr: false,

  // Override default apollo link
  // note: don't override httpLink here, specify httpLink options in the
  // httpLinkOptions property of defaultOptions.
  // link: myLink

  // Override default cache
  // cache: myCache

  // Override the way the Authorization header is set
  // getAuth: (tokenName) => ...

  // Additional ApolloClient options
  // apollo: { ... }

  // Client local data (see apollo-link-state)
  // clientState: { resolvers: { ... }, defaults: { ... } }
}

// Call this in the Vue app file
export function createProvider (options = {}) {
  // Create apollo client
  //console.log("CREATE PROVIDER CALLED")
  const { apolloClient, wsClient } = createApolloClient({
    ...defaultOptions,
    ...options,
  })
  apolloClient.wsClient = wsClient

  // Create vue apollo provider
  const apolloProvider = new VueApollo({
    defaultClient: apolloClient,
    defaultOptions: {
      $query: {
        // fetchPolicy: 'cache-and-network',
      },
    },
    errorHandler (error) {
      // eslint-disable-next-line no-console
      console.log('%cError', 'background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold;', error.message)
    },
  })
  APOLLO_CLIENT = apolloClient;
  return apolloProvider;
}

// Manually call this when user log in
export async function onLogin (token, token_type, client_id) {
  if (typeof localStorage !== 'undefined' && token) {
    localStorage.setItem(AUTH_TOKEN, token);
    localStorage.setItem(TOKEN_TYPE, token_type);
    localStorage.setItem(CLIENT_ID, client_id);
    console.log("ON LOGIN LOCAL STORAGE ITEMS: " + '', localStorage);
  }
  if (APOLLO_CLIENT.wsClient) restartWebsockets(APOLLO_CLIENT.wsClient)
  try {
    await APOLLO_CLIENT.resetStore()
  } catch (e) {
    // eslint-disable-next-line no-console
    console.log('%cError on cache reset (login)', 'color: orange;', e.message)
  }
}

// Manually call this when user log out
export async function onLogout (apolloClient) {
  if (typeof localStorage !== 'undefined') {
    localStorage.removeItem(AUTH_TOKEN)
  }
  if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient)
  try {
    await apolloClient.resetStore()
  } catch (e) {
    // eslint-disable-next-line no-console
    console.log('%cError on cache reset (logout)', 'color: orange;', e.message)
  }
}

No matter what I set the httpEndpoint to, it still spins up the server at http://localhost:4000/graphql. The only other reference I could find to that URL is in grapqhlconfig.yml, and I have changed it there, as well, to no avail. I must be missing something -- a way to override, perhaps, that I can't find in the docs or through googling.

Is there a Best Practice, even in general, that I'm missing as far as getting my local app to receive POST calls from a remote website?

I should add that I'm using a fairly default setup with the Vue CLI 3 scaffolding and vue-apollo default setup.

Here is my vue.config.js:

module.exports = {
pluginOptions: {
    apollo: {
      enableMocks: false,
      enableEngine: false,
      // Base folder for the server source files
      serverFolder: './apollo-server',
      // Cross-Origin options
      cors: '*',
    }
  },
  devServer: {
    disableHostCheck: true,
    https: true
  }
}

Many thanks for any help.

Trip Gould
  • 41
  • 3

1 Answers1

0

I would say the best and safe way is to handle your POST request using a framework like hapi or express if you have your app and graphql running on the same server you don't even need to use graphql you can call your method that will handle the user data for authentication and etc.

Uziel Valdez
  • 2,040
  • 1
  • 15
  • 20