Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
How to merge exsiting graphql nodejs backend in to nextjs app

I have 2 repositories -

  1. Server: A graphql api in typescript/node which communicates with my postgres database through typeorm. This runs on localhost:4000
  2. Client: A nextjs app which sends requests to my graphql server and displays some data and does other things. This runs on localhost:3000

As far as I can tell I could also have both of those pieces of code in a single repo and have 1 package.json etc. I would like to implement this for 2 reasons

  1. The 2 could share code
  2. It will make things easier to deploy

I am however a bit unclear how I should go about this. Do I just put all of the code that I currently have in my Server repo in to the folder "src/pages/api" of the client repo and be done with it?

Below is the server index.js

import { ApolloServer } from "apollo-server-express";
import cors from 'cors';
import express from "express";
import "reflect-metadata";
import { buildSchema } from "type-graphql";
import { appDataSource } from "./data-source";
import { HelloResolver, MarketDataResolver, UserResolver} from "./resolvers";

export const startServer = async () => {
  await appDataSource.initialize();
  await appDataSource.runMigrations();

  const app = express();
  app.use(cors({
    origin: ['http://localhost:3000', 'https://studio.apollographql.com'],
    credentials: true
  }))

  const apolloServer = new ApolloServer({
    schema: await buildSchema({
      resolvers: [UserResolver, HelloResolver, MarketDataResolver],
      validate: false,
    }),
    context: () => ({ em: appDataSource.manager }),
  });

  await apolloServer.start();

  apolloServer.applyMiddleware({ app, cors: false });

  app.listen(4000, () => {
    console.log("server started on localhost:4000");
  });
};

startServer().catch((err) => {
  console.error(err);
});

And the scripts in Server package.json

{
  "name": "server",
  "main": "index.js",
  "scripts": {
    "test-server": "NODE_ENV=test ts-node src/index.ts",
    "test": "NODE_ENV=test jest",
    "rstest": "start-server-and-test 'yarn test-server' 'http-get://localhost:4000/graphql?query={ __schema { queryType { name } } }' 'yarn test'",
    "watch": "tsc -w",
    "start": "node dist/index.js",
    "dev": "nodemon dist/index.js",
    "start2": "ts-node src/index.ts",
    "dev2": "nodemon --exec ts-node src/index.ts",
    "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"
  },

I usually run my server with yarn watch in one terminal and yarn dev in a second.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!