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.
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.
- 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.
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.
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.
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
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.
- TypeScript
- Apollo Server
- @apollo/datasource-rest
- GraphQL Code Generator
- Node.js
# Install dependencies
npm install
# Autogenerate types
npm run generate
#Run tests(coverage)
npm run test:cov
# Start server
npm run start