graphql

graphql is one of the most important filters. It calls Shopify GraphQL endpoint to fetch (query) or update (mutation) store data. Any Shopify GraphQL request can be used. Complete documentation of Shopify's GraphQL can be found here:

Example

Let's add a tag to a customer.

First, using Shopify GraphQL reference, we need to find mutation that will allow us to do so. Use capture tag to define our mutation.

{% capture mutation %} 
  mutation tagsAdd($id: ID!, $tags: [String!]!) {
    tagsAdd(id: $id, tags: $tags) {
      node {
        id
      }
      userErrors {
        field
        message
      }
    }
  }
{% endcapture %}

From the above we see that mutation needs an object id and tag we want to add. Let's use again capture tag to define our mutation variables.

{% json variables %}
  {
    "id": "gid://shopify/Customer/123456",
    "tags": "datajet-tag"
  }
{% endjson %}

Before we execute above GraphQL mutation we need to tell compiler what to expect in order to evaluate required permission for executing this task. See more here.

{%- comment -%} Feed compiler with dummy values to evaluate permissions required.{%- endcomment -%}
{% if mode.compiler %}
  {% json dummy_mutation_variables %}
    {
      "id": "gid://shopify/Customer/123456",
      "tags": "test"
    }
  {% endjson %}
  {% assign result = mutation | graphql: dummy_mutation_variables %}
{% endif %}
{%- comment -%}END REST and GraphQL definitions{%- endcomment -%}

Now we have everything. Last step is to use graphql filter to execute the query.

{% assign result = mutation | graphql: variables %}
{{result | log }}

Last updated