TypeScript के साथ GraphQL API
In this page:
type Resolver<TArgs, TResult> = (parent: unknown, args: TArgs, context: unknown) => Promise<TResult>;
const resolveField: Resolver<{ id: string }, TypeName> = async (parent, args, context) => {
return { /* fields matching TypeName */ };
};
Core Concept
एक typed GraphQL API अपने TypeScript resolver types को अपने GraphQL schema के साथ sync में रखता है, ताकि एक resolver जो किसी field के लिए गलत shape return करे compile time पर पकड़ा जाए एक confusing runtime GraphQL error produce करने के बजाय।
उदाहरण: Core Concept
interface User {
id: string;
name: string;
}
// A resolver returning an incomplete User object is a compile-time error.
function resolveUser(): User {
return { id: "1", name: "Ravi" };
}
console.log(resolveUser());
Basic Setup
एक basic setup एक code generator (जैसे GraphQL Code Generator) उपयोग करता है जो आपकी .graphql schema files पढ़ता है और matching TypeScript types और resolver signatures अपने आप produce करता है।
उदाहरण: Basic Setup
// graphql-codegen reads schema.graphql, generates matching TS types.
// codegen.yml: { generates: { "types.ts": { plugins: ["typescript"] } } }
console.log("Code generation keeps resolver types in sync with the schema");
Typed Example
एक typed example: एक schema field user(id: ID!): User एक resolver signature (parent, args: { id: string }, context) => Promise<User> generate करता है, इसलिए एक required User field missing वाला object return करना तुरंत flag हो जाता है।
उदाहरण: Typed Example
interface User { id: string; name: string; }
interface QueryArgs { id: string; }
async function userResolver(parent: unknown, args: QueryArgs): Promise<User> {
return { id: args.id, name: "Ravi" };
}
userResolver(null, { id: "1" }).then(console.log);
Project Usage
एक असली project में, build के हिस्से के रूप में types regenerate करना (जब भी schema बदले) resolvers को उस schema से silently drift होने से रोकता है जिसे वे implement करने वाले हैं।
उदाहरण: Project Usage
// package.json: "build": "graphql-codegen && tsc"
console.log("Regenerating types on every schema change keeps resolvers honest");
Best Practices
Code generation को एक automated build/CI step के रूप में run करें बजाय एक manual one-off command के, क्योंकि एक schema change जिसके बाद regeneration न हो codebase में stale, misleading types छोड़ देगा।
उदाहरण: Best Practices
// CI step: npx graphql-codegen --check
console.log("Run codegen in CI, not just manually, to avoid stale types");
- Generated types को हाथ से edit करना, इसलिए अगली code generation changes को overwrite कर देती है।
- Schema बदलने के बाद code generator फिर से run करना भूल जाना, इसलिए resolver types out of date रहते हैं।
- Resolver arguments को
anyके रूप में type करना, जो generated types की value को फेंक देता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Todo App with TypeScript
- REST API with Express + TypeScript
- React Dashboard with TypeScript
- CLI Tool with TypeScript
- Library with TypeScript
- Full Stack TypeScript App
- TypeScript Design System
- TypeScript Monorepo Project
- Authentication System
- Real-time App with Socket.io
- GraphQL API with TypeScript
- Microservices with TypeScript
- TypeScript Best Practices Review
- What to Learn Next