How to query Shopify with the GraphQL API and Insomnia

Development
Friday, September 4, 2020

What problem do we solve

Shopify provides an API that can be used to implement:

  • A mobile application connected to a Shopify Front Store
  • An integration pipeline with an external system such as a product management systems (PIM) or an enterprise resource planning (ERP)

This article explains how to use Insomnia Core to query or update the data stored in Shopify using the GraphQL. Insomnia can help you to generate code for languages such as C#, Javascript, C, Swift ...

Insomnia Core a powerful API client developed by the company Kong

The graphQL API

The Shopify test API is available at this EndPoint:

1https://shopify.dev/tools/graphiql-admin-api

This endpoint can be used to experiment and play with Shopify GraphQL API.

i.png

Below a GraphQL query that returns the first 10 products:

1{
2 products(first: 2) {
3 pageInfo {
4 hasNextPage
5 hasPreviousPage
6 }
7 edges {
8 cursor
9 node {
10 handle
11 descriptionHtml
12 }
13 }
14 }
15}

Result:

1{
2 "data": {
3 "products": {
4 "pageInfo": {
5 "hasNextPage": true,
6 "hasPreviousPage": false
7 },
8 "edges": [
9 {
10 "cursor": "eyJsYXN0X2lkIjoxOTc0MjA4Mjk5MDMwLCJsYXN0X3ZhbHVlIjoiMTk3NDIwODI5OTAzMCJ9",
11 "node": {
12 "handle": "rough-snowflake-camisole",
13 "descriptionHtml": ""
14 }
15 },
16 {
17 "cursor": "eyJsYXN0X2lkIjoxOTc0MjA4MzMxNzk4LCJsYXN0X3ZhbHVlIjoiMTk3NDIwODMzMTc5OCJ9",
18 "node": {
19 "handle": "wandering-thunder-blouse",
20 "descriptionHtml": ""
21 }
22 }
23 ]
24 }
25 },
26 "extensions": {
27 "cost": {
28 "requestedQueryCost": 4,
29 "actualQueryCost": 4,
30 "throttleStatus": {
31 "maximumAvailable": 1000,
32 "currentlyAvailable": 996,
33 "restoreRate": 50
34 }
35 }
36 }
37}

The GraphQL API in Shopify for production is available at this endpoint:

1https://**{{ SHOPIFY_SHOP }}**.myshopify.com/admin/api/2020-07/graphql.json

{{ SHOPIFY_SHOP }} represents the name of the Shopify instance. This endpoint is secure by default.

In order to query Shopify from an application such as a Web Application or a mobile application we need to create a Private Application.

Creation of a "Private Application" in Shopify

A link "Managed private apps" is available in the "Apps" section of Shopify.

i1.png

A private application is characterized by:

  • The name of the developer

    • The email of the developer for support
  • A list of permissions

    The Read / Write access policy on each entity defined in the Shopify Back-office

    • Customers
    • Orders
    • Products
    • Price Rules
    • Analytics
      • Shippings

The API is divided into 2 parts:

  • The admin API that gives access to all the entities
  • The storefront API used to develop customized customer-facing experiences on web or mobile

Creation of a private application

In Apps click on the button "Create new private app"

i2.png

Enter the name of the application and the e-mail support

i3.png

Configure the security policy for each entity

i4.png

Click save and create App on the dialog.

Obtain the security parameters

i5.png

  • API Key : represents the logical user identity of the application
  • Password : the password used by the application
  • Shared Secret : used a token in each programmatic call

Once we have an application we can use an API client to test and create our graphQL queries.

Insomnia Core

Insomnia Core is a very powerful Desktop API Client for REST and GraphQL tool to explore and test REST and GraphQL Apis. Insomnia can be downloaded at : https://insomnia.rest/

6.png

Workspace creation in Insomnia

Click create new "Workspace"

7.png

Workspaces are a core concept used for isolating projects within Insomnia.

  • The variable and secrets shared can be stored in an Environment.
  • The environment can be used to store environment variables, that are referenced and used by each query in a workspace.

For example for our example we store the following definitions:

1{
2 "SHOPIFY_SHOP": "test",
3 "SHOPIFY_ACCESS_TOKEN": "FF",
4 "SHOPIFY_API_KEY": "X",
5 "SHOPIFY_PASSWORD": "X"
6}

Create of a GraphQL in a Workspace

In our Workspace choose the action button create "New Request"

08.png

Edit the name of the request and chose "POST" method:

09.png

Type the URL

10.png

SHOPIFY_SHOP represents the variable we have created in the Environment.

Configure the GraphQL Query

1{
2 products(first: 3) {
3 edges {
4 cursor
5 node {
6 handle
7 title
8 }
9 }
10 }
11}

11.png

Configure the security with Basic authentication

Choose the variable "SHOPIFY_API_KEY" and "SHOPIFY_PASSWORD" for username and password

12.png

Create a Header in the query

13.png

Configure the query to disable the cookies 🍪

A right-click on the query definition can be used to open the settings of the query

14.png

Send cookies and store cookies 🍪 must be disabled 🔥

15

We can now test our query with a click on the send button ▶️

i16.png

Et Voila 🥳🚀

Generate a query for your target language

😎 A cool feature of Insomnia is the code generation of a client

17

18

Subscribe to our Newsletter

We deliver high quality blog posts written by professionals monthly. And we promise no spam.

elitizon ltd.

© 2020 elitizon ltd. All Rights Reserved.