↓Skip to main content
Building a GraphQL Mesh API Gateway
  1. Blog Posts/

Building a GraphQL Mesh API Gateway

2 min readΒ·
nodejs graphql graphql-mesh

Published on: 2024-08-22

πŸ“ Introduction

GraphQL Mesh is transforming how APIs are integrated. It allows developers to unify REST APIs, gRPC services, GraphQL APIs, databases, and other sources into a single GraphQL gateway – without writing resolvers manually.

This article covers:

  • What GraphQL Mesh is
  • Why it’s useful for modern backends
  • How it differs from Apollo Federation
  • A minimal working example to try locally

🌐 What is GraphQL Mesh?

GraphQL Mesh is an open-source framework that:

  • Generates GraphQL schemas automatically from existing APIs
  • Supports REST, gRPC, SOAP, OpenAPI, GraphQL APIs, and databases
  • Allows querying multiple sources via one GraphQL endpoint

βœ… Key takeaway: GraphQL Mesh bridges legacy and modern services under a unified schema without manual schema stitching or resolver definitions.

πŸ€” Why Use GraphQL Mesh?

BenefitExplanation
No manual resolversAuto-generates resolvers based on API definitions
Data unificationIntegrates REST, gRPC, GraphQL, and DBs into one schema
Legacy bridgingTurns existing APIs into GraphQL for modern frontend consumption
Type safetyGenerates TypeScript typings and SDKs automatically

πŸ” GraphQL Mesh vs Apollo Federation

FeatureGraphQL MeshApollo Federation
PurposeIntegrate any API/data source as GraphQLCompose multiple GraphQL microservices
Data source supportREST, gRPC, SOAP, GraphQL, DBsGraphQL services only
Resolver implementationAuto-generatedRequires developer-defined schemas and resolvers
Best use caseAPI gateway over heterogeneous servicesFederated GraphQL microservices

πŸ”§ Minimal GraphQL Mesh Example

Here is a practical quickstart using Node.js and npm.

1. Initialize Project

mkdir mesh-gateway
cd mesh-gateway
npm init -y

2. Install GraphQL Mesh CLI

npx @graphql-mesh/cli init

βœ… This creates:

  • mesh.config.yaml (Mesh config file)
  • Example handlers (REST or GraphQL)

3. Example mesh.config.yaml for REST API

sources:
  - name: JSONPlaceholder
    handler:
      openapi:
        source: https://jsonplaceholder.typicode.com/swagger.json
serve:
  port: 4000

This configuration:

  • Loads the OpenAPI schema from JSONPlaceholder Swagger docs
  • Generates GraphQL schemas and resolvers automatically
  • Serves the endpoint at http://localhost:4000

4. Run GraphQL Mesh

npx mesh dev

βœ… Visit http://localhost:4000 to access GraphiQL.

5. Sample Query

query {
  posts {
    id
    title
    body
  }
}

This fetches posts from JSONPlaceholder’s REST API via the auto-generated GraphQL schema.

πŸ—οΈ Architecture Diagram

πŸ”„ TL;DR

GraphQL Mesh simplifies API integration by:

  • Generating GraphQL schemas from any data source
  • Acting as an API gateway to unify REST, GraphQL, gRPC, and databases
  • Eliminating manual resolver complexity

For teams modernising backends or unifying multiple services into a single GraphQL endpoint, GraphQL Mesh offers a practical, production-ready solution.