Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
Last active September 24, 2024 14:11
Show Gist options
  • Save rmosolgo/ed620e6c2c4dbfd892eedfa046ff79b5 to your computer and use it in GitHub Desktop.
Save rmosolgo/ed620e6c2c4dbfd892eedfa046ff79b5 to your computer and use it in GitHub Desktop.
Required input argument example
require "bundler/inline"
gemfile do
gem "graphql", "2.3.16"
gem "graphql-enterprise", "1.5.2", source: "https://gems.graphql.pro"
end
class MySchema < GraphQL::Schema
class MakeInputsRequired < GraphQL::Enterprise::Changeset
release "2024-01-01"
end
class BaseArgument < GraphQL::Schema::Argument
include GraphQL::Enterprise::Changeset::ArgumentIntegration
def initialize(*args, **kwargs)
kwargs = { required: false, camelize: false, **kwargs }
super(*args, **kwargs)
end
end
class BaseMutation < GraphQL::Schema::Mutation
argument_class BaseArgument
include GraphQL::Schema::HasSingleInputArgument
class << self
def required_input
@required_input = true
end
def dummy
dummy_resolver = super
if @required_input
# In this case, remove the automatically generated `input` argument
# and replace it with two new ones:
# - The first has `required: false` and is removed in the changeset
# - The second has `reuqired: true` and is added in the changeset
dummy_resolver.own_arguments.delete("input")
dummy_resolver.argument(:input, input_type, required: false, removed_in: MakeInputsRequired, description: "Parameters for #{graphql_name}")
dummy_resolver.argument(:input, input_type, required: true, added_in: MakeInputsRequired, description: "Parameters for #{graphql_name}") # rubocop:disable GraphQL/DefaultRequiredTrue
end
dummy_resolver
end
end
end
class DoSomething < BaseMutation
required_input
argument :input_value, String
field :output_value, String, camelize: false
def resolve(input_value: "Default")
{
output_value: input_value.upcase
}
end
end
class Mutation < GraphQL::Schema::Object
field :do_something, mutation: DoSomething, camelize: false
end
mutation(Mutation)
use GraphQL::Enterprise::Changeset::Release, changesets: [MakeInputsRequired]
end
pp MySchema.execute("mutation { do_something { output_value } }").to_h
# {"data"=>{"do_something"=>{"output_value"=>"DEFAULT"}}}
pp MySchema.execute("mutation { do_something { output_value } }", context: { changeset_version: "2024-01-01" }).to_h
# {"errors"=>
# [{"message"=>"Field 'do_something' is missing required arguments: input",
# "locations"=>[{"line"=>1, "column"=>12}],
# "path"=>["mutation", "do_something"],
# "extensions"=>{"code"=>"missingRequiredArguments", "className"=>"Field", "name"=>"do_something", "arguments"=>"input"}}]}
puts MySchema.to_definition
# ...
# type Mutation {
# do_something(
# """
# Parameters for DoSomething
# """
# input: DoSomethingInput
# ): DoSomethingPayload
# }
puts MySchema.to_definition(context: { changeset_version: "2024-01-01" })
# ...
# type Mutation {
# do_something(
# """
# Parameters for DoSomething
# """
# input: DoSomethingInput!
# ): DoSomethingPayload
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment