⬡ GraphQL Beginner

What are GraphQL variables?

Answer

Variables make GraphQL operations dynamic and reusable by externalizing dynamic values from the query string. Instead of inline values: { user(id: "123") { name } }, use variables: query GetUser($id: ID!) { user(id: $id) { name } } — then pass { "id": "123" } as a separate JSON object. Variables are declared in the operation signature with $name: Type syntax. Benefits: (1) Prevents injection attacks (values are never embedded in the query string). (2) Makes operations reusable with different inputs. (3) GraphQL clients (Apollo, Relay) can cache by operation name + variable hash. Variable defaults: $status: Status = ACTIVE. Variables must match the declared type — the server validates them before execution.