在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):samesystem/graphql_rails开源软件地址(OpenSource Url):https://github.com/samesystem/graphql_rails开源编程语言(OpenSource Language):Ruby 98.9%开源软件介绍(OpenSource Introduction):GraphqlRailsRails style structure for GraphQL API. InstallationAdd this line to your application's Gemfile: gem 'graphql_rails' And then execute:
Or install it yourself as:
Getting startedExecute:
This will generate code which will let you start your graphql faster UsageDefine GraphQL schema as RoR routes# config/graphql/routes.rb
GraphqlRails::Router.draw do
# will create createUser, updateUser, destroyUser mutations and user, users queries.
# expects that UsersController class exist
resources :users
# if you want custom queries or mutation
query 'searchLogs', to: 'logs#search' # redirects request to LogsController
mutation 'changeUserPassword', to: 'users#change_password'
end Define your Graphql model# app/models/user.rb
class User # works with any class including ActiveRecord
include GraphqlRails::Model
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, type: :string
c.attribute :surname, type: :string
end
end Define controller# app/controllers/graphql/users_controller.rb
class Graphql::UsersController < GraphqlApplicationController
# graphql requires to describe which attributes controller action accepts and which returns
action(:change_user_password)
.permit(:password!, :id!) # Bang (!) indicates that attribute is required
.returns('User!')
def change_user_password
user = User.find(params[:id])
user.update!(password: params[:password])
# 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
.returns('[User!]!')
def search
end
end RoutesGraphqlRails::Router.draw do
# generates `friend`, `createFriend`, `updateFriend`, `destroyFriend`, `friends` routes
resources :friends
resources :shops, only: [:show, :index] # generates `shop` and `shops` routes only
resources :orders, except: :update # generates all routes except `updateOrder`
resources :users do
# generates `findUser` query
query :find, on: :member
# generates `searchUsers` query
query :search, on: :collection
end
# you can use namespaced controllers too:
scope module: 'admin' do
# `updateTranslations` route will be handled by `Admin::TranslationsController`
mutation :updateTranslations, to: 'translations#update'
# all :groups routes will be handled by `Admin::GroupsController`
resources :groups
end
end Testing your GraphqlRails::Controller in RSpecSetupAdd those lines in your # 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 methodsThere are 3 helper methods:
Test examplesclass MyGraphqlController
action(:create_user).permit(:full_name, :email).returns(User)
action(:index).returns('String')
def index
"Called from index: #{params[:message]}"
end
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 Integrating GraphqlRails with other toolsIn 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.rb
GraphqlRails::Integrations.enable(:lograge, :sentry) At the moment, GraphqlRails supports following integrations:
If you need to build something custom, check logging_and_monitoring documentation for more details. Detailed documentationCheck https://samesystem.github.io/graphql_rails for more details DevelopmentAfter checking out the repo, run To install this gem onto your local machine, run ContributingBug reports and pull requests are welcome on GitHub at https://github.com/samesystem/graphql_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct. LicenseThe gem is available as open source under the terms of the MIT License. Code of ConductEveryone interacting in the GraphqlRails project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论