Q10: Why should you use variables instead of inline string interpolation in queries?

Difficulty:EasyTopic:Security & Syntax

GraphQL variables allow query values to be dynamic without constructing queries via string manipulation.

Benefits:

  • Security: Prevents query injection attacks.
  • Caching: Allows GraphQL servers/clients to compile and cache static query document structures.
  • Readability: Keeps document text clean and re-executable.
Key Takeaways
  • Variables separate query structure from parameter values.
  • Using variables prevents query injection and enables client/server execution caching.
Knowledge Check

How are variable names prefixed in standard GraphQL query documents?