We're constantly trying to improve your support experience, and your feedback is extremely valuable to us.

Please take a moment to tell us about your experience today.
Sign up for future Help Center user research studies.

Advanced Concepts

Develop your GraphQL skills even further with these advanced concepts.

Inline fragments

Inline fragments allow you to build additional flexibility and reusability into your queries. They are marked by the ... on <TYPE> syntax.

For example, you can use the node field on the QueryRoot to request specific objects by their ID. This returns the object as a generic node object, which doesn't let you request any information other than the ID. However, by using an inline fragment, you can ask for more specific data data to return when the node is of a specific type. This is especially useful on nodes that don't have an easy access point through the QueryRoot, such as a single LineItem object.

You can specify multiple inline fragments, effectively allowing you to build conditionals in your request that enable you to request different return fields based on the node type. This is useful for selections that can return many different types of fields, such as the nodes connection on the QueryRoot, which accepts an array of IDs and can return any number of different node types.

The following example uses all the concepts covered so far to find where a line item is stocked so that it can be fulfilled from that location. The query uses the node field on the QueryRoot and provides the line item's ID

POST /admin/api/2019-07/graphql.json

query getLineItemLocationId($id: ID!) {
  node(id: $id) {
    ... on LineItem {
      id
      variant {
        inventoryItem {
          inventoryLevels(first: 1) {
            edges {
              node {
                location {
                  id
                  name
                }
              }
            }
          }
        }
      }
    }
  }
}

Variables

{
  "id": "gid://shopify/LineItem/3111147110422"
}

JSON response

{
  "data": {
    "node": {
      "id": "gid:\/\/shopify\/LineItem\/3111147110422",
      "variant": {
        "inventoryItem": {
          "inventoryLevels": {
            "edges": [
              {
                "node": {
                  "location": {
                    "id": "gid:\/\/shopify\/Location\/6884556842",
                    "name": "150 Elgin"
                  }
                }
              }
            ]
          }
        }
      }
    }
  },
  ...
}

Run in GraphiQL

Make multiple queries in one request

You can submit multiple queries or mutations in a single GraphQL request. This lets you query the same field or run the name mutation multiple times with different arguments. It doesn't have any rate-limiting benefits, because the query complexities are still added together.

The syntax for submitting multiple queries has four key points:

  • Declare at the top whether the operations are queries or mutations
  • Give each operation a custom alias by using the syntax <your-custom-name>: <query field or mutation name> (<arguments>)
  • The operations do not have to be the same
  • Each operation must select the fields that it wants to return

The following example uses multiple customerUpdate mutations to set three different tags on three different customers in one request.

POST /admin/api/2019-07/graphql.json

mutation {
  VipGold: customerUpdate(
    input: {
      id: "gid://shopify/Customer/1322001989654",
      tags: ["Gold"]
    }
  )
  {
    customer {
      tags
    }
  }
  VipPlatinum: customerUpdate(
    input: {
      id: "gid://shopify/Customer/774173917206",
      tags: ["Platinum"]
    }
  )
  {
    customer {
      tags
    }
  }
  VipDiamond: customerUpdate(
    input: {
      id: "gid://shopify/Customer/773091000342",
      tags: ["Diamond"]
    }
  )
  {
    customer {
      tags
    }
  }
}

JSON response

{
  "data": {
    "VipGold": {
      "customer": {
        "tags": [
          "Gold"
        ]
      }
    },
    "VipPlatinum": {
      "customer": {
        "tags": [
          "Platinum"
        ]
      }
    },
    "VipDiamond": {
      "customer": {
        "tags": [
          "Diamond"
        ]
      }
    }
  },
  ...
}

Run in GraphiQL

Next steps

Sign up for a Partner account to get started.

Sign up