Skip to content

Unable to perform queries on Data Connect locally using Cloud Functions in emulator #8379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
OutdatedGuy opened this issue Mar 27, 2025 · 3 comments · Fixed by #8387
Closed
Labels

Comments

@OutdatedGuy
Copy link
Contributor

[REQUIRED] Environment info

firebase-tools: 14.0.0

Platform: macOS

[REQUIRED] Test case

index.ts
import { initializeApp } from "firebase-admin";
import { getDataConnect } from "firebase-admin/data-connect";
import { onRequest } from "firebase-functions/https";

initializeApp();

export const callFn = onRequest(async (_req, res) => {
  const dataConnect = getDataConnect({
    location: "us-central1",
    serviceId: "dataconnect",
  });

  const result = await dataConnect.executeGraphql(
    `mutation {
      movie_insert(
        data: {
          id: "11111111222233334444555555555555"
          genre: ""
          imageUrl: ""
          title: ""
        }
      )
    }
  `
  );

  res.status(200).json({ result });
});
schema.gql
# Movie is keyed by a randomly generated UUID.
type Movie @table {
  # If you do not pass a 'key' to `@table`, Data Connect automatically adds the following 'id' column.
  # Feel free to uncomment and customize it.
  #  id: UUID! @default(expr: "uuidV4()")
  title: String!
  imageUrl: String!
  genre: String
}

[REQUIRED] Steps to reproduce

  1. Create a firebase project with Data Connect & Cloud Functions in Typescript using:

    firebase init
  2. Update the functions/src/index.ts file with the code above.

  3. Update the dataconnect/schema/schema.gql file with the schema above.

  4. Run below command to install latest dependencies:

    npm install firebase-admin@latest typescript@latest
  5. Run emulator using the command below:

    npm run build && firebase emulators:start
  6. Call the http function using the command below:

    curl -X POST http://127.0.0.1:5001/<project-id>/us-central1/callFn
  7. Check the error logs in the console.

[REQUIRED] Expected behavior

When running locally using emulators, the function should execute successfully and insert a new movie record into the Data Connect table.

[REQUIRED] Actual behavior

⚠  functions: Error: Resource "projects/<project-id>/locations/us-central1/services/dataconnect" was not found
    at DataConnectApiClient.toFirebaseError (functions/node_modules/firebase-admin/lib/data-connect/data-connect-api-client-internal.js:161:16)
    at functions/node_modules/firebase-admin/lib/data-connect/data-connect-api-client-internal.js:107:24
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async functions/lib/index.js:58:31
    at async runFunction (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:506:9)
    at async runHTTPS (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:531:5)
    at async /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:694:21
⚠  Your function was killed because it raised an unhandled error.
i  Request to function failed: Error: socket hang up
@OutdatedGuy OutdatedGuy changed the title Unable to perform queries on **Data Connect** locally using **Cloud Functions** in emulator Unable to perform queries on Data Connect locally using Cloud Functions in emulator Mar 27, 2025
@aalej aalej added emulators: functions emulator: dataconnect Issues related to the dataconnect emulator labels Mar 28, 2025
@aalej
Copy link
Contributor

aalej commented Mar 28, 2025

Thanks for sharing a test case @OutdatedGuy! When trying to reproduce this issue, I noticed that the debug logs indicate that a request is being made to production instead of the Data Connect emulator, which could be causing the error. Did you notice any logs similar to below?

⚠  Google API requested!
   - URL: "https://oauth2.googleapis.com/token"
   - Be careful, this may be a production service.

If you are trying to make a query on the Data Connect emulator, could you try updating your code to something like:

import { initializeApp } from "firebase-admin/app";
import { getDataConnect } from "firebase-admin/data-connect";
import { onRequest } from "firebase-functions/https";

process.env.DATA_CONNECT_EMULATOR_HOST = "127.0.0.1:9399"; // Connects to the Data Connect emulator
initializeApp();

export const callFn = onRequest(async (_req, res) => {
  const dataConnect = getDataConnect({
    location: "us-central1",
    serviceId: "test-dc-2",
  });
  const result = await dataConnect.executeGraphql(
    `mutation {
        movie_insert(
          data: {
            id: "11111111222233334444555555555667"
            genre: ""
            imageUrl: ""
            title: ""
          }
        )
      }
    `
  );
  res.status(200).json({ result });
});

The Firebase CLI should be automatically setting Data Connect emulator host when it detects that the Data Connect emulator is being used.

If you are trying to connect to a production instance of Data Connect, could you double check if your serviceId matches the one from your dataconnect.yaml file?

@OutdatedGuy
Copy link
Contributor Author

OutdatedGuy commented Mar 28, 2025

Did you notice any logs similar to below?

Yes, I can see the accessing production service warning.

If you are trying to make a query on the Data Connect emulator, could you try updating your code to something like:

After updating the code I'm getting the different error below:

process.env.DATA_CONNECT_EMULATOR_HOST = "127.0.0.1:9399";

Adding the above line worked in my main codebase (hurray 🎉), but I'm getting the below error in my demo (Minimum Reproducible Code).

⚠  functions: Error: Error while making request: write EPROTO 4008DDEB01000000:error:0A00010B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:355:
. Error code: EPROTO
    at .../functions/node_modules/firebase-admin/lib/utils/api-request.js:265:19
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async .../functions/node_modules/firebase-admin/lib/data-connect/data-connect-api-client-internal.js:91:26
    at async .../functions/lib/index.js:14:20
    at async runFunction (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:506:9)
    at async runHTTPS (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:531:5)
    at async /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:694:21
⚠  Your function was killed because it raised an unhandled error.
i  Request to function failed: Error: socket hang up

Thanks @aalej for the help!

@OutdatedGuy
Copy link
Contributor Author

@aalej @joehan after updating firebase-tools to 14.0.1 I'm getting the below error:

⚠  functions: Error: Error while making request: getaddrinfo ENOTFOUND http. Error code: ENOTFOUND

So playing around a bit with host values I figured out that setting process.env.DATA_CONNECT_EMULATOR_HOST to http://127.0.0.1:9399 instead of just 127.0.0.1:9399 causes the above issue.

So it looks like this change from #8387 needs to be modified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants