Implementing GraphQL Gateway Features

1. Configuring GraphQL Schema Stitching

ApproachDetail
Schema stitching LEGACYMerge SDL at gateway
Apollo Federation v2Subgraphs with @key directives
GraphQL MeshUnify REST/gRPC/SOAP as GraphQL
Hasura Remote SchemasDatabase + remote merge

2. Using GraphQL Query Validation

ValidatorCheck
Schema validationField/type existence
Operation typequery/mutation/subscription allowed
Argument typesMatch schema
Directive usageCustom @auth, @rate

3. Implementing Query Complexity Analysis

Example: Cost-based complexity

const rule = createComplexityRule({
  maximumComplexity: 1000,
  variables: req.body.variables,
  onComplete: (cost) => metrics.histogram("gql.cost").observe(cost),
  estimators: [
    fieldExtensionsEstimator(),     // uses @complexity directive
    simpleEstimator({ defaultComplexity: 1 })
  ]
});

4. Setting Query Depth Limits

LimitRecommended
Max depth7-10
Max breadth20 fields per level
Max aliases15 (prevent dup attacks)
Max document size16 KB

5. Configuring Batching and DataLoader

Example: DataLoader pattern

const userLoader = new DataLoader(async (ids) => {
  const users = await db.users.findByIds(ids);
  return ids.map(id => users.find(u => u.id === id));
});

// In resolver
const user = await userLoader.load(parent.userId);
// 100 calls in same tick → 1 DB query

6. Using Persisted Queries

BenefitDetail
Smaller requestSend hash, not full query
AllowlistOnly approved queries
Cacheable GETCDN-friendly
APQAuto-register on first miss

7. Implementing Subscription Support

TransportNotes
graphql-wsModern, recommended
subscriptions-transport-ws DEPRECATEDLegacy
SSEOne-way, simpler
Pub/Sub backendRedis, Kafka, NATS

8. Setting Up Introspection Control

EnvIntrospection
DevelopmentEnabled
StagingEnabled (auth required)
ProductionDisabled or auth-gated
Note: Disabling introspection in prod limits attacker recon. Provide schema via separate dev portal.

9. Configuring Apollo Federation

Example: Subgraph with @key

type User @key(fields: "id") {
  id: ID!
  name: String!
}

extend type Order @key(fields: "id") {
  id: ID! @external
  buyer: User!   # resolved via reference
}

10. Using Schema Registry

ToolUse
Apollo StudioHosted, composition checks
GraphQL HiveOpen source registry
Hasura DDNFederated data network
CI composition checkBlock breaking changes