Giter VIP home page Giter VIP logo

graphql_active's Introduction

GraphqlActive

Installation

Add this line to your application's Gemfile:

gem 'graphql_active'

And then execute:

$ bundle

Or install it yourself as:

$ gem install graphql_active

Usage

Setup

  • Install the gem (see above).
  • Next, add a controller for your GraphQL endpoint: rails g controller GraphqlActive query mutate
  • View the controller that was just generated and add a line of code to each method. When your done the controller should look like this:
class GraphqlActiveController < ApplicationController
  def query
    GraphqlActive.query(params['query']) # can be changed to param key of your choosing
  end

  def mutate
    GraphqlActive.mutate(params['query']) # can be changed to param key of your choosing
  end
end

Note: both the read method (query) and the write method (mutate) pull the query string from params['query']. This is to keep consistancy on the user end of the request. The methods for query and mutate are seperate so that you can omit them from the query string on the client side.

  • Add routes to point to this controller:
get '/graphql' => 'graphql_active#query'
post '/graphql' => 'graphql_active#mutate'
  • And that's it ๐Ÿ‘ Now you will have access to your data via a Graphql style interface

Using

  • Any model that you want to expose a Graphql CRUD interface on, include the GraphqlActive hook in your ActiveRecord model like so:
class User
  make_graphql_active
end
Finding a single record
# Setup data
User.create(first_name: "Bob", last_name: "Example")

# Query
GraphqlActive.query('user(id: 1) { id, first_name, last_name }')

# Return data
{
  "data" => {
    "user" => {
      "id" => "1",
      "first_name" => "Bob",
      "last_name" => "Example"
    }
  }
}
Finding a collection
# Setup data
User.create(first_name: "Foo", last_name: "Bar")
User.create(first_name: "Bob", last_name: "Biz")

# Query
GraphqlActive.query('users() { first_name, last_name }')

# Return data
{
  "data" => {
    "users" => [
      {
        "first_name" => "Foo",
        "last_name" => "Bar"
      },
      {
        "first_name" => "Bob",
        "last_name" => "Biz"
      }
    ]
  }
}
Finding a set of nested data
# Setup data
user = User.create(first_name: "Foo", last_name: "Bar")

first_post = Post.create(title: "My Post", body: "Some stuff about awesome other stuff")

second_post = Post.create(title: "My Other Awesome Post", body: "More awesome inspiring stuff")

user.posts << [first_post, second_post]

first_post.comments << [
  Comment.create(body: "Great post"),
  Comment.create(body: "Thanks!")
]

# Query
GraphqlActive.query('user(id: 1) { first_name, posts { title, comments { body } } }')

# Return data
{
  "data" => {
    "user" => {
      "first_name" => "Foo",
      "posts" [
        {
          "title" => "My Post",
          "comments" => [
            {
              "body" => "Great post"
            },
            {
              "body" => "Thanks!"
            }
          ]
        },
        {
          "title" => "My Other Awesome Post",
          "comments" => []
        }
      ]
    }
  }
}

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/graphql_active.

License

The gem is available as open source under the terms of the MIT License.

graphql_active's People

Contributors

julianrogmans avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.