search_object_graphql 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.projections.json +8 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +39 -0
  6. data/.travis.yml +11 -0
  7. data/CHANGELOG.md +5 -0
  8. data/Gemfile +4 -0
  9. data/LICENSE.txt +22 -0
  10. data/README.md +194 -0
  11. data/Rakefile +8 -0
  12. data/example/.gitignore +20 -0
  13. data/example/.ruby-version +1 -0
  14. data/example/Gemfile +22 -0
  15. data/example/README.md +83 -0
  16. data/example/Rakefile +6 -0
  17. data/example/app/controllers/application_controller.rb +2 -0
  18. data/example/app/controllers/graphql_controller.rb +29 -0
  19. data/example/app/graphql/resolvers/base_search_resolver.rb +9 -0
  20. data/example/app/graphql/resolvers/category_search.rb +35 -0
  21. data/example/app/graphql/resolvers/post_search.rb +69 -0
  22. data/example/app/graphql/schema.rb +3 -0
  23. data/example/app/graphql/types/.keep +0 -0
  24. data/example/app/graphql/types/category_type.rb +8 -0
  25. data/example/app/graphql/types/date_time_type.rb +6 -0
  26. data/example/app/graphql/types/post_type.rb +13 -0
  27. data/example/app/graphql/types/query_type.rb +7 -0
  28. data/example/app/models/application_record.rb +3 -0
  29. data/example/app/models/category.rb +5 -0
  30. data/example/app/models/post.rb +14 -0
  31. data/example/bin/bundle +3 -0
  32. data/example/bin/rails +4 -0
  33. data/example/bin/rake +4 -0
  34. data/example/bin/setup +34 -0
  35. data/example/bin/update +29 -0
  36. data/example/config.ru +5 -0
  37. data/example/config/application.rb +36 -0
  38. data/example/config/boot.rb +3 -0
  39. data/example/config/cable.yml +10 -0
  40. data/example/config/database.yml +25 -0
  41. data/example/config/environment.rb +5 -0
  42. data/example/config/environments/development.rb +42 -0
  43. data/example/config/environments/production.rb +78 -0
  44. data/example/config/environments/test.rb +36 -0
  45. data/example/config/initializers/wrap_parameters.rb +14 -0
  46. data/example/config/puma.rb +56 -0
  47. data/example/config/routes.rb +5 -0
  48. data/example/config/secrets.yml +32 -0
  49. data/example/config/spring.rb +6 -0
  50. data/example/db/migrate/20170507175133_create_demo_tables.rb +21 -0
  51. data/example/db/schema.rb +36 -0
  52. data/example/db/seeds.rb +35 -0
  53. data/example/log/.keep +0 -0
  54. data/example/public/robots.txt +1 -0
  55. data/example/vendor/.keep +0 -0
  56. data/lib/search_object/plugin/graphql.rb +84 -0
  57. data/lib/search_object/plugin/graphql/version.rb +7 -0
  58. data/search_object_graphql.gemspec +31 -0
  59. data/spec/search_object/plugin/graphql_spec.rb +288 -0
  60. data/spec/spec_helper.rb +12 -0
  61. data/spec/spec_helper_active_record.rb +29 -0
  62. metadata +219 -0
@@ -0,0 +1,78 @@
1
+ Rails.application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # Code is not reloaded between requests.
5
+ config.cache_classes = true
6
+
7
+ # Eager load code on boot. This eager loads most of Rails and
8
+ # your application in memory, allowing both threaded web servers
9
+ # and those relying on copy on write to perform better.
10
+ # Rake tasks automatically ignore this option for performance.
11
+ config.eager_load = true
12
+
13
+ # Full error reports are disabled and caching is turned on.
14
+ config.consider_all_requests_local = false
15
+ config.action_controller.perform_caching = true
16
+
17
+ # Attempt to read encrypted secrets from `config/secrets.yml.enc`.
18
+ # Requires an encryption key in `ENV["RAILS_MASTER_KEY"]` or
19
+ # `config/secrets.yml.key`.
20
+ config.read_encrypted_secrets = true
21
+
22
+ # Disable serving static files from the `/public` folder by default since
23
+ # Apache or NGINX already handles this.
24
+ config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
25
+
26
+
27
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server.
28
+ # config.action_controller.asset_host = 'http://assets.example.com'
29
+
30
+ # Specifies the header that your server uses for sending files.
31
+ # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
32
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
33
+
34
+ # Mount Action Cable outside main process or domain
35
+ # config.action_cable.mount_path = nil
36
+ # config.action_cable.url = 'wss://example.com/cable'
37
+ # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
38
+
39
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
40
+ # config.force_ssl = true
41
+
42
+ # Use the lowest log level to ensure availability of diagnostic information
43
+ # when problems arise.
44
+ config.log_level = :debug
45
+
46
+ # Prepend all log lines with the following tags.
47
+ config.log_tags = [ :request_id ]
48
+
49
+ # Use a different cache store in production.
50
+ # config.cache_store = :mem_cache_store
51
+
52
+ # Use a real queuing backend for Active Job (and separate queues per environment)
53
+ # config.active_job.queue_adapter = :resque
54
+ # config.active_job.queue_name_prefix = "example_#{Rails.env}"
55
+
56
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
57
+ # the I18n.default_locale when a translation cannot be found).
58
+ config.i18n.fallbacks = true
59
+
60
+ # Send deprecation notices to registered listeners.
61
+ config.active_support.deprecation = :notify
62
+
63
+ # Use default logging formatter so that PID and timestamp are not suppressed.
64
+ config.log_formatter = ::Logger::Formatter.new
65
+
66
+ # Use a different logger for distributed setups.
67
+ # require 'syslog/logger'
68
+ # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
69
+
70
+ if ENV["RAILS_LOG_TO_STDOUT"].present?
71
+ logger = ActiveSupport::Logger.new(STDOUT)
72
+ logger.formatter = config.log_formatter
73
+ config.logger = ActiveSupport::TaggedLogging.new(logger)
74
+ end
75
+
76
+ # Do not dump schema after migrations.
77
+ config.active_record.dump_schema_after_migration = false
78
+ end
@@ -0,0 +1,36 @@
1
+ Rails.application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Do not eager load code on boot. This avoids loading your whole application
11
+ # just for the purpose of running a single test. If you are using a tool that
12
+ # preloads Rails for running tests, you may have to set it to true.
13
+ config.eager_load = false
14
+
15
+ # Configure public file server for tests with Cache-Control for performance.
16
+ config.public_file_server.enabled = true
17
+ config.public_file_server.headers = {
18
+ 'Cache-Control' => "public, max-age=#{1.hour.seconds.to_i}"
19
+ }
20
+
21
+ # Show full error reports and disable caching.
22
+ config.consider_all_requests_local = true
23
+ config.action_controller.perform_caching = false
24
+
25
+ # Raise exceptions instead of rendering exception templates.
26
+ config.action_dispatch.show_exceptions = false
27
+
28
+ # Disable request forgery protection in test environment.
29
+ config.action_controller.allow_forgery_protection = false
30
+
31
+ # Print deprecation notices to the stderr.
32
+ config.active_support.deprecation = :stderr
33
+
34
+ # Raises error for missing translations
35
+ # config.action_view.raise_on_missing_translations = true
36
+ end
@@ -0,0 +1,14 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # This file contains settings for ActionController::ParamsWrapper which
4
+ # is enabled by default.
5
+
6
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters format: [:json]
9
+ end
10
+
11
+ # To enable root element in JSON for ActiveRecord objects.
12
+ # ActiveSupport.on_load(:active_record) do
13
+ # self.include_root_in_json = true
14
+ # end
@@ -0,0 +1,56 @@
1
+ # Puma can serve each request in a thread from an internal thread pool.
2
+ # The `threads` method setting takes two numbers: a minimum and maximum.
3
+ # Any libraries that use thread pools should be configured to match
4
+ # the maximum value specified for Puma. Default is set to 5 threads for minimum
5
+ # and maximum; this matches the default thread size of Active Record.
6
+ #
7
+ threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
8
+ threads threads_count, threads_count
9
+
10
+ # Specifies the `port` that Puma will listen on to receive requests; default is 3000.
11
+ #
12
+ port ENV.fetch("PORT") { 3000 }
13
+
14
+ # Specifies the `environment` that Puma will run in.
15
+ #
16
+ environment ENV.fetch("RAILS_ENV") { "development" }
17
+
18
+ # Specifies the number of `workers` to boot in clustered mode.
19
+ # Workers are forked webserver processes. If using threads and workers together
20
+ # the concurrency of the application would be max `threads` * `workers`.
21
+ # Workers do not work on JRuby or Windows (both of which do not support
22
+ # processes).
23
+ #
24
+ # workers ENV.fetch("WEB_CONCURRENCY") { 2 }
25
+
26
+ # Use the `preload_app!` method when specifying a `workers` number.
27
+ # This directive tells Puma to first boot the application and load code
28
+ # before forking the application. This takes advantage of Copy On Write
29
+ # process behavior so workers use less memory. If you use this option
30
+ # you need to make sure to reconnect any threads in the `on_worker_boot`
31
+ # block.
32
+ #
33
+ # preload_app!
34
+
35
+ # If you are preloading your application and using Active Record, it's
36
+ # recommended that you close any connections to the database before workers
37
+ # are forked to prevent connection leakage.
38
+ #
39
+ # before_fork do
40
+ # ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
41
+ # end
42
+
43
+ # The code in the `on_worker_boot` will be called if you are using
44
+ # clustered mode by specifying a number of `workers`. After each worker
45
+ # process is booted, this block will be run. If you are using the `preload_app!`
46
+ # option, you will want to use this block to reconnect to any threads
47
+ # or connections that may have been created at application boot, as Ruby
48
+ # cannot share connections between processes.
49
+ #
50
+ # on_worker_boot do
51
+ # ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
52
+ # end
53
+ #
54
+
55
+ # Allow puma to be restarted by `rails restart` command.
56
+ plugin :tmp_restart
@@ -0,0 +1,5 @@
1
+ Rails.application.routes.draw do
2
+ mount GraphiQL::Rails::Engine, at: '/', graphql_path: '/graphql'
3
+
4
+ post "/graphql", to: "graphql#execute"
5
+ end
@@ -0,0 +1,32 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key is used for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+
6
+ # Make sure the secret is at least 30 characters and all random,
7
+ # no regular words or you'll be exposed to dictionary attacks.
8
+ # You can use `rails secret` to generate a secure secret key.
9
+
10
+ # Make sure the secrets in this file are kept private
11
+ # if you're sharing your code publicly.
12
+
13
+ # Shared secrets are available across all environments.
14
+
15
+ # shared:
16
+ # api_key: a1B2c3D4e5F6
17
+
18
+ # Environmental secrets are only available for that specific environment.
19
+
20
+ development:
21
+ secret_key_base: b27fb6109fe0ad0945bb6661c4d2aa2792e1aa06c93920142ede0e8ae26f20b2bbfbdd93503c3445ca87aa290c25eb6b44babdcea8d89a70c527f4bbf9b68d98
22
+
23
+ test:
24
+ secret_key_base: 7796b748b14df70b69c0d50e9142be609c99898f53a769c1b4a43915af101183a8b3438cb586664f834e84d89aeedd252eb58c4c6f54b9b87d88fd77fd58da55
25
+
26
+ # Do not keep production secrets in the unencrypted secrets file.
27
+ # Instead, either read values from the environment.
28
+ # Or, use `bin/rails secrets:setup` to configure encrypted secrets
29
+ # and move the `production:` environment over there.
30
+
31
+ production:
32
+ secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
@@ -0,0 +1,6 @@
1
+ %w(
2
+ .ruby-version
3
+ .rbenv-vars
4
+ tmp/restart.txt
5
+ tmp/caching-dev.txt
6
+ ).each { |path| Spring.watch(path) }
@@ -0,0 +1,21 @@
1
+ class CreateDemoTables < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :categories do |t|
4
+ t.string :name, null: false
5
+ t.index :name, unique: true
6
+ t.timestamps
7
+ end
8
+
9
+ create_table :posts do |t|
10
+ t.references :category, foreign_key: true
11
+ t.string :title, null: false
12
+ t.index :title, unique: true
13
+ t.string :body, null: false
14
+ t.integer :views_count, null: false, default: 0
15
+ t.integer :likes_count, null: false, default: 0
16
+ t.integer :comments_count, null: false, default: 0
17
+ t.datetime :published_at
18
+ t.timestamps
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,36 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # Note that this schema.rb definition is the authoritative source for your
6
+ # database schema. If you need to create the application database on another
7
+ # system, you should be using db:schema:load, not running all the migrations
8
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
9
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
10
+ #
11
+ # It's strongly recommended that you check this file into your version control system.
12
+
13
+ ActiveRecord::Schema.define(version: 20170507175133) do
14
+
15
+ create_table "categories", force: :cascade do |t|
16
+ t.string "name", null: false
17
+ t.datetime "created_at", null: false
18
+ t.datetime "updated_at", null: false
19
+ t.index ["name"], name: "index_categories_on_name", unique: true
20
+ end
21
+
22
+ create_table "posts", force: :cascade do |t|
23
+ t.integer "category_id"
24
+ t.string "title", null: false
25
+ t.string "body", null: false
26
+ t.integer "views_count", default: 0, null: false
27
+ t.integer "likes_count", default: 0, null: false
28
+ t.integer "comments_count", default: 0, null: false
29
+ t.datetime "published_at"
30
+ t.datetime "created_at", null: false
31
+ t.datetime "updated_at", null: false
32
+ t.index ["category_id"], name: "index_posts_on_category_id"
33
+ t.index ["title"], name: "index_posts_on_title", unique: true
34
+ end
35
+
36
+ end
@@ -0,0 +1,35 @@
1
+ Category.delete_all
2
+ Post.delete_all
3
+
4
+
5
+ category_names = [
6
+ 'Books',
7
+ 'Code',
8
+ 'Design',
9
+ 'Database',
10
+ 'Education',
11
+ 'Personal',
12
+ 'News',
13
+ 'Stuff',
14
+ 'Others'
15
+ ]
16
+
17
+ categories = category_names.map do |name|
18
+ Category.create! name: name
19
+ end
20
+
21
+ 400.times do |i|
22
+ Post.create!(
23
+ category: categories.sample,
24
+ title: "Example post #{i + 1}",
25
+ body: 'Body text',
26
+ views_count: rand(1000),
27
+ likes_count: rand(1000),
28
+ comments_count: rand(1000),
29
+ published_at: [rand(30).days.ago, rand(30).days.from_now].sample,
30
+ created_at: rand(30).days.ago
31
+ )
32
+ print '.'
33
+ end
34
+
35
+ puts ''
File without changes
@@ -0,0 +1 @@
1
+ # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
File without changes
@@ -0,0 +1,84 @@
1
+ module SearchObject
2
+ module Plugin
3
+ module Graphql
4
+ def self.included(base)
5
+ base.include SearchObject::Plugin::Enum
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ attr_reader :object, :context
10
+
11
+ def initialize(filters: {}, object: nil, context: {}, scope: nil)
12
+ @object = object
13
+ @context = context
14
+
15
+ super filters: filters, scope: scope
16
+ end
17
+
18
+ module ClassMethods
19
+ def option(name, options = {}, &block)
20
+ argument = Helper.build_argument(name, options)
21
+ arguments[argument.name] = argument
22
+
23
+ options[:enum] = argument.type.values.keys if argument.type.is_a? GraphQL::EnumType
24
+
25
+ super(name, options, &block)
26
+ end
27
+
28
+ def types
29
+ GraphQL::Define::TypeDefiner.instance
30
+ end
31
+
32
+ # NOTE(rstankov): GraphQL::Function interface
33
+ # Documentation - https://rmosolgo.github.io/graphql-ruby/schema/code_reuse#functions
34
+ def call(object, args, context)
35
+ new(filters: args.to_h, object: object, context: context).results
36
+ end
37
+
38
+ def arguments
39
+ config[:args] ||= {}
40
+ end
41
+
42
+ def type(value = :default, &block)
43
+ return config[:type] if value == :default && !block_given?
44
+ config[:type] = block_given? ? GraphQL::ObjectType.define(&block) : value
45
+ end
46
+
47
+ def complexity(value = :default)
48
+ return config[:complexity] || 1 if value == :default
49
+ config[:complexity] = value
50
+ end
51
+
52
+ def description(value = :default)
53
+ return config[:description] if value == :default
54
+ config[:description] = value
55
+ end
56
+
57
+ def deprecation_reason(value = :default)
58
+ return config[:deprecation_reason] if value == :default
59
+ config[:deprecation_reason] = value
60
+ end
61
+ end
62
+
63
+ class MissingTypeDefinitionError < ArgumentError
64
+ def initialize(name)
65
+ super "GraphQL type has to passed as :type to '#{name}' option"
66
+ end
67
+ end
68
+
69
+ # :api: private
70
+ module Helper
71
+ module_function
72
+
73
+ def build_argument(name, options)
74
+ argument = GraphQL::Argument.new
75
+ argument.name = name.to_s
76
+ argument.type = options.fetch(:type) { raise MissingTypeDefinitionError, name }
77
+ argument.default_value = options[:default] if options.key? :default
78
+ argument.description = options[:description] if options.key? :description
79
+ argument
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,7 @@
1
+ module SearchObject
2
+ module Plugin
3
+ module Graphql
4
+ VERSION = '0.1'.freeze
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'English'
5
+ require 'search_object/plugin/graphql/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'search_object_graphql'
9
+ spec.version = SearchObject::Plugin::Graphql::VERSION
10
+ spec.authors = ['Radoslav Stankov']
11
+ spec.email = ['rstankov@gmail.com']
12
+ spec.description = 'Search Object plugin to working with GraphQL'
13
+ spec.summary = 'Maps search objects to GraphQL resolvers'
14
+ spec.homepage = 'https://github.com/RStankov/SearchObjectGraphQL'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split($RS)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'search_object', '~> 1.2'
23
+ spec.add_dependency 'graphql', '~> 1.5'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.13'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rspec', '~> 3.5'
28
+ spec.add_development_dependency 'rubocop', '0.46.0'
29
+ spec.add_development_dependency 'rubocop-rspec', '1.8.0'
30
+ spec.add_development_dependency 'coveralls'
31
+ end