pify/storefront

Shopify Storefront GraphQL API client and the values its reads share.

The Storefront API is the buyer-facing API: products, collections and the cart. A public token is safe in a browser. A private token belongs on a server, which forwards the buyer’s IP.

Types

The buyer’s IPv4 or IPv6 address. Take it from the trusted proxy’s view of the client, never from a raw X-Forwarded-For the buyer can write.

pub opaque type BuyerIp
pub type BuyerIpError {
  BuyerIpInvalid(text: String)
}

Constructors

  • BuyerIpInvalid(text: String)
pub type Image {
  Image(
    url: String,
    alt_text: option.Option(String),
    width: option.Option(Int),
    height: option.Option(Int),
  )
}

Constructors

pub type Metafield {
  Metafield(
    namespace: String,
    key: String,
    value: String,
    value_type: String,
  )
}

Constructors

  • Metafield(
      namespace: String,
      key: String,
      value: String,
      value_type: String,
    )

A metafield a read asks for. The Storefront API answers only the metafields the shop exposes to it.

pub type MetafieldIdentifier {
  MetafieldIdentifier(namespace: String, key: String)
}

Constructors

  • MetafieldIdentifier(namespace: String, key: String)

One page of a list read.

pub type Page {
  Page(first: Int, after: option.Option(String))
}

Constructors

pub type SelectedOption {
  SelectedOption(name: String, value: String)
}

Constructors

  • SelectedOption(name: String, value: String)
pub type Seo {
  Seo(
    title: option.Option(String),
    description: option.Option(String),
  )
}

Constructors

Only new builds one, so the host a token goes to is always checked.

pub opaque type StorefrontClient
pub type StorefrontDomainError {
  StorefrontDomainInvalid(domain: String)
}

Constructors

  • StorefrontDomainInvalid(domain: String)

    Not a host name of dot-separated labels of letters, digits and inner hyphens. Holds the normalised text.

pub type StorefrontToken {
  PublicToken(token: String)
  PrivateToken(token: String, buyer_ip: option.Option(BuyerIp))
}

Constructors

  • PublicToken(token: String)

    Sent as X-Shopify-Storefront-Access-Token. Safe in a browser.

  • PrivateToken(token: String, buyer_ip: option.Option(BuyerIp))

    Sent as Shopify-Storefront-Private-Token, from a server only. The buyer’s IP rides Shopify-Storefront-Buyer-IP, so Shopify throttles each buyer apart.

Values

pub fn buyer_ip_to_string(buyer_ip: BuyerIp) -> String
pub const default_timeout_ms: Int
pub fn endpoint_url(client: StorefrontClient) -> String
pub fn graphql(
  client: StorefrontClient,
  query: String,
  variables: json.Json,
) -> request.ShopifyRequest
pub fn image_decoder() -> decode.Decoder(Image)
pub const image_fields: String
pub const max_page_size: Int
pub fn metafield_identifiers_to_json(
  identifiers: List(MetafieldIdentifier),
) -> json.Json
pub fn metafields_decoder() -> decode.Decoder(List(Metafield))

A metafield the shop does not have reads as null and is left out.

pub const metafields_fields: String

Selects the metafields named by the $identifiers variable.

pub fn new(
  token token: StorefrontToken,
  shop_domain shop_domain: String,
  version version: api_version.ApiVersion,
) -> Result(StorefrontClient, StorefrontDomainError)

shop_domain is <handle>.myshopify.com or the shop’s own domain.

pub fn page_variables(
  first: Int,
  after: option.Option(String),
  arguments: List(#(String, json.Json)),
) -> json.Json

first and after, then arguments. first is held between 1 and max_page_size.

pub fn parse_buyer_ip(
  text: String,
) -> Result(BuyerIp, BuyerIpError)

Accepts hex digits, . and : only, so the value cannot break a header.

pub fn parse_domain(
  text: String,
) -> Result(String, StorefrontDomainError)

Lowercases and trims the text and strips a scheme and trailing slashes. Anything that could move the request to another host, such as /, @, :, ? or #, is refused.

pub const selected_options_fields: String
pub fn seo_decoder() -> decode.Decoder(Seo)
pub const seo_fields: String
pub fn with_timeout(
  client: StorefrontClient,
  timeout_ms: Int,
) -> StorefrontClient
✨ Search Document