Current section

Files

Jump to
ex_shopify_schema lib ex_shopify_schema graphql id.ex
Raw

lib/ex_shopify_schema/graphql/id.ex

defmodule ExShopifySchema.Graphql.ID do
@moduledoc "Represents a unique identifier, often used to refetch an object.\nThe ID type appears in a JSON response as a String, but it is not intended to be human-readable.\n\nExample value: `\"gid://shopify/Product/10079785100\"`\n"
@type t() :: String.t()
use Ecto.Type
@impl Ecto.Type
def type, do: :string
@impl Ecto.Type
def cast(nil), do: {:ok, nil}
def cast(id) when is_binary(id), do: {:ok, id}
def cast(_data), do: :error
@impl Ecto.Type
def load(data), do: cast(data)
@impl Ecto.Type
def dump(nil), do: {:ok, nil}
def dump(id) when is_binary(id), do: {:ok, id}
def dump(_data), do: :error
@doc """
Converts a Shopify Global ID (GID) to an integer.
The GID is expected to be in the format "gid://shopify/<resource_type>/<id>".
## Examples
iex> ExShopifySchema.Graphql.ID.to_integer!("gid://shopify/Product/123456789")
123456789
"""
@spec to_integer!(String.t()) :: integer()
def to_integer!("gid://shopify/" <> resource_parts) do
[_resource_type, id] = String.split(resource_parts, "/")
String.to_integer(id, 10)
end
end