In the last episode we saw how to set up GraphQL in React Native and how to get some JSON from a GraphQL API.

In today's episode, we will see some interesting aspects about GraphQL and the problems it can solve for us. Also, we will take a quick look at the GraphqL features.

GrapqhQL ecosystem

It's worth mentioning the GraphQL ecosystem, because we see a lot of companies using it to build libraries and products for the developer lifestyle.

What problems can GraphQL help us with?

You may be asking what the real problems that GraphQL can solve for us are. Let's cover some of them.

Minimize network requests

The main issue for mobile clients: High network latency. People use 3G or bad connections sometimes. We need to make a lot of round trips to get all of the content from one view, for example. If we have several endpoints for one object, what we need is gathered data without all of those round trips. Using GraphQL we don't need to make a lot of requests, because we can do a complex query in a single request and get all the information we need.

Strong Types

Normally our server returns raw JSON. For typed languages we will need to cast. GraphQL helps us on that. We can use the types from the GraphQL schema.

We can have Types in our Schema.

Validation

We can validate the queries, so that the client and the server can do this validation and check if the queries are correct.

Mutations and Fragments

We’ve covered some problems that GraphQL can help us with. Now, let's see two other important features in GraphQL: Mutations and Fragments.

Fragments

Imagine you have a lot of complicated queries and you might need to reuse some of them. To do that, you can create a Fragment. This allows us to have a set of fields that we can reuse.

Imagine we have the following Query to get some user information:

{
  graphQLHub
  github {
    user(username: "knewter") {
      login
      id
      avatar_url
    }
  }
}

We can create a fragment to handle some of the user information. Let’s imagine you want to create a fragment that only shows the login.

fragment userFragmentWithOnlyLogin on GithubUser {
  login
}

And we can use this fragment:

{
  graphQLHub
  github {
    user(username: "clayallsopp") {
      ...userFragmentWithOnlyLogin
    }
  }
}

fragment userFragmentWithOnlyLogin on GithubUser {
  login
}

Mutations

It's easy to fetch data using GraphQL–it's one of the first things we did. If we use REST to fetch data, we use GET as our standard, and then we use POST when we are going to change some objects. Mutations are what we use to change the database.

Similar to queries, but mutations also allow you to update your UI.

You can choose the updates you would perform with a temporary server queue that allows you to make the real change later.

In the next episode, we will change a bit what we already have in our HelloGraphQL app.

Resources