Skip to content
Draft
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
14 changes: 13 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
extends: [
'plugin:jest/recommended',
'standard',
'prettier',
],
parser: '@typescript-eslint/parser',
parserOptions: {
Expand All @@ -16,6 +17,12 @@ module.exports = {
'@typescript-eslint',
],
rules: {
quotes: ['error', 'single', { allowTemplateLiterals: true }],
indent: ['error', 2, {
SwitchCase: 1,
ignoredNodes: ['PropertyDefinition'],
}],
'no-useless-constructor': 'off',
'comma-dangle': ['error', {
arrays: 'always-multiline',
objects: 'always-multiline',
Expand All @@ -28,6 +35,11 @@ module.exports = {
'react/react-in-jsx-scope': 'off',
'react/prop-types': 'off',
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': ['error', { ignoreRestSiblings: true }],
'@typescript-eslint/no-unused-vars': [
'off', {
ignoreRestSiblings: true,
argsIgnorePattern: '^_',
},
],
},
}
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
.eslintrc.js
8 changes: 8 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"semi": false,
"singleQuote": true,
"jsxSingleQuote": true,
"quoteProps": "preserve",
"trailingComma": "all",
"arrowParens": "avoid"
}
70 changes: 39 additions & 31 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"start": "node dist/src/index.js",
"dev": "yarn docker:down && yarn docker:up && yarn server",
"server": "nodemon --exec sucrase-node src/index.ts",
"server": "ts-node-dev --respawn --transpile-only src/index.ts",
"docker:up": "docker-compose up -d",
"docker:down": "docker-compose down",
"docker:destroy": "docker-compose down --volumes --remove-orphans",
Expand All @@ -31,44 +31,52 @@
"update-deps": "sucrase-node ./src/config/update-deps.ts"
},
"dependencies": {
"@prisma/client": "3.3.0",
"argon2": "0.28.2",
"@fastify/cors": "8.1.0",
"@prisma/client": "4.2.0",
"apollo-server": "3.10.0",
"argon2": "0.28.7",
"class-validator": "0.13.2",
"cors": "2.8.5",
"dotenv": "10.0.0",
"express": "4.17.1",
"fastify": "3.22.1",
"fastify-cors": "6.0.2",
"fp-ts": "2.11.5",
"io-ts": "2.2.16",
"dotenv": "16.0.1",
"express": "4.18.1",
"fastify": "4.4.0",
"fp-ts": "2.12.2",
"graphql": "15",
"graphql-relay": "0.10.0",
"io-ts": "2.2.17",
"io-ts-types": "0.5.16",
"jose": "4.1.1",
"jose": "4.8.3",
"module-alias": "2.2.2",
"monocle-ts": "2.3.11",
"newtype-ts": "0.3.4",
"slugify": "1.6.1",
"monocle-ts": "2.3.13",
"newtype-ts": "0.3.5",
"reflect-metadata": "0.1.13",
"slugify": "1.6.5",
"type-graphql": "1.1.1",
"uuid": "8.3.2"
},
"devDependencies": {
"@types/cors": "2.8.12",
"@types/express": "4.17.13",
"@types/jest": "27.0.2",
"@types/jest": "28.1.6",
"@types/module-alias": "2.0.1",
"@types/node": "16.11.4",
"@types/uuid": "8.3.1",
"@typescript-eslint/eslint-plugin": "5.1.0",
"@typescript-eslint/parser": "5.1.0",
"eslint": "8.1.0",
"eslint-config-standard": "16.0.3",
"eslint-plugin-import": "2.25.2",
"eslint-plugin-jest": "25.2.2",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "5.1.1",
"husky": "7.0.4",
"jest": "27.3.1",
"nodemon": "2.0.14",
"prisma": "3.3.0",
"sucrase": "3.20.3",
"ts-jest": "27.0.7",
"typescript": "4.4.4"
"@types/node": "18.6.5",
"@types/uuid": "8.3.4",
"@typescript-eslint/eslint-plugin": "5.33.0",
"@typescript-eslint/parser": "5.33.0",
"eslint": "8.21.0",
"eslint-config-prettier": "8.8.0",
"eslint-config-standard": "17.0.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-jest": "26.8.2",
"eslint-plugin-n": "15.2.4",
"eslint-plugin-promise": "6.0.0",
"husky": "8.0.1",
"jest": "28.1.3",
"prettier": "2.8.8",
"prisma": "4.2.0",
"sucrase": "3.25.0",
"ts-jest": "28.0.7",
"ts-node-dev": "2.0.0",
"typescript": "4.7.4"
}
}
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ model User {
model Follower {
userId String
followingId String
whoIsFollowing User @relation("Follower", fields: [followingId], references: [id])
following User @relation("Following", fields: [userId], references: [id])
whoIsFollowing User @relation("Follower", fields: [followingId], references: [id])
following User @relation("Following", fields: [userId], references: [id])

@@id([userId, followingId])
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Envs =
| 'PORT'
| 'JWT_SECRET'

export const env = (value: Envs) => {
export const env = (value: Envs): string => {
const envCodec = withMessage(
NonEmptyString,
() => `You must set the env var ${value}`,
Expand Down
2 changes: 1 addition & 1 deletion src/ports/adapters/http/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ type GetUserResponse = (input: GetUserResponseInput) => UserResponse
const getUserResponse: GetUserResponse = ({ user, token }) => ({
user: {
email: user.email,
token: token,
token,
username: user.username,
bio: user.bio ?? '',
image: user.image ?? '',
Expand Down
2 changes: 1 addition & 1 deletion src/ports/adapters/http/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { start } from '@/ports/fastify'
import { start } from '@/ports/apollo-server'

start()
47 changes: 47 additions & 0 deletions src/ports/apollo-server/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ApolloError } from 'apollo-server'
import { getError } from '@/ports/adapters/http/http'

const errors: Record<number, { code: string, name: string }> = {
401: {
code: 'UNAUTHORIZED',
name: 'AuthError',
},
403: {
code: 'FORBIDDEN',
name: 'ForbiddenError',
},
404: {
code: 'NOT_FOUND',
name: 'NotFoundError',
},
422: {
code: 'VALIDATION_ERROR',
name: 'ValidationError',
},
418: {
code: 'UnknownError',
name: 'UNKNOWN_ERROR',
},
}

export type GraphQLErrorInput = ReturnType<typeof getError>

export class GraphQLError extends ApolloError {
constructor (errorObject: GraphQLErrorInput) {
const errorBody = errorObject.error.errors.body
const message = errorBody.length > 1
? JSON.stringify(errorBody)
: errorBody.toString()
const errorData = errors[errorObject.code]
const code = errorData?.code ?? 'GRAPHQL_ERROR'
const name = errorData?.name ?? 'GraphQLError'

super(message, code)
Object.defineProperty(this, 'name', { value: name })

if (code === 'VALIDATION_ERROR') {
const errorsKey = 'errors'
this[errorsKey] = errorBody
}
}
}
56 changes: 56 additions & 0 deletions src/ports/apollo-server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'reflect-metadata'
// import { join } from 'node:path'
import { ApolloServer } from 'apollo-server'
import { ApolloServerPluginLandingPageLocalDefault } from 'apollo-server-core'
import { buildSchema } from 'type-graphql'
import { env } from '@/helpers'

import { Article } from './modules/article/article.type'
import { repositories, authChecker } from './server'

import { ArticleResolver } from './modules/article/article.resolver'
import { CommentResolver } from './modules/comment/comment.resolver'
import { ProfileResolver } from './modules/profile/profile.resolver'
import { TagResolver } from './modules/tag/tag.resolver'
import { UserResolver } from './modules/user/user.resolver'
import { NodeResolver } from './relay/node.resolver'

const PORT = env('PORT')

export async function start () {
const schema = await buildSchema({
orphanedTypes: [
Article,
],
// resolvers: [join(__dirname, '{modules,relay}', '**', '*.resolver.ts')],
resolvers: [
ArticleResolver,
CommentResolver,
ProfileResolver,
TagResolver,
UserResolver,
NodeResolver,
],
// emitSchemaFile: true,
authChecker,
})

const server = new ApolloServer({
schema,
context: async ({ req }) => {
return {
repositories,
req,
}
},
csrfPrevention: true,
cache: 'bounded',
plugins: [
ApolloServerPluginLandingPageLocalDefault({ embed: true }),
],
})

return server.listen({ port: PORT }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
})
}
55 changes: 55 additions & 0 deletions src/ports/apollo-server/modules/article/article.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Field, InputType } from 'type-graphql'

@InputType()
export class CreateArticleInput {
@Field()
title: string

@Field()
description: string

@Field()
body: string

@Field(_type => [String], { nullable: true })
tagList?: string[]
}

@InputType()
export class UpdateArticleInput {
@Field()
slug: string

@Field({ nullable: true })
title?: string

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

@Field({ nullable: true })
body?: string
}

@InputType()
class ArticlesFeedFilterInput {
@Field({ nullable: true })
author?: string

@Field({ nullable: true })
tag?: string

@Field({ nullable: true })
favorited?: string

@Field({ nullable: true })
limit?: number

@Field({ nullable: true })
offset?: number
}

@InputType()
export class ArticlesFeedInput {
@Field(_type => ArticlesFeedFilterInput, { nullable: true })
filter?: ArticlesFeedFilterInput
}
Loading