Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/shopify

src/graphql.ts

48 lines · 1.2 KB · TypeScript
import { shopifyGraphql, shopifyGraphqlRaw } from './lib/client.ts'
import type { ShopifyClientOptions } from './types.ts'

export type ShopifyGraphqlParams = ShopifyClientOptions & {
	query: string
	variables?: Record<string, unknown>
	/**
	 * When true, return `{ data, errors, extensions, shop }` instead of
	 * throwing on GraphQL `errors`. HTTP failures still throw.
	 */
	raw?: boolean
}

/**
 * Run one Admin GraphQL operation.
 *
 * Prefer this for new work — Shopify's REST Admin API is legacy.
 *
 * @example
 * import { shopifyGraphql } from 'kody:@kody/shopify/graphql'
 * const data = await shopifyGraphql({
 *   shop: 'acme',
 *   query: 'query { shop { name myshopifyDomain } }',
 * })
 *
 * @see https://shopify.dev/docs/api/admin-graphql
 */
export async function graphql(params: ShopifyGraphqlParams) {
	if (params.raw) {
		return await shopifyGraphqlRaw({
			shop: params.shop,
			apiVersion: params.apiVersion,
			query: params.query,
			variables: params.variables,
		})
	}
	return await shopifyGraphql({
		shop: params.shop,
		apiVersion: params.apiVersion,
		query: params.query,
		variables: params.variables,
	})
}

export { shopifyGraphql, shopifyGraphqlRaw }

/** Default export: GraphQL helper. */
export default graphql