This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rake monetize:verify | |
namespace :monetize do | |
desc "Verifies if all monetize attributes are set" | |
task verify: :environment do | |
ActiveRecord::Base.connection.tables.sort.map do |model| | |
next if %w(schema_migrations ar_internal_metadata versions).include?(model) | |
puts "Scanning: #{model}" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Types::BaseConnection < GraphQL::Types::Relay::BaseConnection | |
field :total_count, Integer, null: false | |
def total_count | |
object.items.size | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Monkey Patch: Add a count field to all connections by default. | |
# Note, I tried to make this nicer, where it would just call the original method and then add a count field, | |
# but the challenge outlasted my patience. So I just replaced the whole method. TODO: better way to do this | |
module GraphQL | |
module Relay | |
module ConnectionType | |
def self.create_type(wrapped_type, edge_type: wrapped_type.edge_type, edge_class: GraphQL::Relay::Edge, nodes_field: ConnectionType.default_nodes_field, &block) | |
custom_edge_class = edge_class | |
# Any call that would trigger `wrapped_type.ensure_defined` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define an instrumentation that performs the caching | |
class CachingInstrumentation | |
def instrument(_type, field) | |
return field unless field.metadata.include?(:cache_proc) | |
old_resolver = field.resolve_proc | |
new_resolver = -> (obj, args, ctx) do | |
# Get the caching key | |
cache_key = field.metadata[:cache_proc].call(obj, args, ctx) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define an instrumentation that performs the caching | |
class CachingInstrumentation | |
def instrument(_type, field) | |
return field unless field.metadata.include?(:cache_proc) | |
old_resolver = field.resolve_proc | |
new_resolver = -> (obj, args, ctx) do | |
# Get the caching key | |
cache_key = field.metadata[:cache_proc].call(obj, args, ctx) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Resolvers | |
class UsersResolver < Resolvers::BaseSearchResolver | |
create_connection_for(User) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'search_object' | |
require 'search_object/plugin/graphql' | |
module Resolvers | |
class BaseSearchResolver | |
include ::SearchObject.module(:graphql) | |
def self.create_connection_for(klass) | |
plural = ActiveSupport::Inflector.pluralize(klass.to_s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Resolvers | |
class UsersResolver < Resolvers::BaseSearchResolver | |
type Types::UserType.connection_type, null: false | |
scope { object.respond_to?(:users) ? object.users : User.none } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Resolvers | |
class UsersResolver < Resolvers::BaseSearchResolver | |
type Types::UserType.connection_type, null: false | |
scope { context[:current_user].admin? ? User.all : User.none } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ApplicationRecord | |
searchkick | |
def search_data | |
attributes.slice(*%w[first_name last_name email]) | |
end | |
end |