Skip to content

Requiring one of 2 Arguments to be required #2267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RobsonKing opened this issue May 2, 2019 · 4 comments
Closed

Requiring one of 2 Arguments to be required #2267

RobsonKing opened this issue May 2, 2019 · 4 comments

Comments

@RobsonKing
Copy link

I have a field that I want to filter in one of two ways.

    field :car, Types::CarType, null: false do
      argument :id, ID, required: true
      argument :external_id, ID, required: false
    end

Is there anyway to indicate that one of the two values must be required, but not both?

I tried overriding GraphQL::Schema::Argument, but it doesn't seem like 'context' is available where 'required' is used.

@jturkel
Copy link
Contributor

jturkel commented May 3, 2019

This sounds like a good use case input unions in GraphQL to support schema level enforcement of this contract. See graphql/graphql-spec#395 for more on that.

In the meantime the best you can do is a runtime validation check:

field :car, Types::CarType, null: false do
  argument :id, ID, required: false
  argument :external_id, ID, required: false
end

def car(id: :unspecified, external_id: :unspecified)
  if (id == :unspecified && external_id == :unspecified) || (id != :unspecified && external_id != :unspecified)
    raise GraphQL::ExecutionError.new('Must specify exactly one of id or externalId')
  end

  # ...
end

@RobsonKing
Copy link
Author

ok, that is what I fell back to as well.

@rmosolgo
Copy link
Owner

Yep, best bet now is runtime, as shown above.

@blairio
Copy link

blairio commented Mar 20, 2025

@RobsonKing oneOf is now a thing: #4184

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants