From 8a9087878487585c5a9b81d6b8b3bc3773c1f5c5 Mon Sep 17 00:00:00 2001 From: Kevin Pratt Date: Thu, 1 Feb 2024 15:20:16 -0700 Subject: [PATCH] Tweaks to allow for single instance models --- lib/dato_cms_graphql.rb | 4 +++ lib/dato_cms_graphql/graphql_base.rb | 39 +++++++++++++++++++++++++--- lib/dato_cms_graphql/version.rb | 2 +- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/dato_cms_graphql.rb b/lib/dato_cms_graphql.rb index 2ea2d3f..09a9311 100644 --- a/lib/dato_cms_graphql.rb +++ b/lib/dato_cms_graphql.rb @@ -45,6 +45,10 @@ def self.query(query, variables: {}) end end + def self.query_one(query, variables: {}) + Client.query(query, variables: variables).data.item.to_h.deep_transform_keys { |k| k.underscore } + end + def self.count(query, variables: {}) Client.query(query, variables: variables).data.meta_data.count end diff --git a/lib/dato_cms_graphql/graphql_base.rb b/lib/dato_cms_graphql/graphql_base.rb index 8b76140..b6904ac 100644 --- a/lib/dato_cms_graphql/graphql_base.rb +++ b/lib/dato_cms_graphql/graphql_base.rb @@ -4,11 +4,17 @@ module DatoCmsGraphql class GraphqlBase class_attribute :graphql_page_size class_attribute :fields + class_attribute :graphql_single_instance + class << self def page_size(value) self.graphql_page_size = value end + def single_instance(value) + self.graphql_single_instance = value + end + def query_name to_s.humanize.pluralize end @@ -20,8 +26,9 @@ def graphql_fields(*args) def initialize_queries class_eval do - const_set(:INDEX, query_for) - const_set(:META_DATA, meta_data_query) + const_set(:SINGLE, query_for_single) if single_instance? + const_set(:INDEX, query_for) unless single_instance? + const_set(:META_DATA, meta_data_query) unless single_instance? end end @@ -29,6 +36,12 @@ def plural_name to_s.pluralize end + def single_name + rv = to_s + rv[0] = rv[0].downcase + rv + end + def parse(query) DatoCmsGraphql::Client.parse(query) end @@ -43,6 +56,16 @@ def query_for GRAPHQL end + def query_for_single + parse <<~GRAPHQL + query { + item: #{single_name} { + #{fields} + } + } + GRAPHQL + end + def meta_data_query parse <<~GRAPHQL query { @@ -54,18 +77,28 @@ def meta_data_query end def all + raise "This is a single instance model" if single_instance? ModelIterator.new(self) end + + def get + new(DatoCmsGraphql.query_one(self::SINGLE)) + end + + def single_instance? + graphql_single_instance || false + end end attr_reader :attributes page_size(100) # Set the maximum page size as default. + single_instance(false) def initialize(attributes) @attributes = JSON.parse(attributes.to_json, object_class: OpenStruct) end - def respond_to_missing?(method) + def respond_to_missing?(method, *args) @attributes.respond_to?(method) end diff --git a/lib/dato_cms_graphql/version.rb b/lib/dato_cms_graphql/version.rb index 4a04dd8..bbdf4d7 100644 --- a/lib/dato_cms_graphql/version.rb +++ b/lib/dato_cms_graphql/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module DatoCmsGraphql - VERSION = "0.1.1" + VERSION = "0.1.2" end