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

Add Custom scalar type #211

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion app/graphql/types/request_password_recovery_input.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Types
class RequestPasswordRecoveryInput < Types::BaseInputObject
argument :email, String, required: true
argument :email, SquishedString, required: true
end
end
2 changes: 1 addition & 1 deletion app/graphql/types/sign_in_input.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Types
class SignInInput < Types::BaseInputObject
argument :email, String, required: true
argument :email, SquishedString, required: true
argument :password, String, required: true
end
end
6 changes: 3 additions & 3 deletions app/graphql/types/sign_up_input.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Types
class SignUpInput < Types::BaseInputObject
argument :email, String, required: true
argument :email, SquishedString, required: true
argument :password, String, required: true

argument :first_name, String, required: false
argument :last_name, String, required: false
argument :first_name, SquishedString, required: false
argument :last_name, SquishedString, required: false

argument :avatar, Types::ImageUploaderType, required: false
end
Expand Down
14 changes: 14 additions & 0 deletions app/graphql/types/squished_string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Types
class SquishedString < BaseScalar
def self.coerce_input(input_value, _context)
squished = input_value.try(:squish)
raise GraphQL::CoercionError, "#{input_value.inspect} is not a valid string" unless squished.is_a? String

squished
end

def self.coerce_result(ruby_value, _context)
ruby_value.to_s
end
end
end
6 changes: 3 additions & 3 deletions app/graphql/types/update_user_input.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Types
class UpdateUserInput < Types::BaseInputObject
argument :email, String, required: false
argument :first_name, String, required: false
argument :last_name, String, required: false
argument :email, SquishedString, required: false
argument :first_name, SquishedString, required: false
argument :last_name, SquishedString, required: false
argument :current_password, String, required: false
argument :password, String, required: false
argument :avatar, Types::ImageUploaderType, required: false
Expand Down
27 changes: 27 additions & 0 deletions spec/graphql/types/squished_string_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "rails_helper"

describe Types::SquishedString do
describe "#coerce_input" do
subject(:method) { described_class.coerce_input(input_value, {}) }

context "with regular string" do
let(:input_value) { "String without extra whitespaces" }

it { is_expected.to eq("String without extra whitespaces") }
end

context "with overspaced string" do
let(:input_value) { " String with \t extra \n whitespaces " }

it { is_expected.to eq("String with extra whitespaces") }
end

context "with not string provided" do
let(:input_value) { 42 }

it "raises graphql error" do
expect { method }.to raise_error(::GraphQL::CoercionError, "42 is not a valid string")
end
end
end
end