Passing Argument Values Using Variables

Learn how to use variables to pass arguments values to the field.

Using variables to pass argument values

GraphQL variables act as typed placeholders for values that will be sent along with the request. This concept may be familiar to you if you’ve used parameterized SQL queries for the insertion and sanitization of values. GraphQL variables are declared with their types before they’re used alongside the operation type. We haven’t had to think about operation types before, so let’s talk a little bit about what operations are and how they fit inside the GraphQL document.

Understanding operations

A GraphQL document consists of one or more operations, which model something we want the GraphQL server to do. Up to this point, we’ve asked the server to provide the information operation that GraphQL uses to call a query. GraphQL has other operation types, too, notably a mutation for persisting change to the data and a subscription to request a live feed of data. We’ll get into those later.

We’ve been using a simplified way of typing a query operation, which just uses an outer set of curly braces ({}) to demarcate its beginning and ending:

Get hands-on with 1200+ tech skills courses.