Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

GraphQL on Rails. Write GraphQL server side in rails way

License

NotificationsYou must be signed in to change notification settings

samesystem/graphql_rails

Repository files navigation

Build StatuscodecovDocumentation

Rails style structure for GraphQL API.

Installation

Add this line to your application's Gemfile:

gem'graphql_rails'

And then execute:

$ bundle

Or install it yourself as:

$ gem install graphql_rails

Getting started

Execute:

$ bundle exec rails g graphql_rails:install

This will generate code which will let you start your graphql faster

Usage

Define GraphQL schema as RoR routes

# 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.

Define your Graphql model

# 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.

Define controller

# 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.

Testing your GraphqlRails::Controller in RSpec

Setup

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.

Helper methods

There are 3 helper methods:

  • mutation(:your_controller_action_name, params: {}, context: {}).params andcontext are optional
  • query(:your_controller_action_name, params: {}, context: {}).params andcontext are optional
  • response. Response is set only after you callmutation orquery

Test examples

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

Integrating GraphqlRails with other tools

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.

Detailed documentation

Checkhttps://samesystem.github.io/graphql_rails for more details

Development

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.

Contributing

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.

License

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

Code of Conduct

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

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp