TExcelTAD
Home › GraphQL › Aliases

Aliases

The keys in a GraphQL response are normally the field names. That causes a problem when you want the same field twice with different arguments: both results would need the same key. Aliases solve this by letting you rename a field in the response.

The problem

js · try it yourself
import { graphql, buildSchema } from 'graphql';

const schema = buildSchema(`
  type City { name: String, population: Int }
  type Query { city(code: String!): City }
`);

const result = await graphql({
  schema,
  source: '{ city(code: "OSL") { name } city(code: "BCN") { name } }',
  rootValue: {},
});
console.log(result.errors[0].message);
Fields "city" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.

GraphQL refuses: one key, city, can't hold two different answers.

The fix: alias: field

Write the alias, a colon, then the field. The alias becomes the key in the result:

js · try it yourself
import { graphql, buildSchema } from 'graphql';

const schema = buildSchema(`
  type City { name: String, population: Int }
  type Query { city(code: String!): City }
`);

const cities = {
  OSL: { name: 'Oslo', population: 717710 },
  BCN: { name: 'Barcelona', population: 1660314 },
};

const rootValue = { city: ({ code }) => cities[code] };

const query = `{
  home: city(code: "OSL") { name population }
  trip: city(code: "BCN") { name population }
}`;

const result = await graphql({ schema, source: query, rootValue });
console.log(JSON.stringify(result, null, 2));
{
  "data": {
    "home": {
      "name": "Oslo",
      "population": 717710
    },
    "trip": {
      "name": "Barcelona",
      "population": 1660314
    }
  }
}

The server still runs the city resolver twice; only the response keys change.

Renaming for the client's convenience

Aliases work on any field, including scalars and nested fields. They're useful to match the names your UI code already uses, or to request one field in two formats:

js · try it yourself
import { graphql, buildSchema } from 'graphql';

const schema = buildSchema(`
  type Temperature {
    value(unit: String = "C"): Float
  }
  type Query { today: Temperature }
`);

const today = {
  celsius: 21.5,
  value({ unit }) {
    return unit === 'F' ? this.celsius * 9 / 5 + 32 : this.celsius;
  },
};

const query = `{
  today {
    c: value
    f: value(unit: "F")
  }
}`;

const result = await graphql({ schema, source: query, rootValue: { today } });
console.log(JSON.stringify(result.data));
{"today":{"c":21.5,"f":70.7}}

Aliases and __typename

You can alias meta fields too: kind: __typename returns the type name under the key kind.

Rules to remember

Rule Example
An alias is a plain name, no quotes home: city(code: "OSL")
Two fields may share a key only if they are identical { name name } is fine (merged)
Same key, different arguments → error { city(code: "A") city(code: "B") }
Aliases don't change the schema the resolver sees the real field name

Note: Aliases are a client-side feature. Your resolvers never need to know about them: graphql-js calls the resolver of the real field and stores the value under the alias.

Test yourself

What does trip: city(code: "BCN") { name } return in data?

  1. A key city
  2. A key trip
  3. Keys trip and city
  4. An error

When are aliases required?

  1. Always, for every field
  2. When selecting the same field with different arguments at the same level
  3. When a field has no arguments
  4. Only in mutations