I have 2 repositories -
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
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.