Moving from REST to GraphQL – A Case Study
luiz tanure

luiz tanure @letanure

About: Web developer, creating stuff on wthe eb and in the real world

Location:
Berlin, germany
Joined:
Feb 4, 2020

Moving from REST to GraphQL – A Case Study

Publish Date: Jul 14
0 0

Note: This article was originally published on August 20, 2018. Some information may be outdated.

GraphQL’s flexibility in allowing clients to request exactly what they need has been a game changer. This post walks through the process of migrating from REST APIs to GraphQL in a real-world frontend project.

Why We Considered GraphQL

REST was working, but we were facing some problems:

  • Over-fetching or under-fetching data
  • Multiple roundtrips to load related entities
  • Rigid server endpoints and response formats

GraphQL offered:

  • A single endpoint
  • Control over the shape of the data returned
  • Strong tooling and type safety

Setting Up Apollo Client

We used Apollo Client for data management on the frontend. Basic setup looked like this:

npm install @apollo/client graphql
Enter fullscreen mode Exit fullscreen mode
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';

const client = new ApolloClient({
  uri: '/graphql',
  cache: new InMemoryCache(),
});
Enter fullscreen mode Exit fullscreen mode

Then we wrapped the app with the provider:

<ApolloProvider client={client}>
  <App />
</ApolloProvider>
Enter fullscreen mode Exit fullscreen mode

Writing Queries

Instead of calling multiple endpoints, we used queries like this:

query GetUser {
  user(id: "123") {
    name
    email
    posts {
      title
      createdAt
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Apollo gave us useQuery to fetch this easily:

import { useQuery, gql } from '@apollo/client';

const GET_USER = gql`
  query GetUser {
    user(id: "123") {
      name
      email
      posts {
        title
        createdAt
      }
    }
  }
`;

const { loading, error, data } = useQuery(GET_USER);
Enter fullscreen mode Exit fullscreen mode

Mutations

Updating data was just as clean:

mutation AddPost($title: String!) {
  addPost(title: $title) {
    id
    title
  }
}
Enter fullscreen mode Exit fullscreen mode

Benefits Observed

After switching:

  • Fewer network requests overall
  • Easier frontend development
  • Better performance on low-bandwidth devices
  • Stronger type safety across the stack

Migrating to GraphQL simplified data handling and improved developer experience. It wasn’t instant, but the gains were clear once we integrated it into real features.

Comments 0 total

    Add comment