Day 2 of 5
⏱ ~60 minutes
GraphQL in 5 Days — Day 2

Queries: Fetching Exactly What You Need

Arguments, aliases, fragments, and variables — all the query features that make GraphQL powerful.

Query Arguments

Queries
# Basic query — only get what you need
query GetUsers {
  users {
    id
    name
    # Note: we're NOT requesting email
    # The server won't include it
  }
}

# Query with argument
query GetUser {
  user(id: "1") {
    name
    email
    posts {
      title
    }
  }
}

# Aliases: query same field twice with different args
query CompareUsers {
  alice: user(id: "1") { name email }
  bob: user(id: "2") { name email }
}

# Fragments: reusable field sets
fragment UserFields on User {
  id
  name
  email
}

query {
  users { ...UserFields }
}
Variables
# Variables make queries reusable
# Define in the query:
query GetUser($userId: ID!) {
  user(id: $userId) {
    name
    email
  }
}

# Pass variables separately:
{
  "userId": "1"
}

# With optional variables:
query SearchPosts($query: String, $limit: Int = 10) {
  searchPosts(query: $query, limit: $limit) {
    title
    body
  }
}
ℹ️
Variables are the right way to pass dynamic values. Never interpolate values directly into query strings — that's vulnerable to injection and prevents query caching on the server.
📝 Day 2 Exercise
Write Complex Queries
  1. W
  2. r
  3. i
  4. t
  5. e
  6. a
  7. q
  8. u
  9. e
  10. r
  11. y
  12. t
  13. h
  14. a
  15. t
  16. f
  17. e
  18. t
  19. c
  20. h
  21. e
  22. s
  23. u
  24. s
  25. e
  26. r
  27. s
  28. w
  29. i
  30. t
  31. h
  32. t
  33. h
  34. e
  35. i
  36. r
  37. p
  38. o
  39. s
  40. t
  41. s
  42. .
  43. W
  44. r
  45. i
  46. t
  47. e
  48. o
  49. n
  50. e
  51. u
  52. s
  53. i
  54. n
  55. g
  56. a
  57. l
  58. i
  59. a
  60. s
  61. e
  62. s
  63. t
  64. o
  65. f
  66. e
  67. t
  68. c
  69. h
  70. t
  71. w
  72. o
  73. s
  74. p
  75. e
  76. c
  77. i
  78. f
  79. i
  80. c
  81. u
  82. s
  83. e
  84. r
  85. s
  86. i
  87. n
  88. o
  89. n
  90. e
  91. r
  92. e
  93. q
  94. u
  95. e
  96. s
  97. t
  98. .
  99. E
  100. x
  101. t
  102. r
  103. a
  104. c
  105. t
  106. u
  107. s
  108. e
  109. r
  110. f
  111. i
  112. e
  113. l
  114. d
  115. s
  116. i
  117. n
  118. t
  119. o
  120. a
  121. f
  122. r
  123. a
  124. g
  125. m
  126. e
  127. n
  128. t
  129. a
  130. n
  131. d
  132. u
  133. s
  134. e
  135. i
  136. t
  137. i
  138. n
  139. t
  140. w
  141. o
  142. q
  143. u
  144. e
  145. r
  146. i
  147. e
  148. s
  149. .
  150. P
  151. a
  152. s
  153. s
  154. a
  155. n
  156. I
  157. D
  158. a
  159. s
  160. a
  161. v
  162. a
  163. r
  164. i
  165. a
  166. b
  167. l
  168. e
  169. .

Day 2 Summary

  • Clients request only the fields they need — no over-fetching.
  • Aliases: alice: user(id: "1") — same field, different name, different arguments.
  • Fragments: fragment Name on Type { ...fields } — reuse field selections.
  • Variables: always pass dynamic values as variables, not inline in the query string.
Finished this lesson?