⬡ GraphQL
Beginner
What are variables in GraphQL queries?
Answer
Variables allow you to pass dynamic values into a GraphQL query or mutation without embedding them as string literals inside the query document. You declare variables in the operation signature with a $ prefix: query GetUser($id: ID!) { user(id: $id) { name } }. Variables are sent as a separate JSON object alongside the query: { "id": "42" }. This approach is safer (avoids injection attacks), more performant (queries can be cached on the server), and cleaner — the query document stays static and only the variables change between calls.