Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apollo init #5

Merged
merged 11 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#ignore mysql folder created by mysql image
mysql
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SERVER_PORT=4444
MYSQL_HOST=mysql
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_ROOT_PASSWORD=crispy
MYSQL_PASSWORD=crispy
MYSQL_DATABASE=crispy
9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
parser: "@typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.json"
},
plugins: ["@typescript-eslint"],
extends: ["plugin:@typescript-eslint/recommended"],
rules: {}
};
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM node:12.13.1 as base

WORKDIR /code

COPY package.json /code/package.json
COPY yarn.lock /code/yarn.lock
COPY .env /code/.env
RUN yarn

EXPOSE 4444

CMD ["yarn", "start"]

FROM base as prod

COPY . /code
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
# crispy-api
Typescript GraphQL API for `crispy-web`

API based on [`Apollo`](https://www.apollographql.com/docs/apollo-server/) express server.

## How to use
Bazillius marked this conversation as resolved.
Show resolved Hide resolved

### 1. Download project & install dependencies

Clone the `crispy-api` branch of this repository:

```
git clone git@github.com:WiseEngineering/crispy-api.git
```

Go to project folder:

```
cd crispy-api
cp .env.example .env
```


### 2. Start up `Docker` servers

Command to start up server

```
docker-compose up -d
```

Run migration:

```
docker exec -it crispy-api yarn migrate
```

if you use docker-compose you can get container name use

```
docker ps
```

### 3. UI client URL

```
http://localhost:4444/graphql/playground
```
20 changes: 20 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: '3.7'

services:
mysql:
image: mysql:5.7
env_file: .env
volumes:
- ./mysql:/var/lib/mysql
graphql:
build:
context: .
target: base
env_file: .env
links:
- mysql
ports:
- '4444:4444'
volumes:
- ./:/code
- /mysql
20 changes: 20 additions & 0 deletions knexfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as knex from "knex";
require('dotenv').config();
Bazillius marked this conversation as resolved.
Show resolved Hide resolved

const database = {
client: "mysql",
connection:{
host: process.env.MYSQL_HOST,
port: process.env.MYSQL_PORT,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
},
migrations: {
directory: './migrations',
tableName: 'knex_migrations'
},
} as knex.Config;

export = database;

14 changes: 14 additions & 0 deletions migrations/20191218152204_users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as Knex from "knex";


export async function up(Knex: Knex): Promise<any> {
await Knex.schema.createTable('users', table => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure we need user schema for start.
We've decided to start from migrations and them move forward with them

  • logging
  • workers
  • migrations
  • running flow

We have to look at those areas first
I think we have to change this schema with the workers schema. This is the one we really need.
The interface could simple for start:

name: 'beta' //worker name will be sent from server where this migration going to be run
created_at
updated_at
endpoint // where worker is hosted. We will call this API to communicate with worker
api_key? // not required will figure out later how we are going to make auth

Let's change this schema with those fields, then we will be ready to use it in client side

table.string('id').notNullable().primary();
table.string('name').notNullable();
});
}


export async function down(Knex: Knex): Promise<any> {
await Knex.schema.dropTableIfExists('users');
}
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "crispy-api",
"version": "0.0.1",
"main": "index.js",
"repository": "git@github.com:WiseEngineering/crispy-api.git",
"author": "WiseEngineering open-source@wise-engineering.com",
"license": "MIT",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "ts-node src/index.ts",
Bazillius marked this conversation as resolved.
Show resolved Hide resolved
"migrate": "knex migrate:latest",
"develop": "nodemon --watch 'src/**/*.ts' --ext 'ts' --exec 'yarn start'",
"lint": "eslint src/**/*.ts "
},
"dependencies": {
"apollo-server-express": "2.9.14",
"express": "4.17.1",
"graphql": "14.5.8",
"graphql-import": "0.7.1",
"knex": "0.20.4",
"mysql": "2.17.1"
},
"devDependencies": {
"@types/chai": "4.2.7",
"@types/knex": "0.16.1",
"@types/node": "10.17.9",
"@types/mocha": "5.2.7",
"@typescript-eslint/eslint-plugin": "2.10.0",
"@typescript-eslint/parser": "2.10.0",
"dotenv": "8.2.0",
"eslint": "6.7.2",
"mocha": "6.2.2",
"chai": "4.2.0",
"nodemon": "2.0.2",
"ts-node": "7.0.1",
"typescript": "3.2.2"
}
}
3 changes: 3 additions & 0 deletions src/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Knex from "knex";
import * as database from "../knexfile";
export const knex = Knex(database);
11 changes: 11 additions & 0 deletions src/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { knex } from '../db';

const users = () => knex('users');

const Query = {
users,
};

const resolvers = { Query };

export { resolvers }
12 changes: 12 additions & 0 deletions src/graphql/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type User {
id: ID!
name: String!
}

type Query {
users: [User]
}

type Mutation {
addUser(name: String!): User!
}
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import { importSchema } from 'graphql-import'
import { resolvers } from "./graphql";

const typeDefs = importSchema('src/graphql/schema.graphql');
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();

server.applyMiddleware({ app });
app.use('/', express.static('public'));

const port = process.env.SERVER_PORT || 4000;
Bazillius marked this conversation as resolved.
Show resolved Hide resolved
app.listen(port, () => console.log(`App listening on port ${port}!`));
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
Bazillius marked this conversation as resolved.
Show resolved Hide resolved
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["es6", "es2015", "dom"],
"declaration": true,
"outDir": "dist",
"rootDir": ".",
"strict": true,
"types": ["node", "mocha"],
"esModuleInterop": true,
"resolveJsonModule": true
}
}
Loading