Poisonal
Pass it along a thing worth keeping

Studio Owners Defend $2,500 Bike Lane Veto as 'Standard Industry Networking'

In exchange for Zelle payment and TV cameo, city aide agreed to ignore safety advocates' pleas, keeping McGuinness Boulevard lethal for another decade.

1 year ago • 8 min read

Entertainment Labor Union Rep

Entertainment Labor Union Rep

why he refuses to migrate the legacy codebase

why he refuses to migrate the legacy codebase

Migrate Your Legacy PHP Logic to Java Services

Migrate Your Legacy PHP Logic to Java Services

The Server-Side Monopoly: PHP and Java Fight for the World Wide Web

The Server-Side Monopoly: PHP and Java Fight for the World Wide Web

Image

0 ·
No messages yet.

First-time Founder

First-time Founder

useEffect Dependency Arrays

useEffect Dependency Arrays

Before useMemo / After useMemo

typescript

+5-2
1 1
import { useQuery } from '@apollo/client';
2
+import { useMemo } from 'react';
2 3
import { GET_USER } from './queries';
3 4
4 5
export default function UserProfile({ userId }) {
@4 unchanged lines
9 10
if (loading) return <div>Loading...</div>;
10 11
11 12
const user = data.user;
12
- const formattedName = user.firstName + ' ' + user.lastName;
13
- const userObject = { name: formattedName, email: user.email };
13
+ const userObject = useMemo(() => ({
14
+ name: user.firstName + ' ' + user.lastName,
15
+ email: user.email
16
+ }), [user.firstName, user.lastName, user.email]);
14 17
15 18
return (
16 19
<div>
@5 unchanged lines

The Triple-Declaration Problem

The Triple-Declaration Problem

Find the Real Culprit in node_modules

Find the Real Culprit in node_modules

Server Component Type Contracts

Server Component Type Contracts

The Mapping Loop, Documented

typescript

+12-10
1
-// Before: Loose GraphQL query without type safety
2
-const query = `
1
+// After: Type-safe GraphQL with Next.js server components and TypeScript
2
+import { graphql } from 'gql.tada';
3
+
4
+const GetUserQuery = graphql(`
3 5
query GetUser($id: ID!) {
4 6
user(id: $id) {
5 7
id
@1 unchanged line
7 9
email
8 10
}
9 11
}
10
-`;
11
-
12
-const result = await fetch('/api/graphql', {
13
- method: 'POST',
14
- body: JSON.stringify({ query, variables: { id: '123' } })
15
-});
12
+`);
16 13
17
-const data = result.json();
18
-console.log(data.user.name); // No guarantee this exists
14
+export default async function UserProfile({ userId }: { userId: string }) {
15
+ const result = await executeQuery(GetUserQuery, { id: userId });
16
+
17
+ // TypeScript enforces that result.user.name exists
18
+ // Any typo in field names fails at build time ((cite:A1-F2))
19
+ return <div>{result.user.name}</div>;
20
+}

Why Your Types Are Never in the Same Room

Why Your Types Are Never in the Same Room

Cycle

The Type Duplication Loop

01
📝
Define GraphQL Schema
Write your GraphQL schema in your Next.js codebase. This serves as API documentation that lives alongside your code.
02
⚙️
Generate TypeScript Types
Run a code generator (like GraphQL Code Generator) to extract TypeScript types from your schema, creating type definitions for your queries and mutations.
03
🔌
Write Resolvers & API Routes
Implement your GraphQL resolvers or Next.js API routes using the generated types. This is where manual type mapping consumes the majority of your build time, forcing you to manually reconcile schema definitions with resolver implementations.
04
🎯
Execute Queries in Components
Use the generated types in your client code or Next.js server components eliminate the need for a separate GraphQL client for roughly half of typical query work.
05
🚨
Handle Type Mismatches
TypeScript catches every minor typo during compilation instead of letting mismatches slip into runtime. Fix the type errors, then loop back to step 3.
06
🔄
Regenerate on Schema Change
When you update your GraphQL schema, regenerate types and manually reconcile all affected resolver implementations. The cycle repeats.

More from Greenpoint