Module: ActiveShopifyGraphQL::GidHelper

Defined in:
lib/active_shopify_graphql/gid_helper.rb

Overview

Helper module for handling Shopify Global IDs (GIDs) Provides utilities for parsing and building GIDs according to the URI::GID standard

Class Method Summary collapse

Class Method Details

.normalize_gid(id, model_name) ⇒ String

Normalize an ID value to a proper Shopify GID format If the ID is already a valid GID, returns it as-is Otherwise, builds a new GID using the provided model name

Examples:

normalize_gid(123, "Customer")
# => "gid://shopify/Customer/123"

normalize_gid("gid://shopify/Customer/123", "Customer")
# => "gid://shopify/Customer/123"

Parameters:

  • The ID value (can be numeric or existing GID)

  • The GraphQL type name (e.g., "Customer", "Order")

Returns:

  • The normalized GID in format "gid://shopify/ModelName/id"



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_shopify_graphql/gid_helper.rb', line 24

def self.normalize_gid(id, model_name)
  # Check if id is already a valid GID
  begin
    parsed_gid = URI::GID.parse(id)
    return id if parsed_gid
  rescue URI::InvalidURIError, URI::BadURIError, ArgumentError
    # Not a valid GID, proceed to build one
  end

  # Build GID from the provided ID and model name
  URI::GID.build(app: "shopify", model_name: model_name, model_id: id).to_s
end

.valid_gid?(value) ⇒ Boolean

Check if a value is a valid Shopify GID

Examples:

valid_gid?("gid://shopify/Customer/123")
# => true

valid_gid?("123")
# => false

Parameters:

  • The value to check

Returns:

  • true if the value is a valid GID, false otherwise



49
50
51
52
53
54
# File 'lib/active_shopify_graphql/gid_helper.rb', line 49

def self.valid_gid?(value)
  URI::GID.parse(value)
  true
rescue URI::InvalidURIError, URI::BadURIError, ArgumentError
  false
end