javascriptimportgraphqlmonorepoturborepo

how to import apps in packages - turbo monorepo


I'm trying to import some backend methods apps/backend/... into my packages/graphql because my graphql resolvers actualy calls backend routes, but I can't seem to see how in turbo monorepo documentation...

my current frontend (client) imports "@repo/graphql" : "*" but now i need my graphql package to access my backend (server) methods, but I can't just workaround that with relative path, because turbo will not resolve it after deployment.

does anyone have an idea ?

would you recommand me to have graphql as an app in /apps or the packages strategy is a good idea perhaps ?

here is somme information:

let's say I have a getComment from my ./apps/server/src/domain/task/comment.controller.ts and that I access that ressource through graphql ./packages/graphql/graphql/src/resolvers.ts, the resolver would call that specific method.

.
├── apps
│   ├── client
│   └── server
├── packages
│   ├── graphql
│   │  ├── graphql
│   │  │   ├── schema.gql
│   │  │   ├── resolvers.ts 
│   │  │   └── src
│   │  ├── package.json
│   │  ├── src
│   │  │   ├── schema.graphql
│   │  │   └── server.ts
│   │  ├── README.md
│   │  ├── codegen.yml
│   │  └── tsconfig.json
│   └── prisma
├── README.md
├── docker-compose.yml
├── eslint.config.mjs
├── makefile
├── package-lock.json
├── package.json
├── prettier.config.mjs
├── tsconfig.json
└── turbo.json

Solution

  • You neet to export your methods from ./apps/server Nestjs application like a package does in monorepo.

    Here are the steps to do so.

    1. Create a src/export.ts file in apps/server and export * from whatever file or methods which has to be used by graphql package.
    // src/export.ts  
    
    export * from "./domain/task/comment.controller.ts"
    
    1. export src/export.ts from apps/server/package.json
    // ./apps/server/package.json
    
    {
      "name": "server",
      "version": "0.0.1",
      "description": "",
    
      // like this
      "exports": {
        ".": "./src/export.ts"
      },
    
    
    
     "dependencies":{
        "@nestjs/common": "^10.0.0",
      }
    
    }
    
    
    1. Now you need to import this package into your graphql package i.e. /packages/graphql. In ./packages/graphql/package.json add "server":"workspace:*" to dependencies object.
    // ./packages/graphql/package.json
    
      {
        "name": "your-graphql-package",
        "version": "0.0.1",
        "description": "",
        "exports": {
          "types": "./src/export.d.ts"
        },
       "dependencies":{
          "server":"workspace:*"
         // other dependencies
        }
      }
    
    1. And finally run npm/pnpm/yarn/bun install to let turborepo resolve dependencies for graphql package from server app.

    2. You can now import your server packages into your graphql packages by doing something like.

    // ./packages/graphql/index.ts
    
    import { CommentController } from "server";
        
    

    Although it is not recommendad to export from apps/ folder to packges/. You might want to reconsider the architechture of your application.