Class: ActiveShopifyGraphQL::Model

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::AttributeAssignment, ActiveModel::Validations, Associations, Attributes, Connections, FinderMethods, GraphqlTypeResolver, LoaderSwitchable, MetafieldAttributes
Defined in:
lib/active_shopify_graphql/model.rb

Overview

Base class for all GraphQL-backed models.

Models should inherit from this class (typically via an ApplicationShopifyGqlRecord intermediate class) to gain ActiveRecord-like functionality for Shopify GraphQL APIs.

Examples:

Creating an ApplicationShopifyGqlRecord base class

class ApplicationShopifyGqlRecord < ActiveShopifyGraphQL::Model
  attribute :id, transform: ->(id) { id.split("/").last }
  attribute :gid, path: "id"
end

Defining a model

class Customer < ApplicationShopifyGqlRecord
  graphql_type "Customer"

  attribute :first_name
  attribute :email, path: "defaultEmailAddress.emailAddress"
end

Defined Under Namespace

Modules: Associations, Attributes, Connections, FinderMethods, GraphqlTypeResolver, LoaderSwitchable, MetafieldAttributes

Instance Method Summary collapse

Methods included from LoaderSwitchable

#with_admin_api, #with_customer_account_api, #with_loader

Methods included from Connections

#populate_inverse_cache_for_connection

Methods included from GraphqlTypeResolver

resolve

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.



36
37
38
39
40
41
42
43
44
# File 'lib/active_shopify_graphql/model.rb', line 36

def initialize(attributes = {})
  # Extract connection cache if present and populate inverse caches
  if attributes.key?(:_connection_cache)
    @_connection_cache = attributes.delete(:_connection_cache)
    populate_inverse_caches_on_initialization
  end

  assign_attributes(attributes)
end