Skip to content

Commit

Permalink
feature: Places interface graphql terminated
Browse files Browse the repository at this point in the history
  • Loading branch information
ldiego73 committed Jun 11, 2020
1 parent 94c23e7 commit ecb8e25
Show file tree
Hide file tree
Showing 38 changed files with 646 additions and 58 deletions.
2 changes: 2 additions & 0 deletions libs/server-graphql/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./exceptions";
export * from "./filters";
export * from "./server";
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { Field, ObjectType } from "@nestjs/graphql";

@ObjectType("Country")
export class CountrySchema {
@Field()
name!: string;
@Field({ nullable: true })
name?: string;

@Field()
iso!: string;
@Field({ nullable: true })
iso?: string;

@Field()
currency!: string;
@Field({ nullable: true })
currency?: string;

@Field()
status!: boolean;
@Field({ nullable: true })
status?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export class CreateResolver extends BaseResolver {
} catch (err) {
switch (err.constructor) {
case IsoInvalidError:
this.bad(err.message, err.code);
return this.bad(err.message, err.code);
case CountryInvalidError:
this.bad(err.message, err.code);
return this.bad(err.message, err.code);
case CountryAlreadyExistsError:
this.conflict(err.message, err.code);
return this.conflict(err.message, err.code);
case UseCaseUnexpectedError:
this.fail(err.message, err.code);
return this.fail(err.message, err.code);
default:
throw err;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export class DeleteResolver extends BaseResolver {
} catch (err) {
switch (err.constructor) {
case IsoInvalidError:
this.bad(err.message, err.code);
return this.bad(err.message, err.code);
case UseCaseUnexpectedError:
this.fail(err.message, err.code);
return this.fail(err.message, err.code);
default:
throw err;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export class FindResolver extends BaseResolver {
} catch (err) {
switch (err.constructor) {
case IsoInvalidError:
this.bad(err.message, err.code);
return this.bad(err.message, err.code);
case CountryNotFoundError:
this.notFound(err.message, err.code);
return this.notFound(err.message, err.code);
case UseCaseUnexpectedError:
this.fail(err.message, err.code);
return this.fail(err.message, err.code);
default:
throw err;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ export class UpdateResolver extends BaseResolver {
} catch (err) {
switch (err.constructor) {
case IsoInvalidError:
this.bad(err.message, err.code);
break;
return this.bad(err.message, err.code);
case CountryNotExistsError:
this.notFound(err.message, err.code);
break;
return this.notFound(err.message, err.code);
case UseCaseUnexpectedError:
this.fail(err.message, err.code);
break;
return this.fail(err.message, err.code);
default:
throw err;
}
Expand Down
10 changes: 5 additions & 5 deletions microservices/countries/interfaces/graphql/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
# ------------------------------------------------------

type Country {
name: String!
iso: String!
currency: String!
status: Boolean!
name: String
iso: String
currency: String
status: Boolean
}

type Query {
countries: [Country!]!
country(iso: String!): Country!
countries: [Country!]!
}

type Mutation {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQlExceptionFilter } from "@micro/server";
import { GraphQlExceptionFilter } from "@micro/server-graphql";
import { INestApplication } from "@nestjs/common";
import { FastifyAdapter } from "@nestjs/platform-fastify";
import { Test } from "@nestjs/testing";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ export class CreateController extends BaseController {
} catch (err) {
switch (err.constructor) {
case IsoInvalidError:
this.bad(err.message, err.code);
break;
return this.bad(err.message, err.code);
case CountryInvalidError:
this.bad(err.message, err.code);
break;
return this.bad(err.message, err.code);
case CountryAlreadyExistsError:
this.conflict(err.message, err.code);
break;
return this.conflict(err.message, err.code);
case UseCaseUnexpectedError:
this.fail(err.message, err.code);
break;
return this.fail(err.message, err.code);
default:
throw err;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ export class DeleteController extends BaseController {
} catch (err) {
switch (err.constructor) {
case IsoInvalidError:
this.bad(err.message, err.code);
break;
return this.bad(err.message, err.code);
case UseCaseUnexpectedError:
this.fail(err.message, err.code);
break;
return this.fail(err.message, err.code);
default:
throw err;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ export class FindController extends BaseController {
} catch (err) {
switch (err.constructor) {
case IsoInvalidError:
this.bad(err.message, err.code);
break;
return this.bad(err.message, err.code);
case CountryNotFoundError:
this.notFound(err.message, err.code);
break;
return this.notFound(err.message, err.code);
case UseCaseUnexpectedError:
this.fail(err.message, err.code);
return this.fail(err.message, err.code);
default:
throw err;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ export class ListController extends BaseController {
try {
return await this.service.execute();
} catch (err) {
switch (err.constructor) {
case UseCaseUnexpectedError:
this.fail(err.message, err.code);
break;
default:
throw err;
if (err.constructor === UseCaseUnexpectedError) {
this.fail(err.message, err.code);
} else {
throw err;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ export class UpdateController extends BaseController {
} catch (err) {
switch (err.constructor) {
case IsoInvalidError:
this.bad(err.message, err.code);
break;
return this.bad(err.message, err.code);
case CountryNotExistsError:
this.notFound(err.message, err.code);
break;
return this.notFound(err.message, err.code);
case UseCaseUnexpectedError:
this.fail(err.message, err.code);
break;
return this.fail(err.message, err.code);
default:
throw err;
}
Expand Down
12 changes: 12 additions & 0 deletions microservices/places/interfaces/graphql/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = off
trim_trailing_whitespace = false
20 changes: 20 additions & 0 deletions microservices/places/interfaces/graphql/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
parser: "@typescript-eslint/parser"
parserOptions:
project: "tsconfig.json"
sourceType: "module"
plugins:
- "@typescript-eslint/eslint-plugin"
- "simple-import-sort"
extends:
- plugin:@typescript-eslint/eslint-recommended
- plugin:@typescript-eslint/recommended
- prettier
- prettier/@typescript-eslint
env:
node: true
jest: true
rules:
"@typescript-eslint/interface-name-prefix": 0
"@typescript-eslint/explicit-function-return-type": 0
"@typescript-eslint/no-explicit-any": 0
"simple-import-sort/sort": 2
5 changes: 5 additions & 0 deletions microservices/places/interfaces/graphql/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
reports/
lib/
cert/
.env
7 changes: 7 additions & 0 deletions microservices/places/interfaces/graphql/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"endOfLine": "auto",
"semi": true,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5"
}
9 changes: 9 additions & 0 deletions microservices/places/interfaces/graphql/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2020 Luis Diego

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 changes: 19 additions & 0 deletions microservices/places/interfaces/graphql/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"transform": {
"^.+\\.ts?$": "ts-jest"
},
"testRegex": ".spec.ts$",
"testEnvironment": "node",
"moduleFileExtensions": [
"ts",
"js",
"json"
],
"collectCoverage": true,
"coverageReporters": [
"json",
"text",
"html"
],
"coverageDirectory": "reports"
}
54 changes: 54 additions & 0 deletions microservices/places/interfaces/graphql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@micro/places-graphql",
"version": "1.0.0",
"description": "Places GraphQL Interface",
"author": {
"name": "Luis Diego",
"email": "[email protected]",
"url": "http://github.com/ldiego73"
},
"license": "MIT",
"main": "lib/app.js",
"types": "lib/app.d.ts",
"files": [
"lib/"
],
"scripts": {
"clean": "rimraf lib",
"format": "prettier --write \"src/**/*.ts\" --loglevel silent",
"lint": "eslint \"{src,test}/**/*.ts\" -f stylish",
"develop": "nest start --watch",
"prebuild": "yarn clean",
"build": "nest build",
"test": "jest --passWithNoTests"
},
"dependencies": {
"@micro/places-config": "^1.0.0",
"@micro/places-core": "^1.0.0",
"@micro/kernel": "^1.0.0",
"@micro/logger": "^1.0.0",
"@micro/server": "^1.0.0",
"@micro/server-graphql": "^1.0.0",
"fastify-cors": "^3.0.3",
"fastify-rate-limit": "^3.0.1"
},
"devDependencies": {
"@nestjs/cli": "^7.2.0",
"@nestjs/testing": "^7.1.3",
"@types/node": "^14.0.13",
"@types/supertest": "^2.0.9",
"@typescript-eslint/eslint-plugin": "^3.2.0",
"@typescript-eslint/parser": "^3.2.0",
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-simple-import-sort": "^5.0.3",
"jest": "^26.0.1",
"prettier": "^2.0.5",
"rimraf": "^3.0.2",
"supertest": "^4.0.2",
"ts-jest": "^26.1.0",
"ts-node": "^8.10.2",
"typescript": "^3.9.5"
}
}
25 changes: 25 additions & 0 deletions microservices/places/interfaces/graphql/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint @typescript-eslint/no-non-null-assertion:0 */

import { MicroApplication } from "@micro/kernel";
import { readEnv } from "@micro/places-config";
import { Server } from "@micro/server-graphql";
import cors from "fastify-cors";
import rateLimit from "fastify-rate-limit";

import { AppModule } from "./modules/app.module";

export class PlaceGraphqlApplication extends MicroApplication {
async start(): Promise<void> {
const env = readEnv();
const { key, cert } = env.server.https!;
const server = Server.create(AppModule, {
port: env.server.port,
https: { key, cert },
});

server.register(cors);
server.register(rateLimit, { max: 100, timeWindow: "1 minute" });

await server.start();
}
}
3 changes: 3 additions & 0 deletions microservices/places/interfaces/graphql/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { PlaceGraphqlApplication } from "./app";

new PlaceGraphqlApplication().start();
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Field, ObjectType } from "@nestjs/graphql";

@ObjectType("Location")
export class LocationSchema {
@Field({ nullable: true })
lat?: string;

@Field({ nullable: true })
lng?: string;
}

@ObjectType("Address")
export class AddressSchema {
@Field({ nullable: true })
id?: string;

@Field({ nullable: true })
country?: string;

@Field({ nullable: true })
description?: string;

@Field((type) => LocationSchema, { nullable: true })
latLng?: LocationSchema;
}
Loading

0 comments on commit ecb8e25

Please sign in to comment.