Estratégias Avançadas Baseadas em Formação
Diferentes formação
<|repo_name|>johannmolinari/Nodejs-Express-React-Apollo-GraphQL-Typescript<|file_sep|>/src/types/Book.ts
import { Field, ID, Int, ObjectType } from 'type-graphql';
@ObjectType()
export class Book {
@Field(() => ID)
id: number;
@Field()
name: string;
@Field()
price: number;
@Field(() => Int)
pages: number;
}
<|file_sep|>// import { ApolloServer } from 'apollo-server-express';
// import express from 'express';
// import { buildSchema } from 'type-graphql';
// import { BookResolver } from './resolvers/BookResolver';
// const app = express();
// async function start() {
// const apolloServer = new ApolloServer({
// schema: await buildSchema({
// resolvers: [BookResolver],
// validate: false,
// }),
// });
// apolloServer.applyMiddleware({ app });
// app.listen(4000);
// }
// start();
<|file_sep|>// import { Query } from 'type-graphql';
import { Resolver } from 'type-graphql';
import { Book } from '../types/Book';
@Resolver(Book)
export class BookResolver {
@Query(() => [Book])
async books() {
return [
{
id: 1,
name: 'The Awakening',
price: 20,
pages: 500,
},
{
id: 2,
name: 'City of Glass',
price: 25,
pages: 300,
},
];
}
}
<|repo_name|>johannmolinari/Nodejs-Express-React-Apollo-GraphQL-Typescript<|file_sep|>/src/server.ts
import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import cors from 'cors';
import path from 'path';
import { buildSchema } from 'type-graphql';
import { BookResolver } from './resolvers/BookResolver';
const main = async () => {
const schema = await buildSchema({
resolvers: [BookResolver],
});
const app = express();
app.use(cors());
app.get('/', (_, res) => {
res.sendFile(path.join(__dirname + '/../client/build/index.html'));
});
const apolloServer = new ApolloServer({ schema });
await apolloServer.start();
apolloServer.applyMiddleware({ app });
app.listen(4000);
console.log('server started on port :4000');
};
main();
<|file_sep|># Nodejs Express React Apollo GraphQL Typescript
This is an example project for learning purposes.
# Setup
1) Create the project folder
bash
mkdir nodejs-express-react-apoll-graphql-typescript
cd nodejs-express-react-apoll-graphql-typescript
2) Init the project
bash
npm init --yes
## Backend
### Install dependencies
bash
npm i --save express cors type-graphql reflect-metadata graphql apollo-server-express graphql-tools ts-node @types/node @types/express @types/cors typescript ts-node-dev nodemon
### Create tsconfig.json file
json
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"jsx": "react",
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"outDir": "./dist",
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
### Add scripts to package.json file
json
{
"scripts": {
"start": "nodemon --exec ts-node-dev src/server.ts"
}
}
### Create src/server.ts file
ts
import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import cors from 'cors';
import path from 'path';
import { buildSchema } from 'type-graphql';
const main = async () => {
const schema = await buildSchema({
resolvers:[]
});
const app = express();
app.use(cors());
app.get('/', (_, res) => {
res.sendFile(path.join(__dirname + '/../client/build/index.html'));
});
const apolloServer = new ApolloServer({ schema });
await apolloServer.start();
apolloServer.applyMiddleware({ app });
app.listen(4000);
console.log('server started on port :4000');
};
main();
### Create .env file
bash
touch .env
### Add dev environment variables to .env file
bash
PORT=4000
NODE_ENV=development
### Create .gitignore file
bash
touch .gitignore
### Add folders and files to ignore to .gitignore file
/node_modules/
/dist/
/client/node_modules/
/client/build/
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
## Frontend
### Create client folder and install dependencies
bash
mkdir client && cd client && npm i --save react react-dom @apollo/client graphql @types/react @types/react-dom graphql-tag react-scripts typescript concurrently cross-env wait-on rimraf serve ts-node-dev nodemon && cd ..
### Create src/client/index.tsx file
tsx
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(
<>
Hello World!
>,
document.getElementById('root')
);
### Add scripts to package.json file
json
{
"scripts": {
"start": "nodemon --exec ts-node-dev src/server.ts",
"start-client":"concurrently "cross-env BROWSER=none npm start" "wait-on http://localhost:3000 && wait-on http://localhost:4000 && open http://localhost:3000"",
"build-client":"react-scripts build",
"build":"rimraf ./dist && rimraf ./client/build && concurrently "cross-env NODE_ENV=production npm run build-client" "cross-env NODE_ENV=production npm run start"",
"start-prod":"serve -s dist"
}
}
### Create src/client/index.html file
;
### Create src/client/App.tsx file
tsx