Skip to content

Use aliases

Igor Dianov edited this page May 29, 2024 · 2 revisions

Aliases can be used to return objects with a different name than their field name. This is especially useful while fetching the same type of objects with different arguments in the same query.

Example: First, fetch all books. Second, fetch all books by Leo Tolstoy. Third, fetch all books by Anton Chekhov:

query booksByAuthorAliases {
  allBooks: Books {
    select {
      id
      title
    }
  }
  booksByLeoTolstoy: Books(
    where: { author: {id: {EQ: 1  } } }
  ) {
    select {
      id
      title
    }
  }
  booksByAntonChekhov: Books(
    where: { author: {id: {EQ: 4  } } }
  ) {
    select {
      id
      title
    }
  }
}
{
  "data": {
    "allBooks": {
      "select": [
        {
          "id": 2,
          "title": "War and Peace"
        },
        {
          "id": 3,
          "title": "Anna Karenina"
        },
        {
          "id": 5,
          "title": "The Cherry Orchard"
        },
        {
          "id": 6,
          "title": "The Seagull"
        },
        {
          "id": 7,
          "title": "Three Sisters"
        }
      ]
    },
    "booksByLeoTolstoy": {
      "select": [
        {
          "id": 2,
          "title": "War and Peace"
        },
        {
          "id": 3,
          "title": "Anna Karenina"
        }
      ]
    },
    "booksByAntonChekhov": {
      "select": [
        {
          "id": 5,
          "title": "The Cherry Orchard"
        },
        {
          "id": 6,
          "title": "The Seagull"
        },
        {
          "id": 7,
          "title": "Three Sisters"
        }
      ]
    }
  }
}