Skip to main content

Add extra fields to product query using fieldsHelper

Description

​ This function is used to add extra fields to the product query. You can refer this links for the available fields . ​

Dependencies

json-to-graphql-query package

We use this package to convert the object to graphql query. The current fields you will be getting in the fields object. And then you can addFieldsToProduct function to add extra fields to the product query. Example is given below. ​ ​

Parameters

ParameterTypeDescription
fieldsObjectThis object will be merged with the existing product query.

Sample usage

In this example we are adding the publishedAt, seo, collections and custom_products_list fields to the product query. ​ Like the above you can add any fields to the product query based on the available fields in the product object. ​ ​

import { fieldsHelper } from '@appmaker-xyz/shopify';

// you can use this from the theme activate function.
fieldsHelper.addFieldsToProduct({
fields: {
// add any fields in the required format
publishedAt: true, // adding a simple field
seo: {
title: true,
description: true,
}, // adding a nested object
collections: {
__args: {
first: 100,
},
edges: {
node: {
id: true,
title: true,
handle: true,
},
},
}, // adding a collection field with arguments
custom_products_list: {
__aliasFor: 'metafield',
__args: {
key: 'pair_with',
namespace: 'custom',
},
value: true,
}, // adding a metafield and assigning it to a custom_products_list alias
},
});