README.md in graphql_rails-0.1.0 vs README.md in graphql_rails-0.2.0

- old
+ new

@@ -1,27 +1,27 @@ # GraphqlRails -[![Build Status](https://travis-ci.org/povilasjurcys/graphiti.svg?branch=master)](https://travis-ci.org/povilasjurcys/graphiti) -[![codecov](https://codecov.io/gh/povilasjurcys/graphiti/branch/master/graph/badge.svg)](https://codecov.io/gh/povilasjurcys/graphiti) +[![Build Status](https://travis-ci.org/povilasjurcys/graphql_rails.svg?branch=master)](https://travis-ci.org/povilasjurcys/graphql_rails) +[![codecov](https://codecov.io/gh/povilasjurcys/graphql_rails/branch/master/graph/badge.svg)](https://codecov.io/gh/povilasjurcys/graphql_rails) Rails style structure for GrapQL API. ## Installation Add this line to your application's Gemfile: ```ruby -gem 'graphiti' +gem 'graphql_rails' ``` And then execute: $ bundle Or install it yourself as: - $ gem install graphiti + $ gem install graphql_rails ## Usage ### Define GraphQL schema as RoR routes @@ -41,11 +41,11 @@ ```ruby class User # works with any class including ActiveRecord include GraphqlRails::Model - graphiti do |c| + graphql do |c| # most common attributes, like :id, :name, :title has default type, so you don't have to specify it (but you can!) c.attribute :id c.attribute :email, :string c.attribute :surname, :string @@ -63,11 +63,11 @@ def change_user_password user = User.find(params[:id]) user.update!(password: params[:password]) - # returned value needs to have all methods defined in model `graphiti do` part + # returned value needs to have all methods defined in model `graphql do` part user # or SomeDecorator.new(user) end action(:search).permit(search_fields!: SearchFieldsInput) # you can specify your own input fields def search @@ -101,22 +101,90 @@ resources :groups end end ``` +## Testing your GrapqhlRails::Controller in RSpec + +### Setup + +Add those lines in your `spec/spec_helper.rb` file + +```ruby +# spec/spec_helper.rb +require 'graphql_rails/rspec_controller_helpers' + +RSpec.configure do |config| + config.include(GraphqlRails::RSpecControllerHelpers, type: :graphql_controller) + # ... your other configuration ... +end +``` + +### Helper methods + +There are 3 helper methods: + +* `mutation(:your_controller_action_name, params: {}, context: {})`. `params` and `context` are optional +* `query(:your_controller_action_name, params: {}, context: {})`. `params` and `context` are optional +* `response`. Response is set only after you call `mutation` or `query` + +### Test examples + +```ruby +class MyGraphqlController + def index + "Called from index: #{params[:message]}" + end + + action(:create_user).permit(:full_name, :email) + def create_user + User.create!(params) + end +end + +RSpec.describe MyGraphqlController, type: :graphql_controller do + describe '#index' do + it 'is successful' do + query(:index) + expect(response).to be_successful + end + + it 'returns correct message' do + query(:index, params: { message: 'Hello world!' }) + expect(response.result).to eq "Called from index: Hello world!" + end + end + + describe '#create_user' do + context 'when bad email is given' do + it 'fails' do + mutation(:create_user, params { email: 'bad' }) + expect(response).to be_failure + end + + it 'contains errors' do + mutation(:create_user, params { email: 'bad' }) + expect(response.errors).not_to be_empty + end + end + end +end +``` + + ## 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](https://rubygems.org). ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/graphiti. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. +Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/graphql_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). ## Code of Conduct -Everyone interacting in the GraphqlRails project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/graphiti/blob/master/CODE_OF_CONDUCT.md). +Everyone interacting in the GraphqlRails project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/graphql_rails/blob/master/CODE_OF_CONDUCT.md).