apollo-server

द्वारा apollographql

Apollo Server 5.x के साथ विभिन्न फ्रेमवर्कों पर GraphQL सर्वर बनाने के लिए पूर्ण मार्गदर्शिका। इसमें स्कीमा परिभाषा, रिज़ॉल्वर, संदर्भ सेटअप, और TypeScript समर्थन के साथ त्रुटि प्रबंधन शामिल है। प्रोटोटाइपिंग के लिए स्टैंडअलोन मोड और Express, Fastify, Koa, तथा सर्वरलेस वातावरण के साथ एकीकरण का समर्थन करता है। इसमें रिज़ॉल्वर पैटर्न, प्रमाणीकरण/अधिकार, प्लगइन्स, N+1 रोकथाम के लिए DataLoader, और प्रदर्शन

npx skills add https://github.com/apollographql/skills --skill apollo-server

Apollo Server 5.x Guide

Apollo Server is an open-source GraphQL server that works with any GraphQL schema. Apollo Server 5 is framework-agnostic and runs standalone or integrates with Express, Fastify, and serverless environments.

Quick Start

Step 1: Install

npm install @apollo/server graphql

For Express integration:

npm install @apollo/server @as-integrations/express5 express graphql cors

Step 2: Define Schema

const typeDefs = `#graphql
  type Book {
    title: String
    author: String
  }

  type Query {
    books: [Book]
  }
`;

Step 3: Write Resolvers

const resolvers = {
  Query: {
    books: () => [
      { title: "The Great Gatsby", author: "F. Scott Fitzgerald" },
      { title: "1984", author: "George Orwell" },
    ],
  },
};

Step 4: Start Server

Standalone (Recommended for prototyping):

The standalone server is great for prototyping, but for production services, we recommend integrating Apollo Server with a more fully-featured web framework such as Express, Koa, or Fastify. Swapping from the standalone server to a web framework later is straightforward.

import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";

const server = new ApolloServer({ typeDefs, resolvers });

const { url } = await startStandaloneServer(server, {
  listen: { port: 4000 },
});

console.log(`Server ready at ${url}`);

Express:

import { ApolloServer } from "@apollo/server";
import { expressMiddleware } from "@as-integrations/express5";
import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
import express from "express";
import http from "http";
import cors from "cors";

const app = express();
const httpServer = http.createServer(app);

const server = new ApolloServer({
  typeDefs,
  resolvers,
  plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});

await server.start();

app.use(
  "/graphql",
  cors(),
  express.json(),
  expressMiddleware(server, {
    context: async ({ req }) => ({ token: req.headers.authorization }),
  }),
);

await new Promise<void>((resolve) => httpServer.listen({ port: 4000 }, resolve));
console.log("Server ready at http://localhost:4000/graphql");

Schema Definition

Scalar Types

  • Int - 32-bit integer
  • Float - Double-precision floating-point
  • String - UTF-8 string
  • Boolean - true/false
  • ID - Unique identifier (serialized as String)

Type Definitions

type User {
  id: ID!
  name: String!
  email: String
  posts: [Post!]!
}

type Post {
  id: ID!
  title: String!
  content: String
  author: User!
}

input CreatePostInput {
  title: String!
  content: String
}

type Query {
  user(id: ID!): User
  users: [User!]!
}

type Mutation {
  createPost(input: CreatePostInput!): Post!
}

Enums and Interfaces

enum Status {
  DRAFT
  PUBLISHED
  ARCHIVED
}

interface Node {
  id: ID!
}

type Article implements Node {
  id: ID!
  title: String!
}

Resolvers Overview

Resolvers follow the signature: (parent, args, contextValue, info)

  • parent: Result from parent resolver (root resolvers receive undefined)
  • args: Arguments passed to the field
  • contextValue: Shared context object (auth, dataSources, etc.)
  • info: Field-specific info and schema details (rarely used)
const resolvers = {
  Query: {
    user: async (_, { id }, { dataSources }) => {
      return dataSources.usersAPI.getUser(id);
    },
  },
  User: {
    posts: async (parent, _, { dataSources }) => {
      return dataSources.postsAPI.getPostsByAuthor(parent.id);
    },
  },
  Mutation: {
    createPost: async (_, { input }, { dataSources, user }) => {
      if (!user) throw new GraphQLError("Not authenticated");
      return dataSources.postsAPI.create({ ...input, authorId: user.id });
    },
  },
};

Context Setup

Context is created per-request and passed to all resolvers.

interface MyContext {
  token?: string;
  user?: User;
  dataSources: {
    usersAPI: UsersDataSource;
    postsAPI: PostsDataSource;
  };
}

const server = new ApolloServer<MyContext>({
  typeDefs,
  resolvers,
});

// Standalone
const { url } = await startStandaloneServer(server, {
  context: async ({ req }) => ({
    token: req.headers.authorization || "",
    user: await getUser(req.headers.authorization || ""),
    dataSources: {
      usersAPI: new UsersDataSource(),
      postsAPI: new PostsDataSource(),
    },
  }),
});

// Express middleware
expressMiddleware(server, {
  context: async ({ req, res }) => ({
    token: req.headers.authorization,
    user: await getUser(req.headers.authorization),
    dataSources: {
      usersAPI: new UsersDataSource(),
      postsAPI: new PostsDataSource(),
    },
  }),
});

Reference Files

Detailed documentation for specific topics:

Key Rules

Schema Design

  • Use ! (non-null) for fields that always have values
  • Prefer input types for mutations over inline arguments
  • Use interfaces for polymorphic types
  • Keep schema descriptions for documentation

Resolver Best Practices

  • Keep resolvers thin - delegate to services/data sources
  • Always handle errors explicitly
  • Use DataLoader for batching related queries
  • Return partial data when possible (GraphQL's strength)

Performance

  • Use @defer and @stream for large responses
  • Implement DataLoader to solve N+1 queries
  • Consider persisted queries for production
  • Use caching headers and CDN where appropriate

Ground Rules

  • ALWAYS use Apollo Server 5.x patterns (not v4 or earlier)
  • ALWAYS type your context with TypeScript generics
  • ALWAYS use GraphQLError from graphql package for errors
  • NEVER expose stack traces in production errors
  • PREFER startStandaloneServer for prototyping only
  • USE an integration with a server framework like Express, Koa, Fastify, Next, etc. for production apps
  • IMPLEMENT authentication in context, authorization in resolvers

apollographql की और Skills

apollo-client
apollographql
Apollo Client एक व्यापक स्टेट मैनेजमेंट लाइब्रेरी है जो जावास्क्रिप्ट के लिए है, जो आपको GraphQL के साथ स्थानीय और दूरस्थ डेटा दोनों को प्रबंधित करने में सक्षम बनाती है। संस्करण 4.x बेहतर कैशिंग, बेहतर TypeScript समर्थन, और React 19 अनुकूलता लाता है।
official
apollo-client
apollographql
Apollo Client 4.x के साथ React एप्लिकेशन बनाने के लिए व्यापक गाइड, जिसमें क्वेरी, म्यूटेशन, कैशिंग और स्टेट मैनेजमेंट शामिल हैं। यह कई React फ्रेमवर्क और सेटअप को सपोर्ट करता है: क्लाइंट-साइड ऐप्स (Vite, CRA), React सर्वर कंपोनेंट्स के साथ Next.js App Router, स्ट्रीमिंग SSR के साथ React Router 7, और TanStack Start। इसमें क्वेरी (useQuery, useLazyQuery), म्यूटेशन (useMutation), और आधुनिक React 18+ और 19 के लिए Suspense-आधारित पैटर्न (useSuspenseQuery,
official
apollo-connectors
apollographql
REST APIs को @source और @connect निर्देशों का उपयोग करके GraphQL सुपरग्राफ में एकीकृत करें। एक संरचित 5-चरणीय प्रक्रिया प्रदान करता है: API संरचना पर शोध करें, निर्देशों के साथ स्कीमा लागू करें, rover supergraph compose के माध्यम से मान्य करें, कनेक्टर निष्पादित करें, और परीक्षण कवरेज करें। हेडर, बॉडी पेलोड, N+1 पैटर्न के लिए बैचिंग, और $env के माध्यम से पर्यावरण चर इंजेक्शन सहित अनुरोध कॉन्फ़िगरेशन का समर्थन करता है। फ़ील्ड चयन,
official
apollo-federation
apollographql
Apollo Federation एकाधिक GraphQL API (उपग्राफ) को एक एकीकृत सुपरग्राफ में संयोजित करने में सक्षम बनाता है।
official
apollo-ios
apollographql
Apollo iOS एप्पल प्लेटफॉर्म के लिए एक मजबूत टाइप वाला GraphQL क्लाइंट है। यह आपके GraphQL ऑपरेशन और स्कीमा से Swift प्रकार उत्पन्न करता है, और एक async/await क्लाइंट, एक सामान्यीकृत कैश (इन-मेमोरी या SQLite-समर्थित), एक प्लग करने योग्य इंटरसेप्टर-आधारित HTTP ट्रांसपोर्ट जो क्वेरी, म्यूटेशन और मल्टीपार्ट सब्सक्रिप्शन को संभालता है, और एक वैकल्पिक WebSocket ट्रांसपोर्ट (graphql-transport-ws) जो किसी भी ऑपरेशन
official
apollo-kotlin
apollographql
Apollo Kotlin एक मजबूत टाइप किया गया GraphQL क्लाइंट है जो आपके GraphQL संचालन और स्कीमा से Kotlin मॉडल उत्पन्न करता है, जिसका उपयोग Android, JVM और Kotlin मल्टीप्लेटफ़ॉर्म प्रोजेक्ट्स में किया जा सकता है।
official
apollo-mcp-server
apollographql
एआई एजेंटों को मॉडल कॉन्टेक्स्ट प्रोटोकॉल के माध्यम से GraphQL API से जोड़ता है, जिसमें अंतर्निहित इंट्रोस्पेक्शन और ऑपरेशन टूल्स शामिल हैं। GraphQL ऑपरेशन को MCP टूल के रूप में प्रस्तुत करता है; तीन ऑपरेशन स्रोतों का समर्थन करता है: स्थानीय फ़ाइलें, GraphOS Studio संग्रह, और स्थायी क्वेरी मैनिफेस्ट। स्कीमा अन्वेषण और तदर्थ क्वेरी परीक्षण के लिए चार इंट्रोस्पेक्शन टूल (introspect, search, validate, execute) प्रदान करता है;
official
apollo-router
apollographql
अपोलो राउटर एक उच्च-प्रदर्शन ग्राफ राउटर है जो रस्ट में लिखा गया है और अपोलो फेडरेशन 2 सुपरग्राफ चलाने के लिए है। यह आपके सबग्राफ के सामने बैठता है और क्वेरी योजना, निष्पादन और प्रतिक्रिया संरचना को संभालता है।
official