Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda