Aliases

Beginner · 5 min

Rename fields in a GraphQL response and query the same field twice with different arguments using aliases, plus how they resolve field conflicts in validation.

Before thisArguments

ShopifyDevs · 5 min

Aliases

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by ShopifyDevsReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • An alias renames a field in the response: cost: price puts the value under the key cost, and the server still resolves the field price.
  • Aliases are required when you select the same field twice with different arguments, because otherwise both would land on the same response key.
  • The response key is the alias if there is one, otherwise the field name, and each response key may appear only once at a given level.
  • Aliases exist only in the query; the schema and the resolvers never see them.
  • Aliasing lets one request fetch several variants of the same field, such as two authors by id, instead of making two requests.

The collision problem

In the last lesson, a query returned the book with id 10. Say you want two books side by side: a comparison page. The obvious attempt:

{
  book(id: "10") { title }
  book(id: "11") { year }
}

The server refuses:

Fields "book" conflict because they have differing arguments.
Use different aliases on the fields to fetch both if this was intentional.

(I ran this on graphql-js 17.1.0.) The reason is how a response is built. Every field becomes a key in a JSON object, and a JSON object cannot hold two book keys. The server has no way to put two results in one place, so it asks you to say where each goes.

Aliases

An alias is a name you put in front of a field with a colon. The response uses the alias as its key:

{
  first: book(id: "10") { title }
  second: book(id: "12") { title }
}
{"data":{"first":{"title":"The Dispossessed"},"second":{"title":"Cosmos"}}}

Both fields still run the book resolver. They received different id values and came back under first and second. This is the main reason aliases exist: fetch several variants of the same field in one request. A dashboard asking for “open orders” and “closed orders” off the same orders(status: ...) field is the same shape. In REST that is two requests.

I ran the equivalent for authors too: { a: author(id: "1") { name } b: author(id: "2") { name } } gave both names under a and b.

Aliases to rename

You can also alias without a conflict, just because you want a different name in the response:

{ book(id: "10") { name: title cost: price } }
{"data":{"book":{"name":"The Dispossessed","cost":12.5}}}

The client got name and cost and the schema stayed title and price. This helps when a UI component expects a particular shape, or when a field’s name is ambiguous in context. Use it in moderation. Too many renames make the query harder to compare with the schema and make server logs harder to read.

What an alias is not

An alias is purely a response-shaping tool. Three things follow:

  1. The resolver does not see it. Your server code, logs, and tracing see the field name. The alias lives in the query document only.
  2. It does not apply to arguments or types. You cannot alias an argument or a type name.
  3. Different aliases mean different executions. first: book(id: "10") and second: book(id: "10"), the same arguments under two aliases, run the resolver twice unless something caches it. An alias is not a cache.

Fields with the same arguments and no alias merge instead, as in { book(id: "12") { title title year } }: a single title.

Aliases and cost

Because aliasing makes it easy to ask for fifty copies of an expensive field in one request, aliases matter for security. A query that aliases login(...) a thousand times tries a thousand passwords in one HTTP call, which defeats per-request rate limits. That is why servers count fields, not requests, when limiting complexity. You will see it in rate limiting and cost analysis and query depth and complexity limits.

Syntax details

  • The alias comes first: alias: field(args) { ... }.
  • It must be a valid name (letters, digits, underscores, not starting with a digit).
  • It applies to any field: root fields, nested fields, and fields inside fragments.
  • Two different fields can’t share an alias at one level either. x: title x: year conflicts for the same reason.

What the video covers

ShopifyDevs’ “How to use GraphQL Aliases - 2 of 5” is under five minutes, published in November 2020, and is part two of a five-part “GraphQL Language Features” playlist. Its description says the presenter demonstrates the benefits of renaming returned data and querying for the same field more than once, which is exactly this lesson’s two uses. It is from a Shopify channel and the demos probably use Shopify’s API; I could not read a transcript to see. The language feature itself is not Shopify-specific. Jamie Barton’s 85-second alias video is linked as a shorter second view.

Where this fits

Aliases fix collisions between fields. A different kind of repetition shows up when many queries want the same group of fields, and that is what fragments are for. Next: fragments.

Check yourself

  1. 01What does { a: author(id: "1") { name } b: author(id: "2") { name } } return?

    An object with two keys, a and b, each holding the matching author, because the aliases give the two author fields different response keys.

  2. 02Why is { author(id: "1") { name } author(id: "2") { name } } rejected?

    Both fields would use the key author with different arguments, so validation reports a conflict and asks you to use different aliases.

  3. 03Does an alias change which resolver runs?

    No. The resolver for the real field runs, and the alias only changes the key in the response.

  4. 04Why alias a field when there is no conflict?

    To give the client the name it wants, such as renaming title to name, without touching the schema.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark