Skip to content

Getting the parent argument name in InputObject#prepare methods #5266

Open
@eapache-opslevel

Description

@eapache-opslevel

We are implementing prepare methods on several input objects. They do some validation, and may raise/return various errors in those cases. We would like to have those prepare methods automatically generate error messages based on the path to the input object.

For example, given

class MyInputType < GraphQL::Schema::InputObject
  def prepare
    raise GraphQL::ExecutionError, "some error message"
  end
end

# elsewhere
argument :input, MyInputType
argument :something_else, MyInputType

We would like the error message to contain input or something_else as appropriate, but there does not appear to be a way to access that information at the moment.

context.current_path exists, but only includes field names, not the (potentially many) argument names needed to get to the currently-preparing argument.

Describe the solution you'd like

Maybe context.current_argument_path is possible? Or maybe there are reasons this is really tricky?

In general we only need the last element, i.e. the name of the specific argument that holds the current input type, so some sort of parent access would also work. TBH I thought that already existed, but I couldn't find the right incantation.

Describe alternatives you've considered

We currently define our prepare methods as class methods returning procs, and then do e.g.

argument :something_else, MyInputType, prepare: MyInputType.prepare(:something_else)

Which works but is of course a bunch of duplicative boilerplate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions