forked from ajsharp/graphql-rails-generators
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ajsharp#1 from jdpaterson/feature/search-object-ge…
…nerator add generator for search-objects
- Loading branch information
Showing
5 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Gql | ||
class ModelSearchBaseGenerator < Rails::Generators::Base | ||
source_root File.expand_path('../templates', __FILE__) | ||
def generate_model_search_base | ||
gem 'search_object_graphql' | ||
template('model_search_base.rb', "app/graphql/resolvers/base_search_resolver.rb") | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require_relative 'gql_generator_base' | ||
module Gql | ||
class ModelSearchGenerator < Rails::Generators::Base | ||
include GqlGeneratorBase | ||
source_root File.expand_path('../templates', __FILE__) | ||
argument :model_name, type: :string | ||
|
||
def search | ||
inject_into_file( | ||
"app/graphql/types/query_type.rb", | ||
"\t\tfield :#{model_name.downcase.pluralize}, resolver: Resolvers::#{model_name}Search \n", | ||
:after => "class QueryType < Types::BaseObject\n" | ||
) | ||
file_name = "#{model_name.underscore}_search" | ||
@fields = map_model_types(model_name) | ||
template('model_search.rb', "app/graphql/resolvers/#{file_name}.rb") | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Resolvers | ||
class <%= @resolver_prefix %><%= @model_name %>Search < Resolvers::BaseSearchResolver | ||
type [Types::<%= @model_name %>Type], null: false | ||
description "Lists <%= @model_name.downcase.pluralize %>" | ||
|
||
scope { <%= @model_name %>.all } | ||
<% @fields.each do |field| -%> | ||
option(:<%= field[:name] %>, type: <%= field[:gql_type] %>) { |scope, value| scope.where <%= field[:name] %>: value } | ||
<% end %> | ||
def resolve | ||
[] | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Resolvers | ||
class BaseSearchResolver < GraphQL::Schema::Resolver | ||
require 'search_object' | ||
require 'search_object/plugin/graphql' | ||
include SearchObject.module(:graphql) | ||
end | ||
end |