← Back to TypeScript Course | Chapter 28: Real World Projects | Lesson 11 of 14

TypeScript के साथ GraphQL API

GraphQL APIs typed schemas और operations expose करते हैं। TypeScript resolver arguments, results, और domain objects को GraphQL layer के आस-पास model कर सकता है।
Syntax
typescript
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

typescript
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

typescript
// 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

typescript
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

typescript
// 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

typescript
// CI step: npx graphql-codegen --check
console.log("Run codegen in CI, not just manually, to avoid stale types");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Generated types को हाथ से edit करना, इसलिए अगली code generation changes को overwrite कर देती है।
  2. Schema बदलने के बाद code generator फिर से run करना भूल जाना, इसलिए resolver types out of date रहते हैं।
  3. Resolver arguments को any के रूप में type करना, जो generated types की value को फेंक देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.