All Projects → ajsharp → Graphql Rails Generators

ajsharp / Graphql Rails Generators

Licence: mit
Graphql Rails Scaffold™. Automatically generate GraphQL types from your rails models.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Graphql Rails Generators

Graphiti
Stylish Graph APIs
Stars: ✭ 783 (+1565.96%)
Mutual labels:  graphql, rails, ruby-on-rails
Graphjin
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.
Stars: ✭ 1,264 (+2589.36%)
Mutual labels:  graphql, rails, ruby-on-rails
Niklick
Rails Versioned API solution template for hipsters! (Ruby, Ruby on Rails, REST API, GraphQL, Docker, RSpec, Devise, Postgress DB)
Stars: ✭ 39 (-17.02%)
Mutual labels:  graphql, rails, ruby-on-rails
Motion
Reactive frontend UI components for Rails in pure Ruby
Stars: ✭ 498 (+959.57%)
Mutual labels:  rails, ruby-on-rails
Doctor
Doctor is a documentation server for your docs in github
Stars: ✭ 391 (+731.91%)
Mutual labels:  rails, ruby-on-rails
Matestack Ui Core
Matestack enables you to create sophisticated, reactive UIs in pure Ruby, without touching JavaScript and HTML. You end up writing 50% less code while increasing productivity, maintainability and developer happiness.
Stars: ✭ 469 (+897.87%)
Mutual labels:  rails, ruby-on-rails
Pupilfirst
A learning management system (LMS) that lets you run an asynchronous online school, where learning is achieved through focused tasks, directed feedback, an iterative workflow, and community interaction.
Stars: ✭ 357 (+659.57%)
Mutual labels:  graphql, ruby-on-rails
Agoo
A High Performance HTTP Server for Ruby
Stars: ✭ 679 (+1344.68%)
Mutual labels:  graphql, rails
Graphql Ruby
Ruby implementation of GraphQL
Stars: ✭ 4,931 (+10391.49%)
Mutual labels:  graphql, rails
Fae
CMS for Rails. For Reals.
Stars: ✭ 701 (+1391.49%)
Mutual labels:  rails, ruby-on-rails
Leaky Gems
A list of Ruby gems that have known memory leaks (and issues)
Stars: ✭ 895 (+1804.26%)
Mutual labels:  rails, ruby-on-rails
Activejob Scheduler
A background job scheduler for any queue backend
Stars: ✭ 24 (-48.94%)
Mutual labels:  rails, ruby-on-rails
Devise masquerade
Extension for devise, enable login as functionality. Add link to the masquerade_path(resource) and use it.
Stars: ✭ 380 (+708.51%)
Mutual labels:  rails, ruby-on-rails
Learn Rails
An example Rails 5.1 app to accompany the "Learn Ruby on Rails" book.
Stars: ✭ 375 (+697.87%)
Mutual labels:  rails, ruby-on-rails
Sail
Sail is a lightweight Rails engine that brings an admin panel for managing configuration settings on a live Rails app
Stars: ✭ 484 (+929.79%)
Mutual labels:  rails, ruby-on-rails
Loaf
Manages and displays breadcrumb trails in Rails app - lean & mean.
Stars: ✭ 360 (+665.96%)
Mutual labels:  rails, ruby-on-rails
Good job
Multithreaded, Postgres-based, ActiveJob backend for Ruby on Rails.
Stars: ✭ 676 (+1338.3%)
Mutual labels:  rails, ruby-on-rails
Lamby
Simple Rails & AWS Lambda Integration 🐑🛤
Stars: ✭ 336 (+614.89%)
Mutual labels:  rails, ruby-on-rails
Rails performance
Monitor performance of you Rails applications
Stars: ✭ 345 (+634.04%)
Mutual labels:  rails, ruby-on-rails
Railsgoat
A vulnerable version of Rails that follows the OWASP Top 10
Stars: ✭ 699 (+1387.23%)
Mutual labels:  rails, ruby-on-rails

graphql-rails-generators

A few generators to make it easy to integrate your Rails models with 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 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:search_object - A search object based on SearchObjectGraphQL

Installation

gem 'graphql-rails-generators', group: :development

Requirements

This library only supports ActiveRecord, though it would be fairly trivial to add support for other ORMs.

Usage

gql:model_type

Generate a model type from a model.

$ rails generate gql:model_type MODEL_CLASS

Options

  • --name - customize the file/class name, useful if you don't want the default Type suffix.

Example

# app/graphql/post_type.rb
module Types
  class PostType < Types::BaseObject
    field :id, GraphQL::Types::ID, null: true
    field :title, String, null: true
    field :body, String, null: true
    field :created_at, GraphQL::Types::ISO8601DateTime, null: true
    field :updated_at, GraphQL::Types::ISO8601DateTime, null: true
  end
end

gql:input MODEL_CLASS

Generate an input type from a model.

rails generate gql:input Post

Options

  • --name - customize the file/class name, useful if you don't want the default Input suffix.

Example

# app/graphql/types/post_input.rb
module Types
  module Input
    class PostInput < Types::BaseInputObject
      argument :title, String, required: false
      argument :body, String, required: false
    end
  end
end

gql:mutations MODEL_CLASS

Generate create, update and delete mutations for a model.

rails generate gql:mutations Post

Example

# app/graphql/types/post_input.rb
module Types
  module Input
    class PostInput < Types::BaseInputObject
      argument :title, String, required: false
      argument :body, String, required: false
    end
  end
end

gql:mutation MUTATION_PREFIX MODEL_NAME

Generate a mutation class from a model.

A quick note about the mutation generator...

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.

rails generate gql:mutation Update Post

Example

# app/graphql/mutations/update_post.rb
module Mutations
  class UpdatePost < Mutations::BaseMutation
    field :post, Types::PostType, null: true

    argument :attributes, Types::Input::PostInput, required: true
    argument :id, GraphQL::Types::ID, required: false

    def resolve(attributes:, id: nil)
      model = find_or_build_model(id)
      model.attributes = attributes.to_h
      if model.save
        {post: model}
      else
        {errors: model.errors.full_messages}
      end
    end

    def find_or_build_model(id)
      if id
        Post.find(id)
      else
        Post.new
      end
    end
  end
end

gql:search_object MODEL_NAME

Generate a search object from a model using SearchObjectGraphQL

If you have not yet created a base search resolver:

rails g gql:model_search_base

*Adds gem 'search_object_graphql' to gemfile

Example

# 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

Example

# 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

  #app/graphql/types/query_type.rb
  module Types
  class QueryType < Types::BaseObject
    field :posts, resolver: Resolvers::PostSearch
    ...
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].