--- title: GraphQL variables description: >- You can simplify GraphQL queries and mutations by extracting data into separate variables. source_url: html: 'https://shopify.dev/docs/apps/build/graphql/basics/variables' md: 'https://shopify.dev/docs/apps/build/graphql/basics/variables.md' --- ExpandOn this page * [Variable structure](https://shopify.dev/docs/apps/build/graphql/basics/variables.md#variable-structure) * [Simplify the customer creation request](https://shopify.dev/docs/apps/build/graphql/basics/variables.md#simplify-the-customer-creation-request) * [Next steps](https://shopify.dev/docs/apps/build/graphql/basics/variables.md#next-steps) # GraphQL variables Note The REST Admin API is a legacy API as of October 1, 2024. All apps and integrations should be built with the [GraphQL Admin API](https://shopify.dev/docs/api/admin-graphql). For details and migration steps, visit our [migration guide](https://shopify.dev/docs/apps/build/graphql/migrate). You can simplify GraphQL queries and mutations by extracting data into separate variables. GraphQL variables let you reuse the same requests with different arguments. *** ## Variable structure GraphQL requests can be split into query and variable sections. ### Query section In the query section, GraphQL variables begin with the `$` symbol and are declared after the `query` or `mutation` keyword, similar to passing an argument to a function. When you declare a variable, you need to specify its type, such as [`CustomerInput`](https://shopify.dev/docs/api/admin-graphql/latest/input-objects/CustomerInput). This lets GraphQL know that you intend to refer to this type by this variable name later in the actual query. For example, the following query declares an `$input` variable and passes it to the `input` argument: ## GraphQL mutation with an input variable ```graphql mutation($input: CustomerInput!) { customerCreate(input: $input) { ... } } ``` ### Variable section In the variable section, variables are defined as a JSON object. The following JSON object defines the `$input` variable for the query section: ## Input variables ```json { "input": { "firstName": "Ayumu", "lastName": "Hirano", "email": "ayumu@example.com" } } ``` *** ## Simplify the customer creation request The following example simplifies the [`customerCreate` mutation example](https://shopify.dev/docs/apps/build/graphql/basics/mutations#example-create-a-customer) by using variables, resulting in an abstracted mutation that can be reused to create multiple customers. ## POST /admin/api/{api\_version}/graphql.json ## GraphQL mutation ```graphql mutation ($input: CustomerInput!) { customerCreate(input: $input) { customer { id displayName } userErrors { field message } } } ``` ## Input variables ```json { "input": { "firstName": "Ayumu", "lastName": "Hirano", "email": "ayumu@example.com" } } ``` ## JSON response ```json { "data": { "customerCreate": { "customer": { "id": "gid://shopify/Customer/1310038130710", "displayName": "Ayumu Hirano" }, "userErrors": [] } } ... } ``` *** ## Next steps Learn how to optimize your GraphQL implementation further with [inline fragments and multi-query requests](https://shopify.dev/docs/api/usage/graphql-basics/advanced). *** * [Variable structure](https://shopify.dev/docs/apps/build/graphql/basics/variables.md#variable-structure) * [Simplify the customer creation request](https://shopify.dev/docs/apps/build/graphql/basics/variables.md#simplify-the-customer-creation-request) * [Next steps](https://shopify.dev/docs/apps/build/graphql/basics/variables.md#next-steps)