'How to get the current https functions endpoint for firebase when using/not using emulator

I have a graphql api running on a firebase function and I develop locally for the most part.

Before when I called the firebase functions directly I would use the connectFunctionsEmulator function to conditionally connect to my local emulator and when I called the functions using getFunctions it would know to call my localhost endpoint.

However I'm trying to use Apollo client and I'm not sure how to determine when the local emulators have been connected and when they have not as well as how to determine the absolute url when calling the function like the way firebase does when I call it using the firebase functions helpers.



Solution 1:[1]

I went snooping through the code and when I call the connectFunctionsEmulator function it updates a variable called functionsInstance.emulatorOrigin.

This is not present in the functions typing but I'm still able to access the value by doing getFunctions().emulatorOrigin.

If the value is not set to null that means firebase will be talking to my emulators and not to the live endpoint.

function getGraphQLUri(): string {
  const functions = getFunctions();
  const app = getApp();

  const { projectId } = app.options;
  const { region } = functions;

  // emulatorOrigin is not defined in the typings but it exists
  // @ts-ignore
  const emulator = functions.emulatorOrigin;

  let url: string = "";

  if (emulator) {
    url = `${emulator}/${projectId}/${region}/graphql`;
  } else {
    url = `https://${region}-${projectId}.cloudfunctions.net/graphql`;
  }

  return url;
}

I would appreciate if someone at Google can confirm whether this approach might continue to be supported in the future.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1