1

I am new to GraphQL and one of the benefits I often see listed is that you can use it to batch multiple API requests together.

So instead of querying a REST api for Authors (/authors) and posts (/posts) in two trips, with GQL you can do something like this in ONE round trip:

query getAllPostsAndAuthors {
  posts {
    id
    title
    exert
  }
  authors {
    firstName
    lastName
  }
}

I am confused about how this is still one round trip when the resolvers come into play. Lets say I have a postsResolver.js file and a authorsResolver.js file.

Each resolver makes a rest API call to (/posts) and (/authors) respectively to get the data and return it to the front end.

Are these separate REST api resolver calls batched together by Apollo or GQL somehow?

Or have I just pushed the two trip request/response cycle from the client to the revolvers? And if this is the case how is it a performance benefit?

Thanks!

Scott
  • 422
  • 1
  • 4
  • 17

1 Answers1

0

I think you have understood this incorrectly. GraphQL and REST are two different protocols. GraphQL does not batch REST calls. What it does is represent data in a tree-like structure, so the clients can request specific information and the server responds with the requested information only, unlike REST APIs where a certain resource is always responding with a pre-defined data model.

For example a REST resource /api/user always responds with a pre-defined structure like this:

{
    "id": 1
    "name": "John Doe",
}

Then if you need to retrieve, say, the posts from a user, you have to send a new request to /api/{user_id}/posts. This is called under-fetching, i.e. You have to send multiple requests to fetch all the data you need since a single request does not send all the data you need.

But in GraphQL this is defined in the GraphQL schema as a single object.

type User {
    id: ID!
    name: String!
    posts: [Post!]!
}

type Post {
    id: ID!
    title: String!
    content: String!
}

In this case, a client can request the information like this:

query {
    user {
        name
        posts {
            title
            content
        }
    }
}

This will return a response with the user name and the posts from that user with the post title and the content.

Retrieval of this data depends on the GraphQL server implementation. The advantage is you don't have to send two requests to the server as in a REST API. You can send this request and the GraphQL server responds with the requested data.

In your case, the backend does send two requests to the REST endpoints and batch the data, then sends them back to the client. This reduces the number of API calls from the client. But the backend still has to do two calls to fetch the data from a REST API. The advantage here is that the client does not have to send multiple requests and it does not receive data that it did not request.

Consider a scenario where you retrieve the data from a database in the backend. In this case, actually, you don't need to send multiple requests to the database but retrieve the data in a lesser number of database queries as well.

ThisaruG
  • 3,222
  • 7
  • 38
  • 60