Q14 of 20 · GraphQL

What are aliases and fragments, and when are they useful in tests?

GraphQLMidgraphqlqueriesmaintainabilityapi

Short answer

Short answer: Aliases rename fields in the response (and let you query the same field twice with different arguments); fragments are reusable field selections. In tests, aliases let you fetch several variants in one request, and fragments keep large test queries DRY.

Detail

Aliases rename a field in the response and let you request the same field multiple times with different arguments in a single query:

query {
  active: orders(status: ACTIVE) { id }
  cancelled: orders(status: CANCELLED) { id }
}

In testing, this lets a single request exercise multiple argument variants — handy for comparing behaviour across inputs in one round-trip.

Fragments are named, reusable selections of fields:

fragment OrderFields on Order { id total status }
query { order(id: "1") { ...OrderFields } }

In test suites with many queries that share field selections, fragments keep queries DRY and make a field-selection change a one-line edit — the same maintainability win as page objects or shared fixtures elsewhere.

These aren't critical-path concepts for a junior, but a mid-level tester knowing them signals real GraphQL fluency rather than copy-pasted queries.

// WHAT INTERVIEWERS LOOK FOR

Correct definitions and a practical testing use — aliases for multi-variant requests, fragments for DRY test queries.