Commit 087b47de authored by Stan Hu's avatar Stan Hu

Merge branch 'ld-graphql-time-type-coercion-error' into 'master'

GraphQL TimeType to throw GraphQL::CoercionErrors

See merge request gitlab-org/gitlab!38860
parents 3b79f3cb 71355af6
...@@ -7,6 +7,8 @@ module Types ...@@ -7,6 +7,8 @@ module Types
def self.coerce_input(value, ctx) def self.coerce_input(value, ctx)
Time.parse(value) Time.parse(value)
rescue ArgumentError, TypeError => e
raise GraphQL::CoercionError, e.message
end end
def self.coerce_result(value, ctx) def self.coerce_result(value, ctx)
......
...@@ -15,4 +15,14 @@ RSpec.describe GitlabSchema.types['Time'] do ...@@ -15,4 +15,14 @@ RSpec.describe GitlabSchema.types['Time'] do
it 'coerces an ISO-time into Time object' do it 'coerces an ISO-time into Time object' do
expect(described_class.coerce_isolated_input(iso)).to eq(time) expect(described_class.coerce_isolated_input(iso)).to eq(time)
end end
it 'rejects invalid input' do
expect { described_class.coerce_isolated_input('not valid') }
.to raise_error(GraphQL::CoercionError)
end
it 'rejects nil' do
expect { described_class.coerce_isolated_input(nil) }
.to raise_error(GraphQL::CoercionError)
end
end end
...@@ -49,13 +49,13 @@ RSpec.describe 'Setting Due Date of an issue' do ...@@ -49,13 +49,13 @@ RSpec.describe 'Setting Due Date of an issue' do
expect(mutation_response['issue']['dueDate']).to eq(2.days.since.to_date.to_s) expect(mutation_response['issue']['dueDate']).to eq(2.days.since.to_date.to_s)
end end
context 'when passing due date without a date value' do context 'when the due date value is not a valid time' do
let(:input) { { due_date: 'test' } } let(:input) { { due_date: 'test' } }
it 'returns internal server error' do it 'returns a coercion error' do
post_graphql_mutation(mutation, current_user: current_user) post_graphql_mutation(mutation, current_user: current_user)
expect(graphql_errors).to include(a_hash_including('message' => 'Internal server error')) expect(graphql_errors).to include(a_hash_including('message' => /provided invalid value for dueDate/))
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment