Skip to content

Commit 8e605a9

Browse files
authored
Merge pull request #1 from jdpaterson/feature/search-object-generator
add generator for search-objects
2 parents e552c65 + 251e8da commit 8e605a9

File tree

5 files changed

+121
-5
lines changed

5 files changed

+121
-5
lines changed

README.md

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
A few generators to make it easy to integrate your Rails models with [graphql-ruby](https://github.com/rmosolgo/graphql-ruby). I created this because I was wasting too many keystrokes copying my model schema by hand to create graphql types.
44

5-
This project contains three generators that look at your ActiveRecord model schema and generates graphql types for you.
5+
This project contains generators that look at your ActiveRecord model schema and generates graphql types for you.
66

7-
* `gql:model_type Post` - Generate a graphql type for a model
8-
* `gql:input Post` - Generate a graphql input type for a model
9-
* `gql:mutation Update Post` - Generate a graphql mutation class for a model
7+
- `gql:model_type Post` - Generate a graphql type for a model
8+
- `gql:input Post` - Generate a graphql input type for a model
9+
- `gql:mutation Update Post` - Generate a graphql mutation class for a model
10+
- `gql:search_object` - A search object based on [SearchObjectGraphQL](https://github.com/RStankov/SearchObjectGraphQL)
1011

1112
## Installation
1213

@@ -52,6 +53,7 @@ rails generate gql:input Post
5253
```
5354

5455
Result:
56+
5557
```ruby
5658
# app/graphql/types/post_input.rb
5759
module Types
@@ -77,6 +79,7 @@ rails generate gql:mutation Update Post
7779
```
7880

7981
Result:
82+
8083
```ruby
8184
# app/graphql/mutations/update_post.rb
8285
module Mutations
@@ -105,4 +108,66 @@ module Mutations
105108
end
106109
end
107110
end
108-
```
111+
```
112+
113+
### gql:search_object MODEL_NAME
114+
115+
Generate a search object from a model using [SearchObjectGraphQL](https://github.com/RStankov/SearchObjectGraphQL)
116+
117+
If you have not yet created a base search resolver:
118+
119+
`rails g gql:model_search_base`
120+
121+
\*_Adds `gem 'search_object_graphql'` to gemfile_
122+
123+
result:
124+
125+
```ruby
126+
# app/graphql/resolvers/base_search_resolver.rb
127+
module Resolvers
128+
class BaseSearchResolver < GraphQL::Schema::Resolver
129+
require 'search_object'
130+
require 'search_object/plugin/graphql'
131+
include SearchObject.module(:graphql)
132+
end
133+
end
134+
```
135+
136+
Then generate a search object for your model:
137+
138+
`rails g gql:model_search Post`
139+
140+
result:
141+
142+
```ruby
143+
# app/graphql/resolvers/post_search.rb
144+
module Resolvers
145+
class PostSearch < Resolvers::BaseSearchResolver
146+
type [Types::PostType], null: false
147+
description "Lists posts"
148+
149+
scope { Post.all }
150+
151+
option(:id, type: Int) { |scope, value| scope.where id: value }
152+
option(:title, type: String) { |scope, value| scope.where title: value }
153+
option(:body, type: Int) { |scope, value| scope.where rating: value }
154+
option(:created_at, type: GraphQL::Types::ISO8601DateTime) { |scope, value| scope.where created_at: value }
155+
option(:updated_at, type: GraphQL::Types::ISO8601DateTime) { |scope, value| scope.where updated_at: value }
156+
157+
def resolve
158+
[]
159+
end
160+
161+
end
162+
end
163+
```
164+
165+
This will also insert a search field into the beginning of query_type.rb
166+
167+
```ruby
168+
#app/graphql/types/query_type.rb
169+
module Types
170+
class QueryType < Types::BaseObject
171+
field :posts, resolver: Resolvers::PostSearch
172+
...
173+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Gql
2+
class ModelSearchBaseGenerator < Rails::Generators::Base
3+
source_root File.expand_path('../templates', __FILE__)
4+
def generate_model_search_base
5+
gem 'search_object_graphql'
6+
template('model_search_base.rb', "app/graphql/resolvers/base_search_resolver.rb")
7+
end
8+
end
9+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require_relative 'gql_generator_base'
2+
module Gql
3+
class ModelSearchGenerator < Rails::Generators::Base
4+
include GqlGeneratorBase
5+
source_root File.expand_path('../templates', __FILE__)
6+
argument :model_name, type: :string
7+
8+
def search
9+
inject_into_file(
10+
"app/graphql/types/query_type.rb",
11+
"\t\tfield :#{model_name.downcase.pluralize}, resolver: Resolvers::#{model_name}Search \n",
12+
:after => "class QueryType < Types::BaseObject\n"
13+
)
14+
file_name = "#{model_name.underscore}_search"
15+
@fields = map_model_types(model_name)
16+
template('model_search.rb', "app/graphql/resolvers/#{file_name}.rb")
17+
end
18+
end
19+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Resolvers
2+
class <%= @resolver_prefix %><%= @model_name %>Search < Resolvers::BaseSearchResolver
3+
type [Types::<%= @model_name %>Type], null: false
4+
description "Lists <%= @model_name.downcase.pluralize %>"
5+
6+
scope { <%= @model_name %>.all }
7+
8+
<% @fields.each do |field| -%>
9+
option(:<%= field[:name] %>, type: <%= field[:gql_type] %>) { |scope, value| scope.where <%= field[:name] %>: value }
10+
<% end %>
11+
def resolve
12+
[]
13+
end
14+
15+
end
16+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Resolvers
2+
class BaseSearchResolver < GraphQL::Schema::Resolver
3+
require 'search_object'
4+
require 'search_object/plugin/graphql'
5+
include SearchObject.module(:graphql)
6+
end
7+
end

0 commit comments

Comments
 (0)