Logo-amall

Hello, whats the best practice when inserting data to graphql from function? graphql-request isnt working and node-fetch either. just cannot import anything other than express. ` import fetch from "node-fetch"; //// node:internal/modules/cjs/loader:985 const err = new Error(message); ^ Error: Cannot find module 'node-fetch' `

Last active 4 months ago

8 replies

14 views

  • NE

    Hello, whats the best practice when inserting data to graphql from function?

    graphql-request isnt working and node-fetch either.

    just cannot import anything other than express.

    `
    import fetch from "node-fetch";
    ////
    node:internal/modules/cjs/loader:985
    const err = new Error(message);
    ^

    Error: Cannot find module 'node-fetch'
    `

  • NE

    i was looking here:
    https://github.com/nhost/nhost/tree/main/examples/serverless-functions/functions

    no clear way how to manipulate and insert data to database.

  • EL

    Hmm. You should be able tonimport to import npm packages without any issues.

  • EL

    Check this: https://github.com/nhost/nhost/tree/main/examples/serverless-functions/functions/npm-packages

  • EL

    And you can use this:

  • EL

    https://docs.nhost.io/reference/javascript/nhost-js/graphql/request

  • NE
    {
      "dependencies": {
        "@nhost/nhost-js": "^1.6.0",
        "@types/express": "^4.17.14",
        "express": "^4.18.2",
        "graphql": "^16.6.0"
      }
    }
    
    import { Request, Response } from "express";
    import { NhostClient } from "@nhost/nhost-js";
    
    export default async (req: Request, res: Response) => {
      const userId = req.body.event.data.new.id;
      res.status(200).send(`${userId}`);
    };
    
    node:internal/modules/cjs/loader:985
      const err = new Error(message);
                  ^
    
    Error: Cannot find module '@nhost/nhost-js'
    Require stack:
    - /opt/project/functions/add-activity/changed-password.ts
        at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15)
        at Function.d.default._resolveFilename (/usr/local/lib/node_modules/tsx/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:1454)
        at Function.Module._load (node:internal/modules/cjs/loader:833:27)
        at Module.require (node:internal/modules/cjs/loader:1057:19)
        at require (node:internal/modules/cjs/helpers:103:18)
        at  (/opt/project/functions/add-activity/changed-password.ts:2:29)
        at Object. (/opt/project/functions/add-activity/changed-password.ts:14:16)
        at Module._compile (node:internal/modules/cjs/loader:1155:14)
        at Object.v (/usr/local/lib/node_modules/tsx/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:901)
        at Module.load (node:internal/modules/cjs/loader:1033:32) {
      code: 'MODULE_NOT_FOUND',
      requireStack: [ '/opt/project/functions/add-activity/changed-password.ts' ]
    }
    [nodemon] app crashed - waiting for file changes before starting...
    

    i just dont understand

  • NE

    ooooh.. now i get it..
    /package.json express
    /functions/package.json packages.. we can close this now

Last active 4 months ago

8 replies

14 views