Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'graphql', '1.10.10'
end
class BaseArgument < GraphQL::Schema::Argument
def initialize(*args, **kwargs, &block)
super(*args, prepare: method(:convert_id), **kwargs, &block)
end
def convert_id(value, ctx)
if ctx[:raise_in_prepare]
raise GraphQL::ExecutionError, 'invalid id'
end
value
end
end
class BaseField < GraphQL::Schema::Field
argument_class(BaseArgument)
end
class BaseObject < GraphQL::Schema::Object
field_class(BaseField)
end
class BaseInputObject < GraphQL::Schema::InputObject
argument_class(BaseArgument)
end
class MyID < GraphQL::Schema::Scalar
def self.coerce_input(value, ctx)
if ctx[:raise_in_coerce]
raise GraphQL::CoercionError, "Invalid id '#{value}'"
end
value
end
end
class ThingInput < BaseInputObject
argument :id, MyID, required: true
end
class Query < BaseObject
field :thing, MyID, null: true do
argument :id, MyID, required: true
end
def thing(id:)
id
end
field :thing_input, MyID, null: true do
argument :input, ThingInput, required: true
end
def thing_input(input:)
input[:id]
end
end
class Schema < GraphQL::Schema
query Query
end
puts "=> Scalar ID inline"
pp Schema.execute('{ thing(id: "1") }', context: {}).to_h
puts "=> Scalar ID inline (raise_in_prepare)"
pp Schema.execute('{ thing(id: "1") }', context: { raise_in_prepare: true }).to_h
puts "=> Scalar ID inline (raise_in_coerce)"
pp Schema.execute('{ thing(id: "1") }', context: { raise_in_coerce: true }).to_h
puts "= >Scalar ID inline (raise in both)"
pp Schema.execute('{ thing(id: "1") }', context: { raise_in_coerce: true, raise_in_prepare: true }).to_h
variables = { id: "1" }
puts "=> Scalar ID via variable"
pp Schema.execute('query($id: MyID!) { thing(id: $id) }', context: {}, variables: variables).to_h
puts "=> Scalar ID via variable (raise_in_prepare)"
pp Schema.execute('query($id: MyID!) { thing(id: $id) }', context: { raise_in_prepare: true }, variables: variables).to_h
puts "=> Scalar ID via variable (raise_in_coerce)"
pp Schema.execute('query($id: MyID!) { thing(id: $id) }', context: { raise_in_coerce: true }, variables: variables).to_h
puts "=> Scalar ID via variable (raise) in both"
pp Schema.execute('query($id: MyID!) { thing(id: $id) }', context: { raise_in_coerce: true, raise_in_prepare: true }, variables: variables).to_h
puts "=> Input object ID inline"
pp Schema.execute('{ thingInput(input: { id: "1" }) }', context: {}).to_h
puts "=> Input object ID inline (raise_in_prepare)"
pp Schema.execute('{ thingInput(input: { id: "1" }) }', context: { raise_in_prepare: true }).to_h
puts "=> Input object ID inline (raise_in_coerce)"
pp Schema.execute('{ thingInput(input: { id: "1" }) }', context: { raise_in_coerce: true }).to_h
puts "=> Input object ID inline (raise in both)"
pp Schema.execute('{ thingInput(input: { id: "1" }) }', context: { raise_in_coerce: true, raise_in_prepare: true }).to_h
variables = { input: { id: "1" } }
puts "=> Input object ID via variable"
pp Schema.execute('query($input: ThingInput!) { thingInput(input: $input) }', context: {}, variables: variables).to_h
puts "=> Input object ID via variable (raise_in_prepare)"
pp Schema.execute('query($input: ThingInput!) { thingInput(input: $input) }', context: { raise_in_prepare: true }, variables: variables).to_h
puts "=> Input object ID via variable (raise_in_coerce)"
pp Schema.execute('query($input: ThingInput!) { thingInput(input: $input) }', context: { raise_in_coerce: true }, variables: variables).to_h
puts "=> Input object ID via variable (raise in both)"
pp Schema.execute('query($input: ThingInput!) { thingInput(input: $input) }', context: { raise_in_coerce: true, raise_in_prepare: true }, variables: variables).to_h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment