|
| 1 | +# graphql-rails-generators |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +This project contains three generators that look at your ActiveRecord model schema and generates graphql types for you. |
| 6 | + |
| 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 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +``` |
| 14 | +gem 'graphql-rails-generators', group: :development |
| 15 | +``` |
| 16 | + |
| 17 | +## Requirements |
| 18 | + |
| 19 | +This library only supports ActiveRecord, though it would be fairly trivial to add support for other ORMs. |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +### gql:model_type |
| 24 | + |
| 25 | +Generate a model type from a model. |
| 26 | + |
| 27 | +``` |
| 28 | +$ rails generate gql:model_type MODEL_CLASS |
| 29 | +``` |
| 30 | + |
| 31 | +Result: |
| 32 | + |
| 33 | +```ruby |
| 34 | +# app/graphql/post_type.rb |
| 35 | +module Types |
| 36 | + class PostType < Types::BaseObject |
| 37 | + field :id, Int, null: true |
| 38 | + field :title, String, null: true |
| 39 | + field :body, String, null: true |
| 40 | + field :created_at, GraphQL::Types::ISO8601DateTime, null: true |
| 41 | + field :updated_at, GraphQL::Types::ISO8601DateTime, null: true |
| 42 | + end |
| 43 | +end |
| 44 | +``` |
| 45 | + |
| 46 | +### gql:input MODEL_CLASS |
| 47 | + |
| 48 | +Generate an input type from a model. |
| 49 | + |
| 50 | +``` |
| 51 | +rails generate gql:input Post |
| 52 | +``` |
| 53 | + |
| 54 | +Result: |
| 55 | +```ruby |
| 56 | +# app/graphql/types/post_input.rb |
| 57 | +module Types |
| 58 | + module Input |
| 59 | + class PostInput < Types::BaseInputObject |
| 60 | + argument :title, String, required: false |
| 61 | + argument :body, String, required: false |
| 62 | + end |
| 63 | + end |
| 64 | +end |
| 65 | +``` |
| 66 | + |
| 67 | +### gql:mutation MUTATION_PREFIX MODEL_NAME |
| 68 | + |
| 69 | +Generate a mutation class from a model. |
| 70 | + |
| 71 | +A quick note about the mutation generator... |
| 72 | + |
| 73 | +The mutation generator generates something akin to an "upsert" mutation. It takes two arguments: an optional `id` and an optional `attributes`, which is the input type for the model. If you pass an `id`, it will attempt to find the model by the `id` and update it, otherwise it will initialize a new model and attempt to save it. |
| 74 | + |
| 75 | +``` |
| 76 | +rails generate gql:mutation Update Post |
| 77 | +``` |
| 78 | + |
| 79 | +Result: |
| 80 | +```ruby |
| 81 | +# app/graphql/mutations/update_post.rb |
| 82 | +module Mutations |
| 83 | + class UpdatePost < Mutations::BaseMutation |
| 84 | + field :post, Types::PostType, null: true |
| 85 | + |
| 86 | + argument :attributes, Types::Input::PostInput, required: true |
| 87 | + argument :id, Int, required: false |
| 88 | + |
| 89 | + def resolve(attributes:, id: nil) |
| 90 | + model = find_or_build_model(id) |
| 91 | + model.attributes = attributes.to_h |
| 92 | + if model.save |
| 93 | + {post: model} |
| 94 | + else |
| 95 | + {errors: model.errors.full_messages} |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + def find_or_build_model(id) |
| 100 | + if id |
| 101 | + Post.find(id) |
| 102 | + else |
| 103 | + Post.new |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | +end |
| 108 | +``` |
0 commit comments