The following is the first few sections of a chapter from GraphQL and Android, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Aliases

Phil Karlton coined a popular programming maxim:

There are only two hard things in Computer Science: cache invalidation and naming things.

Naming things is a challenge because not everbody agrees on the name. Sometimes, we cannot even agree on a naming convention (such as a naming_convention, a namingConvention, or a NamingConvention).

Aliases allow you, on the client side, to tell the server how to name things. These aliases then impact the JSON that is returned to you.

On the surface, this may not seem all that interesting, as developers are used to having to conform to somebody else’s NAMING_CONVENTION. However, due to the way that GraphQL allows clients to “shape” the responses coming from the server, aliases also play a role in batched requests.

Applying Aliases

Let’s go back to one of the earlier GraphQL requests that we made:

query all {
  allTrips {
    id
    title
  }
}
That gives a raw JSON result that looks like:

{
  "data": {
    "allTrips": [
      {
        "id": "2c494055-78bc-430c-9ab7-19817f3fc060",
        "title": "Vacation!"
      },
      {
        "id": "e323fed5-6805-4bcf-8cb6-8b7a5014a9d9",
        "title": "Business Trip"
      }
    ]
  }
}

The data gives us an allTrips property, containing the data that we requested — in this case, an array of Trip objects, for which we only wanted the id and title fields.

The reason that property is named allTrips is because we requested the allTrips field from the root query object. The field that you query (or mutate) is the default name used for the results from that query or mutation.

However, through aliases, we can rename that property to something else:

query all {
  stuff: allTrips {
    id
    title
  }
}

Here, we have applied an alias of stuff to the allTrips field. Now, our response has a stuff property instead of an allTrips property:

{
  "data": {
    "stuff": [
      {
        "id": "2c494055-78bc-430c-9ab7-19817f3fc060",
        "title": "Vacation!"
      },
      {
        "id": "e323fed5-6805-4bcf-8cb6-8b7a5014a9d9",
        "title": "Business Trip"
      }
    ]
  }
}

This may not seem especially useful. However, aliases are key to unlocking a few other GraphQL features, particularly for cases where we cannot use the original field names… perhaps because we are querying or mutating that field more than once.

One, Two, Many, Lots

The preview of this section was lost due to a rupture in the space-time continuum.

Aliases with Apollo-Android

The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.

GraphQL Execution Rules

The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.