Skip to content

tthaisnunes/rick-and-morty-bff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL BFF with Apollo Server

This project is a Backend-for-Frontend (BFF) that uses GraphQL and Apollo Server to consume public REST APIs (like the Rick and Morty API) and format the data for the frontend in a flexible, efficient way.


How REST API Works

In a traditional REST architecture, the frontend makes multiple requests to different endpoints to get related data.

For example:

  • /characters → returns all characters
  • /episodes → returns all episodes
  • /locations → returns all locations

Each endpoint must be called separately, and sometimes the frontend receives more data than it actually needs

REST API Flow


How GraphQL Works

With GraphQL, the frontend sends one single request describing exactly what data it wants. The GraphQL server then fetches and combines data from multiple sources (APIs, databases, etc.) and returns a custom-shaped response.

This makes it easier, faster, and more efficient for the frontend to get data.

GraphQL Flow


Where the Server Runs

The GraphQL BFF runs on the same server that hosts the backend. That means the latency between them will be very low, improving performance and making data aggregation nearly instant.


What is Apollo Server?

Apollo Server is a powerful library for building a GraphQL API in Node.js. It acts as the “kitchen boss” that:

  • Receives GraphQL queries and mutations
  • Fetches data from other APIs or services
  • Combines and returns the data in one clean response

It also comes with tools like Apollo Sandbox or GraphQL Playground that make it easy to test and debug queries interactively.


✅ Pros of Using GraphQL BFF

  • Returns data “ready to use” for the frontend — one single endpoint, data formatted and combined.
  • Avoids unnecessary API calls — only fetches what’s requested.
  • More security — adds an extra layer between backend and frontend, allowing safe handling of API keys and tokens.
  • Evolvable schema — you can add new fields or APIs later without breaking existing clients.
  • Developer-friendly tools — Apollo Sandbox or GraphQL Playground make it simple to test and explore data.

⚠️ Cons of Using GraphQL BFF

  • Errors return HTTP 200 — even when a nested API fails (GraphQL handles errors inside the response body).
  • More code to maintain — the BFF adds an extra layer in your architecture.
  • Potential performance pitfalls — poorly designed queries can fetch too much data or trigger nested resolver “waterfalls”.

When to Use GraphQL as a BFF

Great choice when:

  • You have multiple frontends (web, mobile, smartwatch, etc.) using the same backend.
  • You need to combine data from multiple APIs or microservices.
  • You want to reduce over-fetching and give the frontend flexibility in shaping its data.

🚫 Not ideal when:

  • You have a very small app with only a few simple endpoints.
  • You don’t need to aggregate or transform data before sending it to the frontend.

Example Tech Stack

  • Node.js
  • Apollo Server
  • GraphQL
  • Rick and Morty REST API

Try It Out

Start the server:

npm install
npm start

Then open the Apollo Sandbox and test queries like:

query Characters {
  characters {
    name,
    gender,
    species,
    location {
      name
    }
  }
}

Author: Thais Souza Purpose: Demonstration of GraphQL BFF architecture using Apollo Server.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors