Skip to content

Commit

Permalink
Filters: search links query
Browse files Browse the repository at this point in the history
  • Loading branch information
ztratify committed Dec 2, 2020
1 parent a8c7107 commit ac56701
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ gem 'jbuilder', '~> 2.7'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

# START: ADDED GEMS

# Graphql rails
gem 'graphql', '1.9.17'

# Ensure secure passwords
gem 'bcrypt', '~> 3.1.13'

# Graphiql GUI
gem 'graphiql-rails', '1.7.0', group: :development

# Filter mutations (for search)
gem 'search_object_graphql', '0.3.1'

# END: ADDED GEMS

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand All @@ -41,6 +51,3 @@ end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

# Graphiql GUI
gem 'graphiql-rails', '1.7.0', group: :development
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ GEM
sprockets (> 3.0)
sprockets-rails
tilt
search_object (1.2.4)
search_object_graphql (0.3.1)
graphql (~> 1.8)
search_object (~> 1.2.2)
spring (2.1.1)
spring-watcher-listen (2.0.1)
listen (>= 2.7, < 4.0)
Expand Down Expand Up @@ -181,6 +185,7 @@ DEPENDENCIES
puma (~> 4.1)
rails (~> 6.0.3, >= 6.0.3.4)
sass-rails (>= 6)
search_object_graphql (= 0.3.1)
spring
spring-watcher-listen (~> 2.0.0)
sqlite3 (~> 1.4)
Expand Down
40 changes: 40 additions & 0 deletions app/graphql/resolvers/search_links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'search_object'
require 'search_object/plugin/graphql'

class Resolvers::SearchLinks
# include SearchObject for GraphQL
include SearchObject.module(:graphql)

# scope is starting point for search
scope { Link.all }

type [Types::LinkType]

# inline input type definition for the advanced filter
class LinkFilter < ::Types::BaseInputObject
argument :OR, [self], required: false
argument :description_contains, String, required: false
argument :url_contains, String, required: false
end

# when "filter" is passed "apply_filter" would be called to narrow the scope
option :filter, type: LinkFilter, with: :apply_filter

# apply_filter recursively loops through "OR" branches
def apply_filter(scope, value)
branches = normalize_filters(value).reduce { |a, b| a.or(b) }
scope.merge branches
end

def normalize_filters(value, branches = [])
scope = Link.all
scope = scope.where('description LIKE ?', "%#{value[:description_contains]}%") if value[:description_contains]
scope = scope.where('url LIKE ?', "%#{value[:url_contains]}%") if value[:url_contains]

branches << scope

value[:OR].reduce(branches) { |s, v| normalize_filters(v, s) } if value[:OR].present?

branches
end
end
1 change: 1 addition & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class QueryType < BaseObject
# queries are just represented as fields
# `all_links` is automatically camelcased to `allLinks`
field :all_links, [LinkType], null: false
field :search_links, resolver: Resolvers::SearchLinks

# this method is invoked, when `all_link` fields is being resolved
def all_links
Expand Down

0 comments on commit ac56701

Please sign in to comment.