commit 53b620ec9b32845deeddb8a79135e7b2cf0f3181 Author: mpabi Date: Mon Mar 24 13:39:46 2025 +0000 init diff --git a/doc/main.pdf b/doc/main.pdf new file mode 100644 index 0000000..376da37 Binary files /dev/null and b/doc/main.pdf differ diff --git a/doc/main.tex b/doc/main.tex new file mode 100644 index 0000000..01435da --- /dev/null +++ b/doc/main.tex @@ -0,0 +1,179 @@ +\documentclass{article} +\usepackage[utf8]{inputenc} +\usepackage{listings} +\usepackage{xcolor} + +% Configure colors for code +\definecolor{codegreen}{rgb}{0,0.6,0} +\definecolor{codegray}{rgb}{0.5,0.5,0.5} +\definecolor{codepurple}{rgb}{0.58,0,0.82} +\definecolor{backcolour}{rgb}{0.95,0.95,0.92} + +% Code listing style +\lstdefinestyle{mystyle}{ + backgroundcolor=\color{backcolour}, + commentstyle=\color{codegreen}, + keywordstyle=\color{magenta}, + stringstyle=\color{codepurple}, + basicstyle=\ttfamily\footnotesize, + breakatwhitespace=false, + breaklines=true, + captionpos=b, + keepspaces=true, + numbers=left, + numbersep=5pt, + showspaces=false, + showstringspaces=false, + showtabs=false, + tabsize=2 +} + +\lstset{style=mystyle} + +\title{Instructions for Integrating Fastify, GraphQL, and TypeORM} +\author{} +\date{} + +\begin{document} + +\maketitle + +\section*{Step 1: Install Dependencies} +Install the required packages using npm: +\begin{lstlisting}[language=bash] +npm install fastify mercurius typeorm reflect-metadata sqlite3 graphql +\end{lstlisting} + +\section*{Step 2: Configure TypeORM} +Create a file named \texttt{ormconfig.json} with the following content: +\begin{lstlisting}[language=json] +{ + "type": "sqlite", + "database": "./db.sqlite", + "synchronize": true, + "logging": false, + "entities": ["src/entity/**/*.ts"], + "migrations": ["src/migration/**/*.ts"], + "subscribers": ["src/subscriber/**/*.ts"] +} +\end{lstlisting} + +Next, create entities in the \texttt{src/entity} folder. Example of \texttt{User.ts}: +\begin{lstlisting}[language=typescript] +import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm'; +import { Post } from './Post'; + +@Entity() +export class User { + @PrimaryGeneratedColumn() + id: number; + + @Column() + name: string; + + @OneToMany(() => Post, (post) => post.author) + posts: Post[]; +} +\end{lstlisting} + +Example of \texttt{Post.ts}: +\begin{lstlisting}[language=typescript] +import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm'; +import { User } from './User'; + +@Entity() +export class Post { + @PrimaryGeneratedColumn() + id: number; + + @Column() + title: string; + + @ManyToOne(() => User, (user) => user.posts) + author: User; +} +\end{lstlisting} + +\section*{Step 3: Configure Fastify with GraphQL} +Create a file named \texttt{index.ts} with the following content: +\begin{lstlisting}[language=typescript] +import 'reflect-metadata'; +import { createConnection } from 'typeorm'; +import fastify from 'fastify'; +import mercurius from 'mercurius'; +import { User } from './entity/User'; +import { Post } from './entity/Post'; + +const app = fastify(); + +const schema = ` + type User { + id: ID! + name: String! + posts: [Post!]! + } + + type Post { + id: ID! + title: String! + author: User! + } + + type Query { + user(id: ID!): User + } +`; + +const resolvers = { + Query: { + user: async (_, { id }) => { + return await User.findOne({ where: { id }, relations: ['posts'] }); + }, + }, +}; + +const startServer = async () => { + await createConnection(); + + app.register(mercurius, { + schema, + resolvers, + graphiql: true, + }); + + app.listen({ port: 3000 }, (err, address) => { + if (err) { + console.error(err); + process.exit(1); + } + console.log(`🚀 Server ready at ${address}`); + }); +}; + +startServer(); +\end{lstlisting} + +\section*{Step 4: Testing} +1. Start the server: +\begin{lstlisting}[language=bash] +npx ts-node index.ts +\end{lstlisting} + +2. Open your browser at: +\begin{lstlisting}[language=text] +http://localhost:3000/graphiql +\end{lstlisting} + +3. Send a sample GraphQL query: +\begin{lstlisting}[language=graphql] +query { + user(id: 1) { + name + posts { + title + } + } +} +\end{lstlisting} + +\end{document}