skip to content

What does an alias do in a GraphQL query, and when do you need one?

level: middleimportance: should knowfreq 57%

answer

  1. The key in the response is not always the field name
  2. One field, two sets of arguments
  3. Selections sharing a key must collapse
  4. Invented by the client, unknown to the schema
  5. Two keys still mean two resolutions

basics

~20 s

An alias, written alias: field, renames the key a field appears under in the response. You need one to select the same field twice at one level with different arguments, because two selections sharing a response key must otherwise be identical.

solid answer

~50 s

Every selected field lands in the response under a **response key**: the alias if one is written, otherwise the field name. An alias changes only that key — it does not pick a different schema field, and the schema never declares it. Its real job follows from a validation rule the specification calls Field Selection Merging: two selections that share a response key at the same level must be *mergeable*, meaning the same field name with the same arguments. So in a freight-tracking graph, asking `leg(sequence: 1)` and `leg(sequence: 7)` side by side is invalid — both want the key `leg` but pass different arguments. Writing `origin: leg(sequence: 1)` and `final: leg(sequence: 7)` gives them distinct keys and the document validates. Aliases are also how a client avoids a key collision when two selections would otherwise overwrite each other in its own result object.

code

graphql · 7 lines
graphql
query ShipmentEtas($id: ID!) {
  shipment(id: $id) {
    reference
    declaredEta: eta(source: DECLARED)
    carrierEta:  eta(source: CARRIER)
  }
}

go deeper

for a junior

Be able to read origin: leg(sequence: 1) and say which key comes back in the response. Knowing the syntax and that the response key follows the alias is enough at this level.

for a middle

Explain why aliases exist rather than just what they look like: two selections sharing a response key must be mergeable, and differing arguments make them conflict. Be ready to state that aliases live only in the document.

for a senior

Be ready for the operational angle — aliasing multiplies field resolutions inside one request, so a short document can imply heavy fan-out, and anything typed against the response keys off the alias rather than the schema field.

for a principal

Own the contract framing: response keys are client-chosen, so the schema is not the whole interface. Decide how your organization treats alias churn, request-cost accounting for aliased fan-out, and field usage attribution when clients rename freely.

## The response key, not the field name GraphQL's response is a plain object whose keys come from the document, not from the schema. The spec calls the key a field produces its **response key**, and defines it in one line: the alias if the selection has one, otherwise the field name. Everything about aliases falls out of that definition. ```graphql query TerminalDwell($id: ID!) { shipment(id: $id) { reference origin: leg(sequence: 1) { terminal { code } departedAt } final: leg(sequence: 7) { terminal { code } arrivedAt } } } ``` The response carries `origin` and `final`. It carries no key called `leg` at all, and the `Shipment` type in the schema has no field called `origin` — an alias is a property of the *document*, invented by the client, and it never has to exist anywhere else. ## Why the language needs them at all If aliases were only cosmetic renaming they would be a footnote. They are not, because of a validation rule the specification calls **Field Selection Merging**. Informally: if two selections in the same scope share a response key, the executor must be able to collapse them into one, and it can only do that when they name the same field with the same arguments (and, for composite fields, when their sub-selections merge too). That rule is what makes the innocent-looking document illegal: ```graphql # invalid: both selections want the response key `leg`, with different arguments shipment(id: "BKG-40913") { leg(sequence: 1) { departedAt } leg(sequence: 7) { arrivedAt } } ``` Validation rejects the whole document. There is no rule that a field may not appear twice — selecting `reference` three times is perfectly legal, and the executor simply merges the three into one key. The rule is about *conflict*, and arguments are the usual source of it. Aliasing resolves the conflict by giving each selection its own key. Nothing else changes: the server still resolves the `leg` field twice, once per set of arguments. ## Merging is what makes repetition safe The same mechanism is why identical selections can pile up harmlessly. Two parts of a document may each ask for `reference`; they merge and the field is resolved once. For composite fields the merge is recursive — two selections of `terminal` under the same key merge if their arguments match, and their sub-selections are then merged in turn, so `terminal { code }` and `terminal { city }` combine into `terminal { code city }` under a single key. This matters most in a document assembled from several pieces, where no single author sees the whole selection at once. Merging is why the pieces compose; the conflict rule is why they occasionally refuse to. ## What an alias is not * **Not a schema concept.** Aliases exist only in the document. They do not appear in the SDL, they cannot be constrained by the server, and two clients may alias the same field differently. * **Not a way to reach a different field.** `total: reference` returns the value of `reference`; the alias has no lookup power of its own. * **Not a cost saving.** Two aliased selections of the same field with different arguments are two field resolutions. A document that aliases the same expensive field forty times causes forty resolutions — which is exactly why aliasing shows up in the abuse-control conversation about a public endpoint. * **Not free for consumers.** Because the response key follows the alias, anything typed against the response — generated result types, a normalized client cache's own bookkeeping — keys off the alias, not the schema field name. Renaming an alias is a change to the response contract as far as the calling code is concerned. ## A worked case: two views of the same object A freight-tracking screen shows a shipment's declared ETA and the current carrier estimate side by side. Both come from one schema field, `eta(source: DECLARED)` and `eta(source: CARRIER)`. Without aliases the document does not validate. With `declaredEta:` and `carrierEta:` it does, and the response reads naturally: ```json { "data": { "shipment": { "declaredEta": "2026-04-18", "carrierEta": "2026-04-21" } } } ``` An interviewer following up will usually ask what happens if you alias the same field with the *same* arguments — the answer is that it is legal, produces two keys with equal values, and the field is still only resolved once per distinct set of arguments. ## What to say in an interview Define the response key first, then say the alias sets it. Give the two-arguments-one-field case as the reason aliases exist rather than presenting them as cosmetic. Mention Field Selection Merging by what it does — selections sharing a key must be collapsible — and note that aliases are document-only and never appear in the schema.

  • Is it legal to select the same field twice at one level with identical arguments and no aliases?
    Yes. The rule forbids selections that share a response key and cannot be merged, not repetition. Identical selections merge into one key and the field resolves once. For composite fields the merge is recursive, so `terminal { code }` and `terminal { city }` combine into a single `terminal` key carrying both sub-fields.
  • Does aliasing a field twice make the server do less work than sending two requests?
    Less transport work, not less field work. Each distinct set of arguments is a separate field resolution, so forty aliased selections of one expensive field are forty resolutions inside a single request. That asymmetry — a tiny document causing large fan-out — is precisely why aliasing features in abuse-control discussions for a public endpoint.
  • If a client renames an alias, what downstream code can break?
    Anything keyed off the response. Generated result types take their member names from the response key, and code reading the payload indexes by that key, so renaming an alias is a breaking change for the caller even though the schema did not move at all. The server is entirely unaffected — it never sees the alias as anything but a label.

An alias is a label on the box you asked to be delivered in, not the name of the item inside; ask for two of the same item with different options and you must label the boxes differently.

saying these in an interview costs you the question

  • Thinks the alias must be declared in the schema
  • Says an alias selects a differently named field
  • Believes any repeated field name is illegal
  • Claims aliasing avoids resolving the field twice
  • Treats aliases as purely cosmetic renaming
  • Cannot explain why differing arguments conflict

context