Skip to content

Commit

Permalink
Merge pull request ajsharp#1 from jdpaterson/feature/search-object-ge…
Browse files Browse the repository at this point in the history
…nerator

add generator for search-objects
  • Loading branch information
ajsharp authored Jan 7, 2020
2 parents e552c65 + 251e8da commit 8e605a9
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 5 deletions.
75 changes: 70 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

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.

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

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

## Installation

Expand Down Expand Up @@ -52,6 +53,7 @@ rails generate gql:input Post
```

Result:

```ruby
# app/graphql/types/post_input.rb
module Types
Expand All @@ -77,6 +79,7 @@ rails generate gql:mutation Update Post
```

Result:

```ruby
# app/graphql/mutations/update_post.rb
module Mutations
Expand Down Expand Up @@ -105,4 +108,66 @@ module Mutations
end
end
end
```
```

### gql:search_object MODEL_NAME

Generate a search object from a model using [SearchObjectGraphQL](https://github.com/RStankov/SearchObjectGraphQL)

If you have not yet created a base search resolver:

`rails g gql:model_search_base`

\*_Adds `gem 'search_object_graphql'` to gemfile_

result:

```ruby
# app/graphql/resolvers/base_search_resolver.rb
module Resolvers
class BaseSearchResolver < GraphQL::Schema::Resolver
require 'search_object'
require 'search_object/plugin/graphql'
include SearchObject.module(:graphql)
end
end
```

Then generate a search object for your model:

`rails g gql:model_search Post`

result:

```ruby
# app/graphql/resolvers/post_search.rb
module Resolvers
class PostSearch < Resolvers::BaseSearchResolver
type [Types::PostType], null: false
description "Lists posts"

scope { Post.all }

option(:id, type: Int) { |scope, value| scope.where id: value }
option(:title, type: String) { |scope, value| scope.where title: value }
option(:body, type: Int) { |scope, value| scope.where rating: value }
option(:created_at, type: GraphQL::Types::ISO8601DateTime) { |scope, value| scope.where created_at: value }
option(:updated_at, type: GraphQL::Types::ISO8601DateTime) { |scope, value| scope.where updated_at: value }

def resolve
[]
end

end
end
```

This will also insert a search field into the beginning of query_type.rb

```ruby
#app/graphql/types/query_type.rb
module Types
class QueryType < Types::BaseObject
field :posts, resolver: Resolvers::PostSearch
...
```
9 changes: 9 additions & 0 deletions lib/generators/gql/model_search_base_generator.rb
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
19 changes: 19 additions & 0 deletions lib/generators/gql/model_search_generator.rb
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
16 changes: 16 additions & 0 deletions lib/generators/gql/templates/model_search.rb
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
7 changes: 7 additions & 0 deletions lib/generators/gql/templates/model_search_base.rb
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

0 comments on commit 8e605a9

Please sign in to comment.