{"id":13879678,"url":"https://github.com/ajsharp/graphql-rails-generators","last_synced_at":"2025-11-11T18:39:18.934Z","repository":{"id":47695856,"uuid":"224326846","full_name":"ajsharp/graphql-rails-generators","owner":"ajsharp","description":"Graphql Rails Scaffold™. Automatically generate GraphQL types from your rails models.","archived":false,"fork":false,"pushed_at":"2021-08-17T17:03:04.000Z","size":26,"stargazers_count":75,"open_issues_count":3,"forks_count":15,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-02T14:13:05.123Z","etag":null,"topics":["graphql","rails","ruby-on-rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ajsharp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-27T02:25:47.000Z","updated_at":"2025-08-20T17:08:05.000Z","dependencies_parsed_at":"2022-08-20T22:30:46.237Z","dependency_job_id":null,"html_url":"https://github.com/ajsharp/graphql-rails-generators","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/ajsharp/graphql-rails-generators","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajsharp%2Fgraphql-rails-generators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajsharp%2Fgraphql-rails-generators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajsharp%2Fgraphql-rails-generators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajsharp%2Fgraphql-rails-generators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ajsharp","download_url":"https://codeload.github.com/ajsharp/graphql-rails-generators/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajsharp%2Fgraphql-rails-generators/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":283691392,"owners_count":26878134,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-11-10T02:00:06.292Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","rails","ruby-on-rails"],"created_at":"2024-08-06T08:02:28.658Z","updated_at":"2025-11-11T18:39:18.900Z","avatar_url":"https://github.com/ajsharp.png","language":"Ruby","readme":"# graphql-rails-generators\n\nA 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.\n\nThis project contains generators that look at your ActiveRecord model schema and generates graphql types for you.\n\n- `gql:model_type Post` - Generate a graphql type for a model\n- `gql:input Post` - Generate a graphql input type for a model\n- `gql:mutation Update Post` - Generate a graphql mutation class for a model\n- `gql:search_object` - A search object based on [SearchObjectGraphQL](https://github.com/RStankov/SearchObjectGraphQL)\n\n## Installation\n\n```\ngem 'graphql-rails-generators', group: :development\n```\n\n## Requirements\n\nThis library only supports ActiveRecord, though it would be fairly trivial to add support for other ORMs.\n\n## Usage\n\n### gql:model_type\n\nGenerate a model type from a model.\n\n```\n$ rails generate gql:model_type MODEL_CLASS\n```\n\n#### Options\n\n- `--name` - customize the file/class name, useful if you don't want the default Type suffix.\n\n#### Example\n\n```ruby\n# app/graphql/post_type.rb\nmodule Types\n  class PostType \u003c Types::BaseObject\n    field :id, GraphQL::Types::ID, null: true\n    field :title, String, null: true\n    field :body, String, null: true\n    field :created_at, GraphQL::Types::ISO8601DateTime, null: true\n    field :updated_at, GraphQL::Types::ISO8601DateTime, null: true\n  end\nend\n```\n\n### gql:input MODEL_CLASS\n\nGenerate an input type from a model.\n\n```\nrails generate gql:input Post\n```\n\n#### Options\n\n- `--name` - customize the file/class name, useful if you don't want the default Input suffix.\n\n#### Example\n\n```ruby\n# app/graphql/types/post_input.rb\nmodule Types\n  module Input\n    class PostInput \u003c Types::BaseInputObject\n      argument :title, String, required: false\n      argument :body, String, required: false\n    end\n  end\nend\n```\n\n### gql:mutations MODEL_CLASS\n\nGenerate create, update and delete mutations for a model.\n\n```\nrails generate gql:mutations Post\n```\n\n#### Example\n\n```ruby\n# app/graphql/types/post_input.rb\nmodule Types\n  module Input\n    class PostInput \u003c Types::BaseInputObject\n      argument :title, String, required: false\n      argument :body, String, required: false\n    end\n  end\nend\n```\n\n### gql:mutation MUTATION_PREFIX MODEL_NAME\n\nGenerate a mutation class from a model.\n\nA quick note about the mutation generator...\n\nThe 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.\n\n```\nrails generate gql:mutation Update Post\n```\n\n#### Example\n\n```ruby\n# app/graphql/mutations/update_post.rb\nmodule Mutations\n  class UpdatePost \u003c Mutations::BaseMutation\n    field :post, Types::PostType, null: true\n\n    argument :attributes, Types::Input::PostInput, required: true\n    argument :id, GraphQL::Types::ID, required: false\n\n    def resolve(attributes:, id: nil)\n      model = find_or_build_model(id)\n      model.attributes = attributes.to_h\n      if model.save\n        {post: model}\n      else\n        {errors: model.errors.full_messages}\n      end\n    end\n\n    def find_or_build_model(id)\n      if id\n        Post.find(id)\n      else\n        Post.new\n      end\n    end\n  end\nend\n```\n\n### gql:search_object MODEL_NAME\n\nGenerate a search object from a model using [SearchObjectGraphQL](https://github.com/RStankov/SearchObjectGraphQL)\n\nIf you have not yet created a base search resolver:\n\n`rails g gql:model_search_base`\n\n\\*_Adds `gem 'search_object_graphql'` to gemfile_\n\n#### Example\n\n```ruby\n# app/graphql/resolvers/base_search_resolver.rb\nmodule Resolvers\n  class BaseSearchResolver \u003c GraphQL::Schema::Resolver\n    require 'search_object'\n    require 'search_object/plugin/graphql'\n    include SearchObject.module(:graphql)\n  end\nend\n```\n\nThen generate a search object for your model:\n\n`rails g gql:model_search Post`\n\n#### Example\n\n```ruby\n# app/graphql/resolvers/post_search.rb\nmodule Resolvers\n  class PostSearch \u003c Resolvers::BaseSearchResolver\n    type [Types::PostType], null: false\n    description \"Lists posts\"\n\n    scope { Post.all }\n\n    option(:id, type: Int)   { |scope, value| scope.where id: value }\n    option(:title, type: String)   { |scope, value| scope.where title: value }\n    option(:body, type: Int)   { |scope, value| scope.where rating: value }\n    option(:created_at, type: GraphQL::Types::ISO8601DateTime)   { |scope, value| scope.where created_at: value }\n    option(:updated_at, type: GraphQL::Types::ISO8601DateTime)   { |scope, value| scope.where updated_at: value }\n\n    def resolve\n      []\n    end\n\n  end\nend\n```\n\nThis will also insert a search field into the beginning of query_type.rb\n\n```ruby\n  #app/graphql/types/query_type.rb\n  module Types\n  class QueryType \u003c Types::BaseObject\n    field :posts, resolver: Resolvers::PostSearch\n    ...\n```\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajsharp%2Fgraphql-rails-generators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fajsharp%2Fgraphql-rails-generators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajsharp%2Fgraphql-rails-generators/lists"}