Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/rubocop #3

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- main

jobs:
rspec:
test:
runs-on: ubuntu-latest

services:
Expand Down Expand Up @@ -49,6 +49,9 @@ jobs:
bundle exec rails db:create
bundle exec rails db:migrate

- name: Run Rubocop
run: rubocop

- name: Run RSpec
env:
RAILS_ENV: test
Expand Down
27 changes: 27 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require: rubocop-rails

AllCops:
Exclude:
# App files
- "app/channels/application_cable/**/*"
- "app/graphql/kiln_backend_schema.rb"
- "app/jobs/application_job.rb"
- "app/mailers/application_mailer.rb"
- "app/models/application_record.rb"
- "bin/**/*"
- "config.ru"
- "config/**/*"
- "db/**/*"
- "Rakefile"
- "spec/rails_helper.rb"
- "spec/spec_helper.rb"
# Added files to review
- "app/controllers/graphql_controller.rb"
- "app/graphql/types/base_*"

Documentation:
Enabled: false

Metrics/BlockLength:
Exclude:
- "spec/**/*"
33 changes: 18 additions & 15 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
source "https://rubygems.org"
# frozen_string_literal: true

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.2.1"
ruby '3.2.1'

gem "bootsnap", require: false
gem "graphql"
gem "pg", "~> 1.1"
gem "puma", "~> 5.0"
gem "rack-cors"
gem "rails", "~> 7.0.8"
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
gem 'bootsnap', require: false
gem 'graphql'
gem 'pg', '~> 1.1'
gem 'puma', '~> 5.0'
gem 'rack-cors'
gem 'rails', '~> 7.0.8'
gem 'rubocop-rails', require: false
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
# gem "jbuilder"
Expand All @@ -27,16 +30,16 @@ gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
# gem "image_processing", "~> 1.2"

group :development, :test do
gem "debug", platforms: %i[ mri mingw x64_mingw ]
gem "factory_bot_rails"
gem "faker"
gem "rspec-rails", "~> 6.0.0"
gem 'debug', platforms: %i[mri mingw x64_mingw]
gem 'factory_bot_rails'
gem 'faker'
gem 'rspec-rails', '~> 6.0.0'
end

group :development do
gem "graphiql-rails"
gem 'graphiql-rails'
end

group :test do
gem "shoulda-matchers", "~> 5.0"
gem 'shoulda-matchers', '~> 5.0'
end
30 changes: 30 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ GEM
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
ast (2.4.2)
bootsnap (1.17.0)
msgpack (~> 1.2)
builder (3.2.4)
Expand Down Expand Up @@ -97,6 +98,8 @@ GEM
irb (1.9.0)
rdoc
reline (>= 0.3.8)
json (2.6.3)
language_server-protocol (3.17.0.3)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand All @@ -122,6 +125,10 @@ GEM
nio4r (2.6.0)
nokogiri (1.15.4-x86_64-darwin)
racc (~> 1.4)
parallel (1.23.0)
parser (3.2.2.4)
ast (~> 2.4.1)
racc
pg (1.5.4)
psych (5.1.1.1)
stringio
Expand Down Expand Up @@ -161,11 +168,14 @@ GEM
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.1.0)
rdoc (6.6.0)
psych (>= 4.0.0)
regexp_parser (2.8.2)
reline (0.4.0)
io-console (~> 0.5)
rexml (3.2.6)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
Expand All @@ -183,6 +193,24 @@ GEM
rspec-mocks (~> 3.12)
rspec-support (~> 3.12)
rspec-support (3.12.1)
rubocop (1.57.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.2.2.4)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.28.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
rubocop-rails (2.22.1)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
ruby-progressbar (1.13.0)
shoulda-matchers (5.3.0)
activesupport (>= 5.2.0)
sprockets (4.2.1)
Expand All @@ -197,6 +225,7 @@ GEM
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand All @@ -217,6 +246,7 @@ DEPENDENCIES
rack-cors
rails (~> 7.0.8)
rspec-rails (~> 6.0.0)
rubocop-rails
shoulda-matchers (~> 5.0)
tzinfo-data

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

class ApplicationController < ActionController::API
end
5 changes: 3 additions & 2 deletions app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ def execute
# Query context goes here, for example:
# current_user: current_user,
}
result = KilnBackendSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
result = KilnBackendSchema.execute(query, variables:, context:, operation_name:)
render json: result
rescue StandardError => e
raise e unless Rails.env.development?

handle_error_in_development(e)
end

Expand Down Expand Up @@ -47,6 +48,6 @@ def handle_error_in_development(e)
logger.error e.message
logger.error e.backtrace.join("\n")

render json: { errors: [{ message: e.message, backtrace: e.backtrace }], data: {} }, status: 500
render json: { errors: [{ message: e.message, backtrace: e.backtrace }], data: {} }, status: :internal_server_error
end
end
2 changes: 1 addition & 1 deletion app/graphql/types/member_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Types
class MemberType < Types::BaseObject
description "A Kiln Collective member"
description 'A Kiln Collective member'

field :id, ID, null: false
field :first_name, String
Expand Down
12 changes: 6 additions & 6 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

module Types
class MutationType < Types::BaseObject
field :create_member, MemberType, null: true, description: "Create a new member" do
field :create_member, MemberType, null: true, description: 'Create a new member' do
argument :first_name, String, required: true
argument :last_name, String, required: true
argument :title, String, required: true
end
def create_member(first_name:, last_name:, title:)
Member.create(
first_name: first_name,
last_name: last_name,
title: title
first_name:,
last_name:,
title:
)
end

field :delete_member, Boolean, null: false, description: "Delete a member permanently" do
field :delete_member, Boolean, null: false, description: 'Delete a member permanently' do
argument :id, ID, required: true
end
def delete_member(id:)
member = Member.find_by(id: id)
member = Member.find_by(id:)
return false unless member

member.destroy
Expand Down
19 changes: 10 additions & 9 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

module Types
class QueryType < Types::BaseObject
field :node, Types::NodeType, null: true, description: "Fetches an object given its ID." do
argument :id, ID, required: true, description: "ID of the object."
field :node, Types::NodeType, null: true, description: 'Fetches an object given its ID.' do
argument :id, ID, required: true, description: 'ID of the object.'
end

def node(id:)
context.schema.object_from_id(id, context)
end

field :nodes, [Types::NodeType, null: true], null: true, description: "Fetches a list of objects given a list of IDs." do
argument :ids, [ID], required: true, description: "IDs of the objects."
field :nodes, [Types::NodeType, { null: true }], null: true,
description: 'Fetches a list of objects given a list of IDs.' do
argument :ids, [ID], required: true, description: 'IDs of the objects.'
end

def nodes(ids:)
Expand All @@ -21,16 +22,16 @@ def nodes(ids:)
# Add root-level fields here.
# They will be entry points for queries on your schema.

field :members, [MemberType], null: false, description: "Fetches all members"
field :members, [MemberType], null: false, description: 'Fetches all members'
def members
Member.all.order("first_name ASC")
Member.all.order('first_name ASC')
end

field :member, MemberType, null: true, description: "Fetches a member by first_name" do
argument :first_name, String, required: true, description: "First name of the member"
field :member, MemberType, null: true, description: 'Fetches a member by first_name' do
argument :first_name, String, required: true, description: 'First name of the member'
end
def member(first_name:)
Member.find_by(first_name: first_name)
Member.find_by(first_name:)
end
end
end
6 changes: 4 additions & 2 deletions app/models/member.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true

class Member < ApplicationRecord
validates_presence_of :first_name
validates_presence_of :last_name
validates :first_name, presence: true
validates :last_name, presence: true
end
2 changes: 2 additions & 0 deletions spec/factories/member.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

FactoryBot.define do
factory :member do
first_name { Faker::Name.first_name }
Expand Down
40 changes: 21 additions & 19 deletions spec/graphql/mutations/members/create_member_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require "rails_helper"
# frozen_string_literal: true

require 'rails_helper'

module Mutations
RSpec.describe Member, type: :request do
describe "Member create mutation" do
let(:mutation) {
describe 'Member create mutation' do
let(:mutation) do
<<~GQL
mutation {
createMember(
Expand All @@ -16,54 +18,54 @@ module Mutations
}
}
GQL
}
end

let(:result) { KilnBackendSchema.execute(mutation).as_json }

it "returns the created Member" do
data = result["data"]["createMember"]
it 'returns the created Member' do
data = result['data']['createMember']

expect(data).to include(
{
"id" => be_present,
"firstName" => "Test"
'id' => be_present,
'firstName' => 'Test'
}
)
expect(Member.count).to be 1
end
end

describe "Member delete mutation" do
describe 'Member delete mutation' do
let!(:result) { KilnBackendSchema.execute(mutation).as_json }

context "when member exists" do
context 'when member exists' do
let(:member) { create(:member) }
let(:mutation) {
let(:mutation) do
<<~GQL
mutation {
deleteMember(id: "#{member.id}")
}
GQL
}
end

it "returns true" do
data = result["data"]["deleteMember"]
it 'returns true' do
data = result['data']['deleteMember']
expect(data).to be true
expect(Member.count).to be 0
end
end

context "when member does not exist" do
let(:mutation) {
context 'when member does not exist' do
let(:mutation) do
<<~GQL
mutation {
deleteMember(id: "999")
}
GQL
}
end

it "returns false" do
expect(result["data"]["deleteMember"]).to be false
it 'returns false' do
expect(result['data']['deleteMember']).to be false
end
end
end
Expand Down
Loading
Loading