Skip to content

Instantly share code, notes, and snippets.

@jturkel
Created March 20, 2019 15:14
Show Gist options
  • Save jturkel/d95364ffd9d2a56827168bb155dfdad1 to your computer and use it in GitHub Desktop.
Save jturkel/d95364ffd9d2a56827168bb155dfdad1 to your computer and use it in GitHub Desktop.
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'graphql', '1.9.3'
gem 'json_spec'
gem 'rspec'
end
require 'rspec/autorun'
# Schema
class TestInputObject < GraphQL::Schema::InputObject
argument :number, Integer, required: true, prepare: ->(value, context) do
if value < 1
raise GraphQL::ExecutionError.new("Argument 'number' must be greater than or equal to 1: #{value}")
else
value
end
end
end
class TestQueryType < GraphQL::Schema::Object
field :echo_positive_number, Integer, null: true do
argument :number, Integer, required: true, prepare: ->(value, context) do
if value < 1
raise GraphQL::ExecutionError.new("Argument 'number' must be greater than or equal to 1: #{value}")
else
value
end
end
end
def echo_positive_number(number:)
number
end
field :echo_test_input, Integer, null: true do
argument :input, TestInputObject, required: true
end
def echo_test_input(input:)
input.number
end
end
class TestSchema < GraphQL::Schema
if ENV['USE_NEW_RUNTIME'] == 'true'
use GraphQL::Execution::Interpreter
use GraphQL::Analysis::AST
end
query TestQueryType
end
describe 'prepare exception handling' do
it "works for literal field arguments" do
query = <<~GRAPHQL
query {
valid: echoPositiveNumber(number: 1)
invalid: echoPositiveNumber(number: 0)
}
GRAPHQL
expected = {
data: {
valid: 1,
invalid: nil
},
errors: [
{
message: "Argument 'number' must be greater than or equal to 1: 0",
locations: [
{
line: 3,
column: 3
}
],
path: [
'invalid'
]
}
]
}
result = TestSchema.execute(query)
expect(result.to_json).to be_json_eql(expected.to_json)
end
it "works for literal input object arguments" do
query = <<~GRAPHQL
query {
valid: echoTestInput(input: { number: 1 })
invalid: echoTestInput(input: { number: 0 })
}
GRAPHQL
expected = {
data: {
valid: 1,
invalid: nil
},
errors: [
{
message: "Argument 'number' must be greater than or equal to 1: 0",
locations: [
{
line: 3,
column: 3
}
],
path: [
'invalid'
]
}
]
}
result = TestSchema.execute(query)
expect(result.to_json).to be_json_eql(expected.to_json)
end
it "works for variable input object arguments" do
query = <<~GRAPHQL
query($validTestInput: TestInputObject!, $invalidTestInput: TestInputObject!) {
valid: echoTestInput(input: $validTestInput)
invalid: echoTestInput(input: $invalidTestInput)
}
GRAPHQL
expected = {
errors: [
{
message: "Argument 'number' must be greater than or equal to 1: 0"
}
]
}
# This provides more information on the source of the error and would make the error consistent with custom scalar coercion errors
better_expected = {
errors: [
{
message: "Variable invalidTestInput of type TestInputObject! was provided invalid value",
locations: [
{
line: 1,
column: 42
}
],
extensions: {
value: {
number: 0
},
problems: [
{
path: [],
explanation: "Argument 'number' must be greater than or equal to 1: 0"
}
]
}
}
]
}
result = TestSchema.execute(query, variables: { validTestInput: { number: 1 }, invalidTestInput: { number: 0 }})
expect(result.to_json).to be_json_eql(expected.to_json)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment