Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails GraphQL: Parameter of resolver-methods - Meaning of the semicolon-suffix?

Tags:

ruby

What's the meaning of the semicolon after the id-parameter? Please see my comment within the snippet.

That isn't a Ruby-symbol. Isn't it? Otherwise the semicolon would be on the left-side. The ID is an integer. A bit confused currently ...

    field :vendor, VendorType, null: false,
      description: "A single car-vendor." do
        argument :id, ID, required: true
      end
    def vendor(id:) # Why is there a semicolon after "id"?
      Vendor.find(id)
    end
like image 336
cluster1 Avatar asked Dec 05 '25 08:12

cluster1


1 Answers

The syntax is defining a keyword argument that the caller has to explicitly state:

def v(id:)
  p id
end

v(1) # error

v(id: 1)
1
 => 1 

v(something: 1)
ArgumentError (missing keyword: id)

It has its uses, primarily for interaction in the console and ensuring the user knows what they are doing before they call the method, or ensuring new developers of the codebase understand how to call the method.

If the required argument is missing, Ruby throws a nice ArgumentError which is concise and reduces the cognitive overhead for future developers to engage with a codebase.

like image 191
benjessop Avatar answered Dec 07 '25 21:12

benjessop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!