DEV Community

Cover image for REST APIs vs GraphQL: is it one or the other?
Rob Walters
Rob Walters

Posted on

REST APIs vs GraphQL: is it one or the other?

GraphQL and REST are popular methods for building APIs that facilitate data transfer across networks. Both approaches emphasize key principles such as statelessness and the clear separation of client and server. They rely on HTTP as the main communication protocol. Both are flexible methods for developing APIs and can easily integrate with various programming languages and applications. As developers began to use APIs more, the need arose to filter and modify responses. Some APIs support synchronous calls, where one output serves as the input for another, while others can operate in parallel by design. This process, called API orchestration, highlights the advantages of GraphQL over REST APIs.
But what if you’ve invested considerable engineering hours and money into developing REST APIs for your organization? Is it worth rewriting all of these to implement GraphQL?

The good news is that you don’t have to rewrite any of your REST APIs to leverage GraphQL and utilize API Orchestration! Apollo Connectors for REST APIs make it easy and quick to integrate REST APIs into your GraphQL with no complex code to write or compile!

To illustrate, consider a REST API endpoint /getOrders/:orderId, where orderId represents an order ID like “12345”. The response to a GET request on /getOrders/12345 would be:

  {
      "order_id": "12345",
      "customer": {
        "id": "C001",
        "name": "John Doe",
        "email": "johndoe@example.com"
      },
      "items": [
        {
          "product_id": "P1001",
          "name": "Wireless Mouse",
          "quantity": 2,
          "price": 25.99
        },
        {
          "product_id": "P1002",
          "name": "Mechanical Keyboard",
          "quantity": 1,
          "price": 89.99
        }
      ],
      "total_price": 141.97,
      "status": "Shipped",
      "order_date": "2025-02-25T14:30:00Z"
    }
Enter fullscreen mode Exit fullscreen mode

To integrate this REST API with GraphQL, you only need to define the @source directive in your GraphQL schema like this:

extend schema
  @link(
    url: "https://specs.apollo.dev/federation/v2.10"
    import: ["@key", "@requires"]
  )
  @link(
    url: "https://specs.apollo.dev/connect/v0.1"
    import: ["@source", "@connect"]
  )
  @source(
    name: "orders"
    http: {
      baseURL: "<<REST API URL>>"
  })
Enter fullscreen mode Exit fullscreen mode

Then add your Types and Query:

type Order {
  orderId: ID!
  customer: String!
  totalItems: Float!
  totalPrice: Float!
  status: String!
  orderDate: String!
}

type Query {
  getOrder(order_id: ID!): Order
    @connect(
      source: "orders"
      http: { GET: "/getOrders/{$args.order_id}" }
      selection: """
          orderId: order_id
          customer:customer.name
          totalItems: items->size
          totalPrice: total_price
          status
          orderDate: order_date
      """
    )
}
Enter fullscreen mode Exit fullscreen mode

The @connect directive in the Query instructs GraphQL to use the “orders” connector. You can also specify a resource path that will be added to the baseUrl defined in the @source declarative statement. The selection field indicates how you want the data to be returned to the client. Here, you can optionally transform and filter the data. To help you build the selection mapping, Apollo provides a free Connectors Mapping Playground, as shown in the figure below.

Image description
The connection mapper takes an input and can suggest a mapping based on that input. In this example, we are renaming fields and even transforming a field called totalItems to indicate how many order items it contains instead of listing the items themselves (e.g., “totalItems: items->size”). Using the Apollo Sandbox included with the rover CLI, you can run ad-hoc GraphQL queries. In the figure below, we executed the getOrder query by passing “12345”, and the returned data shown is from our REST API.

Image description
All of this configuration was accomplished without any procedural code, saving time and making it easy to integrate your existing REST APIs into the graph!

For more information on Apollo Connectors check out the following:

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay