Skip to content

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

Closed

Description

@OutdatedGuy

[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

Activity

  1. 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[/+] on Mar 27, 2025
  2. added
    emulator: dataconnect
    on Mar 28, 2025
  3. aalej commented on Mar 28, 2025

    @aalej
    Contributor

    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?

  4. OutdatedGuy commented on Mar 28, 2025

    @OutdatedGuy
    ContributorAuthor

    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!

  5. OutdatedGuy commented on Mar 28, 2025

    @OutdatedGuy
    ContributorAuthor

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    emulator: dataconnectemulators: functionsreproducibletype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions