1 Aliasing (rename query elements)
changeling edited this page 2019-05-14 17:40:29 -05:00

GraphQL allows you to alias, or rename, elements of your queries. This is built-in and trivial, but a powerful feature that is often overlooked.

To alias, simply prefix the elements you wish to rename with the desired string followed by :, like so:

{
  imaginarium:places(
    id:"UGxhY2VUeXBlOjUxOA==, UGxhY2VUeXBlOjU0Nw==")
  {
    menagerie:edges {
      dogs:node {
        dogtag:id
        place:name
        favoritePizzaPlace:name
      }
    }
  }
}

Which results in:

{
  "data": {
    "imaginarium": {
      "menagerie": [
        {
          "dogs": {
            "dogtag": "UGxhY2VUeXBlOjUxOA==",
            "place": "Dallas, Dallas, Texas, United States",
            "favoritePizzaPlace": "Dallas, Dallas, Texas, United States"
          }
        }
      ]
    }
  }
}