shopify.graphql

This action sends a GraphQL query or mutation (update) to Shopify.

Parameters

url: string API endpoint to call (optional)
Defaults to /admin/api/LOWEST_SUPPORTED_VERSION/graphql.json
query: string A GraphQL expression that can be either:
  • A string containing a query or a mutation
  • An object containing:
    - query: string - The GraphQL query or mutation
    - variables: object - Variables used in the query (optional)

Returns

Returns a Promise resolving to a GraphQL response object.

The response object contains:

  • data: The requested or updated resource

Examples

Query the name of the shop

Basic string query using the default endpoint:

Query the name of the shop
module.exports = async function(payload, actions, context) {
	const shopResult = await actions.shopify.graphql(
		'query { shop { name } }'
	);

	console.log('Shop name: ', shopResult.data.shop.name)
}

Query the 'createdAt' and 'description' fields of a product

Parameterized string query using the default endpoint:

Query the 'createdAt' and 'description' fields of a product
module.exports = async function(payload, actions, context) {
	const qId = 'gid://shopify/Product/6705843699795';
	const qRes = await actions.shopify.graphql(`query {
		product(id: "${qId}") {
    	createdAt, description
  	}
	}`);

	console.log('Product: ', qRes);
}

Update the 'description' and 'tags' fields of a product

Parameterized string mutation with explicit endpoint:

Update the 'description' and 'tags' fields of a product
module.exports = async function(payload, actions, context) {
	const mId = 'gid://shopify/Product/6705843699795';

	const mRes = await actions.shopify.graphql(
		'/admin/api/2024-04/graphql.json',
		`mutation {
			productUpdate(input: {id: "${mId}",     
				descriptionHtml: "Modified description", 
				tags: ["modifiedProduct"]  
			}) {
				product { descriptionHtml, tags }
			}
		}`
	);

	console.log('Update result: ', mRes);
}

Create a product

Mutation using query and variables with an explicit endpoint:

Create product
module.exports = async function(payload, actions, context) {
    const query = `mutation CreateProduct($input: ProductInput!) { 
      productCreate(input: $input) { 
        product { 
          id 
          title 
          
        } 
        userErrors { 
          field 
          message 
          
        } 
      } 
    }`;
    const variables = {
        input: {
            title: payload.title,
            descriptionHtml: payload.description,
            tags: payload.tags.split(',').map(tag => tag.trim()),
        }
    };
    
    const result = await actions.shopify.graphql(
      '/admin/api/2024-04/graphql.json',
      {query, variables);
    
    if (result.data.productCreate.userErrors.length > 0) {
        throw new Error(`Product creation failed: ${result.data.productCreate.userErrors.map(err => err.message).join(', ')}`);
    }
};