Skip to content

Commit

Permalink
feat: improved fastify-graphql version
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLzq committed Jun 26, 2022
1 parent a8216a4 commit 9662720
Show file tree
Hide file tree
Showing 14 changed files with 870 additions and 875 deletions.
10 changes: 10 additions & 0 deletions example/fastify-graphql/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.eslintignore
.eslintrc
.gitignore
CHANGELOG.md
Dockerfile
heroku.yml
*.http
LICENSE
nodemon.json
README.md
2 changes: 0 additions & 2 deletions example/fastify-graphql/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ RUN yarn build

RUN yarn remove webpack webpack-node-externals tsconfig-paths-webpack-plugin

COPY dist /app/dist

CMD [ "yarn", "start" ]
48 changes: 24 additions & 24 deletions example/fastify-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,44 @@
"author": "AnthonyLzq <sluzquinosa@uni.pe>",
"license": "MIT",
"dependencies": {
"@fastify/cors": "^7.0.0",
"@fastify/swagger": "^6.0.0",
"@graphql-tools/schema": "^8.3.10",
"@fastify/cors": "^7",
"@fastify/swagger": "^6",
"@graphql-tools/schema": "^8.4.0",
"@sinclair/typebox": "^0.23.5",
"ajv": "^8.11.0",
"ajv-formats": "^2.1.1",
"apollo-server-core": "^3.6.7",
"apollo-server-fastify": "^3.6.7",
"apollo-server-plugin-base": "^3.5.2",
"fastify": "^3.29.0",
"graphql": "^16.4.0",
"apollo-server-core": "^3.9.0",
"apollo-server-fastify": "^3.9.0",
"apollo-server-plugin-base": "^3.6.1",
"fastify": "^3",
"graphql": "^16.5.0",
"http-errors": "^2.0.0",
"mongoose": "^6.3.1",
"pino-pretty": "^7.6.1"
"mongoose": "^6.4.0",
"pino-pretty": "^8.1.0"
},
"devDependencies": {
"@types/http-errors": "^1.8.2",
"@types/node": "^17.0.30",
"@typescript-eslint/eslint-plugin": "^5.21.0",
"@typescript-eslint/parser": "^5.21.0",
"dotenv": "^16.0.0",
"eslint": "^8.14.0",
"@types/node": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.29.0",
"@typescript-eslint/parser": "^5.29.0",
"dotenv": "^16.0.1",
"eslint": "^8.18.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^6.0.0",
"nodemon": "^2.0.16",
"prettier": "^2.6.2",
"standard-version": "^9.3.2",
"ts-loader": "^9.3.0",
"ts-node": "^10.7.0",
"tsconfig-paths": "^3.14.1",
"nodemon": "^2.0.18",
"prettier": "^2.7.1",
"standard-version": "^9.5.0",
"ts-loader": "^9.3.1",
"ts-node": "^10.8.1",
"tsconfig-paths": "^4.0.0",
"tsconfig-paths-webpack-plugin": "^3.5.2",
"typescript": "^4.6.4",
"webpack": "^5.72.0",
"webpack-cli": "^4.9.2",
"typescript": "^4.7.4",
"webpack": "^5.73.0",
"webpack-cli": "^4.10.0",
"webpack-node-externals": "^3.0.0"
}
}
1 change: 0 additions & 1 deletion example/fastify-graphql/src/@types/models/user.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
interface UserDBO {
id: string
name: string
lastName: string
createdAt: Date
Expand Down
11 changes: 6 additions & 5 deletions example/fastify-graphql/src/database/mongo/queries/user.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Document, Types } from 'mongoose'
import { Document, MergeType, Types } from 'mongoose'

import { UserModel } from '..'
import { UserDTO } from 'schemas'
import { User, UserDTO, UserWithId } from 'schemas'

const userDBOtoDTO = (
userDBO: Document<unknown, unknown, UserDBO> &
userDBO: Document<unknown, unknown, MergeType<UserDBO, UserDBO>> &
Omit<UserDBO, keyof UserDBO> &
UserDBO & {
_id: Types.ObjectId
}
Expand All @@ -14,7 +15,7 @@ const userDBOtoDTO = (
updatedAt: userDBO.updatedAt.toISOString()
})

const store = async (userData: UserDTO): Promise<UserDTO> => {
const store = async (userData: User): Promise<UserDTO> => {
const user = new UserModel(userData)

await user.save()
Expand Down Expand Up @@ -50,7 +51,7 @@ const get = async (
return users.map(u => userDBOtoDTO(u))
}

const update = async (userData: UserDTO): Promise<UserDTO | null> => {
const update = async (userData: UserWithId): Promise<UserDTO | null> => {
const { id, ...rest } = userData
const user = await UserModel.findByIdAndUpdate(id, rest, { new: true })

Expand Down
6 changes: 3 additions & 3 deletions example/fastify-graphql/src/graphQL/models/User/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ApolloError } from 'apollo-server-core'

import { store, remove, update } from 'database'
import { UserDTO } from 'schemas'
import { User, UserDTO, UserWithId } from 'schemas'
import { EFU, MFU, GE, errorHandling } from '../utils'

const storeUser = async (
{ user }: { user: UserDTO },
{ user }: { user: User },
{ log }: Context
): Promise<UserDTO> => {
try {
Expand Down Expand Up @@ -43,7 +43,7 @@ const deleteAllUsers = async ({ log }: Context): Promise<string> => {
}

const updateUser = async (
{ user }: { user: UserDTO },
{ user }: { user: UserWithId },
{ log }: Context
): Promise<UserDTO> => {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { DefinedError } from 'ajv'
import { ApolloError } from 'apollo-server-core'

import { ajv, idSchema, UserDTO } from 'schemas'
import { storeUserSchema, updateUserSchema } from './schemas'
import {
ajv,
idSchema,
User,
user as storeUserSchema,
UserDTO,
UserWithId,
userWithId as updateUserSchema
} from 'schemas'
import { storeUser, updateUser, deleteUser, deleteAllUsers } from './mutations'
import { errorHandling, GE } from '../utils'

const Mutation = {
storeUser: async (
parent: unknown,
{ user }: { user: UserDTO },
{ user }: { user: User },
context: Context
): Promise<UserDTO> => {
const { log } = context
Expand Down Expand Up @@ -41,7 +48,7 @@ const Mutation = {
},
updateUser: async (
parent: unknown,
{ user }: { user: UserDTO },
{ user }: { user: UserWithId },
context: Context
): Promise<UserDTO> => {
const validate = ajv.compile(updateUserSchema)
Expand Down
20 changes: 0 additions & 20 deletions example/fastify-graphql/src/graphQL/models/User/schemas.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const User = gql`
id: ID!
name: String!
lastName: String!
createdAt: String!
updatedAt: String!
}
Expand Down
13 changes: 12 additions & 1 deletion example/fastify-graphql/src/network/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class Server {
#connection: mongoose.Connection | undefined

constructor() {
this.#app = fastify({ logger: { prettyPrint: true } })
this.#app = fastify({
logger: { prettyPrint: process.env.NODE_ENV !== 'production' }
})
this.#config()
}

Expand Down Expand Up @@ -115,6 +117,15 @@ class Server {
console.error(e)
}
}

public async stop(): Promise<void> {
try {
await this.#app.close()
process.exit(0)
} catch (e) {
console.error(e)
}
}
}

const server = new Server()
Expand Down
21 changes: 20 additions & 1 deletion example/fastify-graphql/src/schemas/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const user = Type.Object({

type User = Static<typeof user>

const userWithId = Type.Intersect([user, Type.Object({ id })])

type UserWithId = Static<typeof userWithId>

const userDto = Type.Object({
id: Type.Optional(id),
lastName: Type.String(),
Expand All @@ -19,4 +23,19 @@ const userDto = Type.Object({

type UserDTO = Static<typeof userDto>

export { userDto, UserDTO, user, User }
const storeUserDto = Type.Object({
args: user
})

type StoreUserDTO = Static<typeof storeUserDto>

export {
userDto,
UserDTO,
userWithId,
UserWithId,
user,
User,
storeUserDto,
StoreUserDTO
}
Loading

0 comments on commit 9662720

Please sign in to comment.