Posts

Showing posts with the label pothos

Guide to using Pothos GraphQL

Image
Introduction Pothos GraphQL is a GraphQL Schema Builder , which is a library that helps you write/define a GraphQL Schema using TypeScript code (a Code-First API approach). Functions Helps define each Query , Mutation and GraphQL Type using strongly typed TypeScript code, automatically catching errors to ensure 100% Type Safety . Pothos has a built-in plugin to connect with Drizzle (@pothos/plugin-drizzle) . You only need to declare tables in Drizzle corresponding to Objects in GraphQL and Pothos will automatically understand relations, pagination , helping you avoid writing complex SELECT/JOIN statements. Detail bun add @pothos/core @pothos/plugin-drizzle @pothos/plugin-zod Create file src/drizzle-orm/schema/schema.ts import {sql} from 'drizzle-orm' import { check, decimal, integer, pgTable, serial, text, timestamp, unique, varchar, } from 'drizzle-orm/pg-core' export const baseColumns = { id : serial ( 'id' ). primaryKe...

Resolve N+1 Query Problem

Image
Introduction N+1 Query Problem is a performance issue that occurs when an application executes 1 initial query to fetch a list of N records and then executes N additional sub-queries to fetch related data for each record. The total number of queries sent to the Database will be N + 1 . As N increases (for example N = 1000), the application must execute 1001 SQL statements, causing an I/O bottleneck , increasing latency and overloading the Database . Example Suppose you need to display 10 products (N = 10) along with the list of variants for each product: First Query (1): Get the list of 10 products: SELECT * FROM products LIMIT 10 Next N Queries (N = 10): Iterate through each product to get variants. SELECT * FROM product_variants WHERE product_id = 1 ; SELECT * FROM product_variants WHERE product_id = 2 ; ... SELECT * FROM product_variants WHERE product_id = 10 ; Total: 10+1=11 SQL queries. If fetching 1,000 products, the number of queries spikes to 1,001 SQL ...