Skip to content

Commit 1b4d92c

Browse files
committed
added graphql support
1 parent 15c1a91 commit 1b4d92c

File tree

16 files changed

+2133
-318
lines changed

16 files changed

+2133
-318
lines changed

package-lock.json

Lines changed: 2000 additions & 300 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,33 @@
1717
"webpack": "webpack --config webpack.config.js"
1818
},
1919
"dependencies": {
20-
"@nestjs/common": "^5.0.0",
21-
"@nestjs/core": "^5.0.0",
20+
"@nestjs/common": "^5.3.7",
21+
"@nestjs/core": "^5.3.7",
22+
"@nestjs/graphql": "^5.3.1",
23+
"@nestjs/jwt": "^0.1.3",
24+
"@nestjs/passport": "^5.1.0",
25+
"@nestjs/swagger": "^2.5.1",
26+
"@nestjs/websockets": "^5.3.7",
27+
"@types/joi": "^13.4.5",
28+
"apollo-server-express": "^2.0.7",
2229
"arangojs": "^6.6.0",
30+
"cache-manager": "^2.9.0",
31+
"class-validator": "^0.9.1",
32+
"compression": "^1.7.3",
33+
"csurf": "^1.9.0",
34+
"dotenv": "^6.0.0",
35+
"express-rate-limit": "^3.1.1",
2336
"fastify-formbody": "^2.0.0",
37+
"graphql": "^14.0.2",
38+
"graphql-tools": "^3.1.1",
39+
"graphql-type-json": "^0.2.1",
40+
"hbs": "^4.0.1",
41+
"helmet": "^3.13.0",
42+
"joi": "^13.6.0",
43+
"mqtt": "^2.18.8",
44+
"passport": "^0.4.0",
45+
"passport-http-bearer": "^1.0.1",
46+
"passport-jwt": "^4.0.0",
2447
"reflect-metadata": "^0.1.12",
2548
"rxjs": "^6.0.0",
2649
"typescript": "^2.6.2"

src/app.module.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
import { Module } from '@nestjs/common';
2+
import { GraphQLModule } from '@nestjs/graphql';
3+
import * as GraphQLJSON from 'graphql-type-json';
4+
import { join } from 'path';
25
import { AppController } from './app.controller';
36
import { AppService } from './app.service';
47
import { UsersModule } from './users/users.module';
58
import { ConversationsModule } from './conversations/conversations.module';
69
import { MessagesModule } from './messages/messages.module';
7-
import { ControllersService } from './controllers/controllers.service';
10+
import { AuthModule } from './auth/auth.module';
11+
import { CommonsModule } from './commons/commons.module';
812

913
@Module({
10-
imports: [UsersModule, ConversationsModule, MessagesModule],
14+
imports: [
15+
UsersModule,
16+
ConversationsModule,
17+
MessagesModule,
18+
AuthModule,
19+
CommonsModule,
20+
GraphQLModule.forRoot({
21+
typePaths: ['./**/*.graphql'],
22+
context: ({ req }) => ({ req }),
23+
resolvers: { JSON: GraphQLJSON },
24+
installSubscriptionHandlers: true,
25+
definitions: {
26+
path: join(process.cwd(), 'src/graphql.ts'),
27+
},
28+
}),
29+
],
1130
controllers: [AppController],
12-
providers: [AppService, ControllersService],
31+
providers: [AppService],
1332
})
1433
export class AppModule {}

src/auth/auth.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Module } from '@nestjs/common';
2+
3+
@Module({})
4+
export class AuthModule {}

src/auth/guards/gql-auth.guard.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Injectable, ExecutionContext } from '@nestjs/common';
2+
import { AuthGuard } from '@nestjs/passport';
3+
import { GqlExecutionContext } from '@nestjs/graphql';
4+
5+
@Injectable()
6+
export class GqlAuthGuard extends AuthGuard('jwt') {
7+
getRequest(context: ExecutionContext) {
8+
const ctx = GqlExecutionContext.create(context);
9+
return ctx.getContext().req;
10+
}
11+
}

src/commons/commons.module.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Module } from '@nestjs/common';
2+
import { DateTimeScaler } from './scalers/date-time-scaler';
3+
4+
@Module({
5+
providers: [DateTimeScaler],
6+
})
7+
export class CommonsModule {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Injectable, NestMiddleware, MiddlewareFunction } from '@nestjs/common';
2+
3+
@Injectable()
4+
export class LoggerMiddleware implements NestMiddleware {
5+
resolve(...args: any[]): MiddlewareFunction {
6+
return (req, res, next) => {
7+
// tslint:disable-next-line:no-console
8+
console.log('Request...');
9+
next();
10+
};
11+
}
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Scalar } from '@nestjs/graphql';
2+
import { Kind } from 'graphql';
3+
4+
@Scalar('DateTime')
5+
export class DateTimeScaler {
6+
description = 'DateTime custom scalar type';
7+
8+
parseValue(value) {
9+
return new Date(value); // value from the client
10+
}
11+
12+
serialize(value) {
13+
return value.getTime(); // value sent to the client
14+
}
15+
16+
parseLiteral(ast) {
17+
if (ast.kind === Kind.INT) {
18+
return parseInt(ast.value, 10); // ast value is always in string format
19+
}
20+
return null;
21+
}
22+
}

src/conversations/conversations.types.graphql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# import * from '../messages/messages.types.graphql'
2-
# import * from '../shared.graphql'
1+
# import MessageConnection from '../messages/messages.types.graphql'
2+
# import DateTime from '../shared.graphql'
33

4-
type Conversation implements Node {
4+
type Conversation {
55
# Generated id for a user. read-only
66
_id: ID!
77
# A unique identifier for the object.

src/main.hmr.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ declare const module: any;
55

66
async function bootstrap() {
77
const app = await NestFactory.create(AppModule);
8-
await app.listen(3000);
8+
await app.listen(5000);
99

1010
if (module.hot) {
1111
module.hot.accept();
1212
module.hot.dispose(() => app.close());
1313
}
1414
}
15-
bootstrap();
15+
bootstrap();

0 commit comments

Comments
 (0)