- Notifications
You must be signed in to change notification settings - Fork11
GraphQL on Rails. Write GraphQL server side in rails way
License
samesystem/graphql_rails
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Rails style structure for GraphQL API.
Add this line to your application's Gemfile:
gem'graphql_rails'
And then execute:
$ bundle
Or install it yourself as:
$ gem install graphql_rails
Execute:
$ bundle exec rails g graphql_rails:install
This will generate code which will let you start your graphql faster
# config/graphql/routes.rbGraphqlRails::Router.drawdo# will create createUser, updateUser, destroyUser mutations and user, users queries.# expects that UsersController class existresources:users# if you want custom queries or mutationquery'searchLogs',to:'logs#search'# action is handled by LogsController#searchend
SeeRoutes docs for more info.
# app/models/user.rbclassUser# works with any class including ActiveRecordincludeGraphqlRails::Modelgraphqldo |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).type('String')c.attribute(:surname).type('String')endend
SeeModel docs for more info.
# app/controllers/graphql/users_controller.rbclassGraphql::UsersController <GraphqlApplicationControllermodel('User')# specify that all actions returns User by default# DRUD actions descriptionaction(:index).permit(id:'ID!').returns_manyaction(:show).permit(id:'ID!').returns_singleaction(:create).permit(email:'String!').returns_singleaction(:update).permit(id:'ID!',email:'String!').returns_singleaction(:destroy).permit(id:'ID!').returns_singledefindexUser.allenddefshowUser.find(params[:id])end# ... code for create / update / destroy is skipped ...end
SeeController docs for more info.
Add those lines in yourspec/spec_helper.rb
file
# spec/spec_helper.rbrequire'graphql_rails/rspec_controller_helpers'RSpec.configuredo |config|config.include(GraphqlRails::RSpecControllerHelpers,type::graphql_controller)# ... your other configuration ...end
SeeTesting docs for more info.
There are 3 helper methods:
mutation(:your_controller_action_name, params: {}, context: {})
.params
andcontext
are optionalquery(:your_controller_action_name, params: {}, context: {})
.params
andcontext
are optionalresponse
. Response is set only after you callmutation
orquery
classMyGraphqlControlleraction(:create_user).permit(:full_name,:email).returns(User)action(:index).returns('String')defindex"Called from index:#{params[:message]}"enddefcreate_userUser.create!(params)endendRSpec.describeMyGraphqlController,type::graphql_controllerdodescribe'#index'doit'is successful'doquery(:index)expect(response).tobe_successfulendit'returns correct message'doquery(:index,params:{message:'Hello world!'})expect(response.result).toeq"Called from index: Hello world!"endenddescribe'#create_user'docontext'when bad email is given'doit'fails'domutation(:create_user,params{email:'bad'})expect(response).tobe_failureendit'contains errors'domutation(:create_user,params{email:'bad'})expect(response.errors).not_tobe_emptyendendendend
In order to make GraphqlRails work with tools such as lograge or sentry, you need to enable them. In Ruby on Rails, you can add initializer:
# config/initializers/graphql_rails.rbGraphqlRails::Integrations.enable(:lograge,:sentry)
At the moment, GraphqlRails supports following integrations:
- lograge
- sentry
If you need to build something custom, checklogging_and_monitoring documentation for more details.
Checkhttps://samesystem.github.io/graphql_rails for more details
After checking out the repo, runbin/setup
to install dependencies. Then, runrake spec
to run the tests. You can also runbin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, runbundle exec rake install
. To release a new version, update the version number inversion.rb
, and then runbundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the.gem
file torubygems.org.
Bug reports and pull requests are welcome on GitHub athttps://github.com/samesystem/graphql_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to theContributor Covenant code of conduct.
The gem is available as open source under the terms of theMIT License.
Everyone interacting in the GraphqlRails project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow thecode of conduct.
About
GraphQL on Rails. Write GraphQL server side in rails way