[英]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:我在
app/graphql/graph/enums
中定义了一个枚举item_status.rb
,它看起来像这样:
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:它在我的
schema.graphql
中定义如下:
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.我想从我的前端发送一个包含多个 ItemStatus 值的查询,例如在我的 queryVariables 中我会发送
status: ["GOOD", "NORMAL"]
我想接收所有具有这些状态之一的项目背部。 How do I modify my schema to accept an array of values as well as a single value?如何修改我的架构以接受值数组和单个值?
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.不过,我可能会遗漏一些关于您正在尝试做的事情。
I prefer you need to send the array of enum like [GOOD,NORMAL] for query variable我更喜欢你需要为查询变量发送像 [GOOD,NORMAL] 这样的枚举数组
Define your graphql field like this:像这样定义您的 graphql 字段:
field :Items, ReturnType do
argument :statuses, [Graph::Enums::ItemStatus], required: true
end
def Items(statuses:)
Item.where(status: statuses)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.