GraphQL Shop Policies

Solved
Tourist
9 0 3

I'm using the Shopify GraphQL Admin API to retrieve my Shop Policies.

  • Refund Policy
  • Privacy Policy
  • Terms of Service
  • Shipping Policy

I've installed the Shopify GraphiQL App and I'm unable to retrieve any of the shop policies. I've granted full read access to all scopes of the API.

 

My query looks like this;

{
    shop {
        privacyPolicy {
            body
            id
            title
            url
        }
        refundPolicy {
            body
            id
            title
            url
        }
        shippingPolicy {
            body
            id
            title
            url
        }
        termsOfService {
            body
            id
            title
            url
        }
    }
}

Response is as follows:

{
  "errors": [
    {
      "message": "Field 'privacyPolicy' doesn't exist on type 'Shop'",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "fields": [
        "query",
        "shop",
        "privacyPolicy"
      ]
    },
    {
      "message": "Field 'refundPolicy' doesn't exist on type 'Shop'",
      "locations": [
        {
          "line": 9,
          "column": 5
        }
      ],
      "fields": [
        "query",
        "shop",
        "refundPolicy"
      ]
    },
    {
      "message": "Field 'shippingPolicy' doesn't exist on type 'Shop'",
      "locations": [
        {
          "line": 15,
          "column": 5
        }
      ],
      "fields": [
        "query",
        "shop",
        "shippingPolicy"
      ]
    },
    {
      "message": "Field 'termsOfService' doesn't exist on type 'Shop'",
      "locations": [
        {
          "line": 21,
          "column": 5
        }
      ],
      "fields": [
        "query",
        "shop",
        "termsOfService"
      ]
    }
  ]
}

Anyone know what's up?

0 Likes
Highlighted
Shopify Staff
Shopify Staff
53 3 15

Hey @bradgarropy !

 

The Shop object in the GraphQL Admin API doesn't have these fields.

However, the Shop object in the GraphQL Storefront API has all of these, minus shippingPolicy.

 

If you're using the Shopify GraphiQL App from our GraphQL Admin API Explorer docs, it will only have access to the admin side of things.

 

To see the GraphQL Storefront API in action, you can check out the in-browser demo we have in the GraphQL Storefront API Explorer docs.

 

These are two different APIs, both accessible with GraphQL.

It can be a little unclear which API a particular doc is referring to, especially since we use the same "GraphQL explorer" terminology for both.

1 Like
Highlighted
Tourist
9 0 3

Great answer @Alex_B, thank you!

 

So if the GraphQL Storefront API does not provide access to the Shipping Policy, what API can I use to fetch that data?

0 Likes
Highlighted
Shopify Staff
Shopify Staff
53 3 15

Hey @bradgarropy 

 

You can also access all the policies directly through the REST Admin API's policies.json endpoint.

Your app can use the same token/authorization to access both the GraphQL and REST admin APIs, so this is probably easier than trying to also get a storefront access token.

0 Likes
Highlighted
Shopify Partner
1838 174 615

Or in case you need this only for storefront, i.e. won't be using Admin APIs at all (for whatever reason) then you can create a no layout page template and 

 

{%- layout none -%}
{%- assign policy_url_parts = shop.shipping_policy.url | split: "/" -%}
{
{%- if shop.shipping_policy %}
  "shop": {
    "shippingPolicy": {
      "id": "{{ shop.shipping_policy.id }}",
      "handle": "{{ policy_url_parts.last }}",
      "title": "{{ shop.shipping_policy.title | escape }}",
      "body": "{{ shop.shipping_policy.body | escape }}",
      "url": "{{ shop.shipping_policy.url }}",
    }
  }
{% endif -%}
}

Bob's your uncle too ;-)

I turn coffee in to code - since 1998
0 Likes
Highlighted
Tourist
9 0 3

@Alex_B,

Is there any plan to add Shipping Policy to the GraphQL Storefront API? Seems like a natural fit there alongside the other policies.

 

Just asking because I'm using Gatsby to create an online store and the gatsby-source-shopify plugin uses only the GraphQL Storefront API to retrieve data.

0 Likes
Highlighted

Success.

Shopify Partner
1838 174 615

In Gatsby you can use multiple sources i.e. have the Shopify source for the bulk of data and use the gatsby-source-graphql or gatsby-source-api to use Shopify GraphQL Admin API or REST Admin API respectively. Using admin APIs in this case would make sense and be safe given that Gatsby build-time SSB. With the source-api plugin, you could also use the storefront itself (the one hosted by Shopify with good ol' Liquid) as that comes with some nifty data endpoints too and you can make your own "faux" data endpoints as shown above - these come in handy as Storefront API will at times be a dead end.

I turn coffee in to code - since 1998
0 Likes
Highlighted
Tourist
9 0 3

@KarlOffenberger,

This is a great answer!

 

It looks like the GraphQL Admin API doesn't yet support Shop Policies.

So I'll go ahead and use the gatsby-source-apiserver plugin to pull in the Shop Policies from the REST Admin API.

 

Thank you everyone for your help!

2 Likes