How to use runDataSource function to run a gqlQuery function
This document describes how to use runDataSource function to run gqlQuery method of dataSource object.
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
- Import
runDataSourcefunction from@appmaker-xyz/corepackage.
import { runDataSource } from "@appmaker-xyz/core";- Create dataSource object with
attributesandsourceproperties.
const dataSource = {
attributes: {},
source: "shopify",
};- Call
runDataSourcefunction withdataSourceobject andmethodNameasgqlQueryandparamsproperties.
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);- 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"
}
}
]
}
}