Aliases

GraphQL API documentation on using aliases to rename fields and query the same field multiple times with different arguments.

GraphQL allows you to rename the result of a field to anything you want. This is possible with the help of aliases.

You can use this feature to:

  • rename long fields and use name that you prefer over fields from the schema
  • query for the same field multiple times but with different arguments

Here is an example:

query GetPaymentLines($cid: Int!)
{
  useCompany(no: $cid)
  {
    paymentLine(first: 1)
    {
      items {
        paymentNo
        lineNo
        
        currency : joinup_Currency {
            isoCode
         }
        
        supplier : joinup_Associate_via_Supplier {
            bankAccount
          
            country : joinup_Country {
               isoCode
            }
         }
      }
    }
  }
}

Aliases can be used in:

  • queries (example shown above)
  • inserts and updates
  • aggregates

Here is another example in an aggregate function:

query GetAgregates($cid: Int!)
{
  useCompany(no: $cid)
  {
    order_aggregate
    {
      totalvat : sum
      {
         vatAmountDomestic
      }
    }
  }
} 

As previously mentioned, an important use case is to query for the same field multiple times but using different arguments. An example is shown below:

Query

query ($cid : Int!)
{
  useCompany(no : $cid)
  {
    some : generalLedgerAccount(first : 2)
    {
      items
      {
        accountNo
        name
      }
    }
    more : generalLedgerAccount(first : 2,
      filter : {accountNo : {_gt : 6000}})
    {
      items
      {
        accountNo
        name
      }
    }
  }
}
Result

{
  "data": {
    "useCompany": {
      "some": {
        "items": [
          {
            "accountNo": 1000,
            "name": "Forskning og utvikling"
          },
          {
            "accountNo": 1020,
            "name": "Konsesjoner"
          }
        ]
      },
      "more": {
        "items": [
          {
            "accountNo": 6010,
            "name": "Avskr. maskiner, inventar mv."
          },
          {
            "accountNo": 6020,
            "name": "Avskr. immaterielle eiendeler"
          }
        ]
      }
    }
  },
  "extensions": {
    "vbnxt-trace-id": "02c029a3a07c7a..."
  }
}
Last modified September 24, 2024