Skip to content

thegodtune/graphql-proxy-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL Proxy Server with TypeScript

📉 What is GraphQL?

GraphQL is an open-source query language and runtime for APIs developed by Facebook.
It provides a more efficient, powerful, and flexible alternative to REST by allowing clients to:

  • Ask for exactly the data they need — nothing more, nothing less
  • Combine multiple resources in a single request
  • Evolve APIs without versioning, thanks to a strongly-typed schema

In this app, GraphQL serves as a proxy server.

🫱🏽‍🫲🏿 What is Proxy Server?

A proxy server sits between clients and one or more backend services and forwards requests on the client’s behalf. In the context of this app the proxy server is a GraphQL layer that translates client GraphQL requests into REST calls to an existing API, then translates the REST responses back into the GraphQL shapes expected by the client.

How this differs from related concepts

  • Reverse proxy (e.g., Nginx): typically routes/terminates HTTP requests without changing payload semantics.
  • API gateway: can perform similar tasks (auth, routing, transformations) but is usually a dedicated infra component; this project behaves like an application-level gateway focused on translating REST → GraphQL.
  • BFF (Backend-for-Frontend): similar idea, a tailored backend interface for a specific client, and a GraphQL proxy is often used as a BFF.

⚙️ How it works

1. Data Sources

The server uses Apollo’s @apollo/datasource-rest to consume external REST APIs. Each data source encapsulates the logic for fetching and transforming REST data into a GraphQL-friendly format.

These data sources are then injected into the GraphQL context, allowing resolvers to access REST data seamlessly.

2. Resolvers and Schema

Resolvers bridge the GraphQL operations (queries/mutations) with the data sources, effectively performing the “adapter” role. They take client requests written in GraphQL and translate them into REST calls behind the scenes.

3. Type Safety

To maintain strict type safety across the schema and resolvers, this project uses GraphQL Code Generator (graphql-codegen) to automatically generate TypeScript types from the GraphQL schema.

This ensures:

  • End-to-end type consistency
  • Fewer runtime errors
  • Faster, safer development

🧠 Design Pattern Inspiration

This implementation borrows from the OOP's Adapter Pattern, where:

  • Target interface: GraphQL API expected by the client
  • Adaptee: Underlying REST API
  • Adapter (this server): Bridges the two by converting GraphQL queries into REST calls and formatting the responses back into GraphQL objects.

🛠️ Tech Stack

  • TypeScript
  • Apollo Server
  • @apollo/datasource-rest
  • GraphQL Code Generator
  • Node.js

Getting Started

# Install dependencies
npm install

# Autogenerate types
npm run generate

#Run tests(coverage)
npm run test:cov

# Start server
npm run start

About

GraphQL proxy server for REST API. Drew inspiration from the Apollo GraphQL odyssey-intro-typescript project

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors