Skip to main content

How to use runDataSource function to run a gqlQuery function

Description

​ This document describes how to use runDataSource function to run gqlQuery method of dataSource object. This is for developers who want to use custom graphql queries to fetch data from shopify dataSource. ​

Steps

​ ​

  1. Import runDataSource function from @appmaker-xyz/core package.
import { runDataSource } from "@appmaker-xyz/core";

​ 2. Create dataSource object with attributes and source properties.

const dataSource = {
attributes: {},
source: "shopify",
};

​ 3. Call runDataSource function with dataSource object and methodName as gqlQuery and params properties. ​

const [response] = await runDataSource(
{
dataSource,
},
{
methodName: "gqlQuery",
params: {
query: `
query getMediaImages($ids: [ID!]!) {
nodes(ids: $ids) {
... on MediaImage {
alt
id
image {
url(transform:{maxHeight: 800, maxWidth: 800})
}
}
}
}
`,
variables: {
"ids": [
"gid://shopify/MediaImage/27773354672290",
"gid://shopify/MediaImage/27569974116510",
"gid://shopify/MediaImage/27505126113438"
]
}
},
}
);

console.log(response);

​ 4. You will get the response as below.

{
"data": {
"nodes": [
{
"alt": "alt text",
"id": "gid://shopify/MediaImage/27773354672290",
"image": {
"url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/files/1_800x.jpg?v=1588080000"
}
},
{
"alt": "alt text",
"id": "gid://shopify/MediaImage/27569974116510",
"image": {
"url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/files/2_800x.jpg?v=1588080000"
}
},
{
"alt": "alt text",
"id": "gid://shopify/MediaImage/27505126113438",
"image": {
"url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/files/3_800x.jpg?v=1588080000"
}
}
]
}
}