espeon
espeon

Reputation: 57

How to set up an enum to allow multiple enums in a query? GraphQL Ruby

I have an enum, item_status.rb defined in app/graphql/graph/enums that looks something like this:

Graph::Enums::ItemStatus = GraphQL::EnumType.define do
 name "ItemStatus"
 description "lorem ipsum"
 
 value "GOOD", "item is good", value: "good"
 value "NORMAL", "item is normal", value: "normal"
 value "BAD", "item is bad", value: "bad"

end

It's defined in my schema.graphql like this:

enum ItemStatus {
 GOOD
 NORMAL
 BAD
}

I want to send a query from my front-end that contains multiple values for ItemStatus, for example in my queryVariables I'd send status: ["GOOD", "NORMAL"] and I want to receive all items with either of those statuses back. How do I modify my schema to accept an array of values as well as a single value?

Upvotes: 2

Views: 1215

Answers (2)

Syamlal
Syamlal

Reputation: 910

I prefer you need to send the array of enum like [GOOD,NORMAL] for query variable

Define your graphql field like this:

field :Items, ReturnType do
  argument :statuses, [Graph::Enums::ItemStatus], required: true
end

def Items(statuses:)
  Item.where(status: statuses)
end

Upvotes: 1

dtakeshta
dtakeshta

Reputation: 214

Wouldn't the argument in your query just be an array of strings?

field :your_field, YourType do
  argument :status, [String], required: true
end
def your_field(status:)
  Mymodel.where(status: status)
end

I might be missing something about what you are trying to do though.

Upvotes: 1

Related Questions