Fetching from a REST API: Integrating External Data into GraphQL
Learn to move beyond static data by fetching from a REST API in your GraphQL resolvers. Master data transformation and async integration for your server.

Previously in this course, we explored Asynchronous Resolvers. Now that you understand how to manage promises, we’ll move beyond static JSON files and connect your GraphQL server to a live, external REST API.
By the end of this lesson, you will be able to use fetch (or axios) inside your resolvers to pull data from a remote service and transform that raw REST structure to match your GraphQL schema perfectly.
Why Integrate REST into GraphQL?
Most modern applications don't live in a vacuum. Your GraphQL server acts as an orchestration layer—a "gateway"—that fetches data from various sources (databases, microservices, or third-party APIs) and presents a clean, unified interface to the client.
While we previously connected to JSON data stored locally, real-world services require dynamic, live data. REST integration allows your GraphQL API to act as a powerful wrapper, hiding the complexity of multiple endpoints from your frontend.
The Integration Pattern
The workflow is straightforward:
- Trigger: A GraphQL query hits a resolver.
- Fetch: The resolver makes an asynchronous
GETrequest to a REST endpoint. - Transform: The resolver maps the REST-style JSON response to your GraphQL schema's field names.
- Return: The GraphQL server sends the sanitized data to the client.
Worked Example: Fetching a User Profile

Let's assume we want to fetch user data from a public placeholder API (https://jsonplaceholder.typicode.com).
1. Define the Schema
First, ensure your SDL matches the data you expect to receive:
GraphQLtype User { id: ID! name: String email: String } type Query { user(id: ID!): User }
2. Implement the Resolver
We will use the native Node.js fetch API. In your resolver, you must await the response, parse the JSON, and map it.
JAVASCRIPTconst resolvers = { Query: { user: async (_, { id }) => { const response = await fetch(CE9178">`https://jsonplaceholder.typicode.com/users/${id}`); if (!response.ok) { throw new Error("Failed to fetch user from external API"); } const data = await response.json(); // Transform: Mapping REST keys to our GraphQL schema return { id: data.id, name: data.name, email: data.email }; }, }, };
Key Considerations for REST Integration
- Error Handling: Unlike local files, network calls fail. Always check
response.okor wrap your fetch in atry/catchblock to return meaningful errors to the client. - Transformation: REST APIs often return deeply nested objects or different naming conventions (like
user_namevsname). The resolver is the perfect place to flatten or rename these fields to maintain your GraphQL schema's contract. - Performance: Every call to
fetchadds network latency. If you resolve a list of users, you’ll trigger the N+1 problem. We will address this in the next lesson regarding the Data Loader pattern.
Hands-on Exercise
- Install
node-fetchif you are using a Node version older than 18, or simply use the built-infetchif you are on a modern version. - Modify your current project's
Queryresolver to fetch a list of "posts" fromhttps://jsonplaceholder.typicode.com/posts. - Create a
Posttype in your schema withid,title, andbodyfields. - Ensure your resolver returns an array that matches this schema structure.
Common Pitfalls
- Ignoring Async: Forgetting to
awaitthefetchcall is the most common bug. If you return a promise instead of the resolved data, your API will returnnullor an unexpected object. - Over-fetching: Just because the REST API returns 20 fields doesn't mean your GraphQL resolver should pass them all through. Map only what your schema defines to keep the data layer clean.
- Hardcoding URLs: Avoid hardcoding API URLs directly inside resolvers for large projects. Use environment variables or a dedicated service class to manage configuration.
FAQ
Q: Should I use fetch or axios?
A: Both work perfectly. fetch is built into modern Node.js, making it great for simple projects. axios provides better features for interceptors and automatic JSON parsing, which are helpful in complex production environments.
Q: Does this make my GraphQL server slow? A: It adds network overhead. The goal is to consolidate multiple REST calls into one GraphQL response, which actually improves performance for the frontend by reducing the number of round-trips.
Q: Can I use fetch inside a field resolver (not just a root query)?
A: Absolutely! This is the primary way to build "nested" relationships where a User has a field posts that fetches data from a different REST endpoint.
Recap
We’ve successfully moved from static data to dynamic REST integration. By using fetch inside our resolvers and transforming the resulting JSON, we’ve made our GraphQL API a true gateway for external data. Remember to always handle your network errors gracefully and keep your schema strictly typed.
Up next: The Data Loader Pattern — how to solve the N+1 problem when fetching lists of data from REST APIs.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

Headless WordPress + Next.js Frontend Development
Keep WordPress for content, get a lightning-fast Next.js frontend. The best of both worlds — familiar editing, modern speed.


