Skip to content

Commit 2ee687f

Browse files
committed
init + user resolver
0 parents  commit 2ee687f

File tree

11 files changed

+3856
-0
lines changed

11 files changed

+3856
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea/
2+
.vscode/
3+
node_modules/
4+
build/
5+
tmp/
6+
temp/

README.md

Whitespace-only changes.

ormconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"type": "postgres",
3+
"host": "localhost",
4+
"port": 5432,
5+
"username": "test",
6+
"password": "test",
7+
"database": "test",
8+
"synchronize": true,
9+
"logging": true,
10+
"entities": ["src/entity/**/*.*"],
11+
"cli": {
12+
"entitiesDir": "src/entity"
13+
}
14+
}

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "gql-ts-server",
3+
"version": "1.0.0",
4+
"main": "src/index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"dev": "ts-node-dev --respawn src/index.ts",
8+
"start": "npm run dev"
9+
},
10+
"dependencies": {
11+
"apollo-server-express": "^2.8.1",
12+
"class-validator": "^0.10.0",
13+
"express": "^4.17.1",
14+
"graphql": "^14.4.2",
15+
"pg": "^7.12.1",
16+
"reflect-metadata": "^0.1.13",
17+
"ts-node-dev": "^1.0.0-pre.40",
18+
"type-graphql": "^0.17.4",
19+
"typeorm": "^0.2.18"
20+
},
21+
"devDependencies": {
22+
"@types/express": "^4.17.0",
23+
"@types/graphql": "^14.2.3",
24+
"@types/node": "^12.7.1",
25+
"nodemon": "^1.19.1",
26+
"ts-node": "^8.3.0",
27+
"typescript": "^3.5.3"
28+
}
29+
}

src/entity/User.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";
2+
import { ObjectType, Field, ID } from "type-graphql";
3+
4+
@ObjectType()
5+
@Entity()
6+
export class User extends BaseEntity {
7+
@Field(() => ID)
8+
@PrimaryGeneratedColumn()
9+
id: number;
10+
11+
@Field()
12+
@Column()
13+
firstName: string;
14+
15+
@Field()
16+
@Column()
17+
lastName: string;
18+
19+
@Field()
20+
@Column("text", { unique: true })
21+
email: string;
22+
23+
@Column()
24+
password: string;
25+
26+
@Field()
27+
name: string;
28+
}

src/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import "reflect-metadata";
2+
import { ApolloServer } from "apollo-server-express";
3+
import Express from "express";
4+
import { buildSchema } from "type-graphql";
5+
import { createConnection } from "typeorm";
6+
import { RegisterResolver } from "./modules/user/register";
7+
8+
const startServer = async () => {
9+
await createConnection();
10+
11+
const schema = await buildSchema({
12+
resolvers: [RegisterResolver]
13+
});
14+
15+
const apolloServer = new ApolloServer({ schema });
16+
17+
const app = Express();
18+
19+
apolloServer.applyMiddleware({ app });
20+
app.listen(5000, () => {
21+
console.log("server started on http://localhost:5000/graphql");
22+
});
23+
};
24+
25+
startServer();

src/modules/user/register.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {
2+
Resolver,
3+
Query,
4+
Mutation,
5+
Arg,
6+
FieldResolver,
7+
Root
8+
} from "type-graphql";
9+
import { User } from "../../entity/User";
10+
import { RegisterUserInpput } from "./types";
11+
12+
@Resolver(User)
13+
export class RegisterResolver {
14+
@Query(() => String)
15+
async hello() {
16+
return "Hello World";
17+
}
18+
19+
@Query(()=> User)
20+
async getUser(){
21+
22+
}
23+
24+
@FieldResolver()
25+
async name(@Root() parent: User) {
26+
return `${parent.lastName} ${parent.firstName}`;
27+
}
28+
29+
@Mutation(() => User)
30+
async register(@Arg("user") userInput: RegisterUserInpput): Promise<User> {
31+
const user = await User.create(userInput).save();
32+
return user;
33+
}
34+
}

src/modules/user/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { InputType, Field } from "type-graphql";
2+
import { User } from "../../entity/User";
3+
4+
@InputType({ description: "register user input" })
5+
export class RegisterUserInpput implements Partial<User> {
6+
@Field()
7+
firstName: string;
8+
@Field()
9+
lastName: string;
10+
@Field()
11+
email: string;
12+
@Field()
13+
password: string;
14+
}

tsconfig.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "commonjs",
5+
"lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
6+
"sourceMap": true,
7+
"outDir": "./dist",
8+
"moduleResolution": "node",
9+
"removeComments": true,
10+
"noImplicitAny": true,
11+
"strictNullChecks": true,
12+
"strictFunctionTypes": true,
13+
"noImplicitThis": true,
14+
"noUnusedLocals": true,
15+
"noUnusedParameters": true,
16+
"noImplicitReturns": true,
17+
"noFallthroughCasesInSwitch": true,
18+
"esModuleInterop": true,
19+
"allowSyntheticDefaultImports": true,
20+
"emitDecoratorMetadata": true,
21+
"experimentalDecorators": true
22+
},
23+
"exclude": ["node_modules"],
24+
"include": ["./src/**/*.tsx", "./src/**/*.ts"]
25+
}

tslint.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"defaultSeverity": "error",
3+
"extends": ["tslint:latest", "tslint-config-prettier"],
4+
"jsRules": {},
5+
"rules": {
6+
"no-console": false,
7+
"member-access": false,
8+
"object-literal-sort-keys": false,
9+
"ordered-imports": false,
10+
"interface-name": false
11+
},
12+
"rulesDirectory": []
13+
}

0 commit comments

Comments
 (0)