Skip to content

Commit

Permalink
feat: initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
tincho1089 committed Mar 8, 2023
1 parent 29bef81 commit 505076d
Show file tree
Hide file tree
Showing 85 changed files with 19,489 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
packages/**/node_modules
.gitignore
.git
*.md
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NODE_ENV=development
FRONTEND_PORT=5173
BACKEND_PORT=4000
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**/node_modules/
/**/build/
/**/dist/
.vscode
44 changes: 42 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# GraphQL-Crud
CRUD using GraphQL on the frontend and a GraphQL server configured on the backend
# GraphQL Crud

## Application Demo

The purpose of this project was to create a CRUD using GraphQL on the frontend and set up a GraphQL server on the backend. The frontend uses React JS + Typescript with Vite. Vitest was also used for testing.
The backend uses node + express as the base for the GraphQL configuration.
MongoDB is used as the database and an instance is created through Docker.

![Demo](/images/demo.gif?raw=true "Demo")


## Local execution with Docker
The command will build the API developed in Node, will create a MongoDB instance and will build the frontend project developed in React JS with Vite.
`docker-compose stop && docker-compose up --build -d --remove-orphans`

![Docker Services](/images/docker.png?raw=true "Docker Services")

This is the final result of the services that you should see if everything works as expected.

## Application access
To access the application you can do it through: `http://localhost:5173`

![Application](/images/app.png?raw=true "Application")


## Database Access
To MongoDB database can be accessed through the extension MongoDB for VScode:

![db extension](/images/mongoextension.png?raw=true "db extension")

The connection string should be for this case `mongodb://localhost:27017/`. After we stablish the connection, we should have access to the database:

![db access](/images/mongodb.png?raw=true "db access")


## Testing
To execute the tests locally, we need to install the libraries dependencies for the service with `npm i`. After that we can run the command:
`npm run test`

The tests are working with Vitest and Testing Library

![tests](/images/testFront.png?raw=true "tests")
12 changes: 12 additions & 0 deletions backend/Dockerfile.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:16-alpine AS development
RUN mkdir -p /svr/app
WORKDIR /svr/app
COPY package*.json ./

RUN npm install glob rimraf

RUN npm install

COPY . .

RUN npm run build
25 changes: 25 additions & 0 deletions backend/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import cors from 'cors';
import mongoose from 'mongoose';
import { resolvers } from './resolvers';
import { typeDefs } from './typeDefs';
const dbContext = process.argv[2] == 'mongodb' ? 'mongodb' : 'localhost';
async function startServer() {
const app = express();
app.use(cors());

const server = new ApolloServer({ typeDefs, resolvers });

mongoose.connect(`mongodb://${dbContext}:27017/myapp`);

await server.start();

server.applyMiddleware({ app });

app.listen({ port: 4000 }, () =>
console.log(`Server ready at http://localhost:4000${server.graphqlPath}`)
);
}

startServer();
27 changes: 27 additions & 0 deletions backend/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import mongoose from 'mongoose';

export interface UserDocument extends mongoose.Document {
name: string;
email: string;
}

export interface ProductDocument extends mongoose.Document {
name: string;
description: string;
price: number;
}

const UserSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true },
});

export const User = mongoose.model<UserDocument>('User', UserSchema);

const ProductSchema = new mongoose.Schema({
name: { type: String, required: true },
description: { type: String, required: true },
price: { type: Number, required: true },
});

export const Product = mongoose.model<ProductDocument>('Product', ProductSchema);
6 changes: 6 additions & 0 deletions backend/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"watch": ["src"],
"ext": "ts",
"exec": "ts-node src/index.ts"
}

Loading

0 comments on commit 505076d

Please sign in to comment.