给定一组GraphQL变量类型,是否可以使用客户端架构为该集中的每种类型创建所有有效值的映射 [英] Given a set of GraphQL variable types, is it possible to use the client schema to create a map of all valid values for each type in the set
问题描述
标题几乎说明了一切:我正在构建一个React/Relay应用程序,该应用程序将允许用户在运行时动态创建图表,以显示他们在指定时间范围内的各种收入流.该图表的一个功能是用户指定每个收入流的采样间隔的能力(例如 YEAR
, QUARTER
, MONTH
, WEEK
等)作为每个流的参数.
Title mostly says it all: I'm building a react / relay application which will allow the user to dynamically create charts at runtime displaying their various income streams over a specified time range. One feature of this chart is the ability of the user to specify the sampling interval of each income stream (e.g. YEAR
, QUARTER
, MONTH
, WEEK
, etc.) as a parameter of each stream.
这些值在架构中定义为 GraphQLInputObjectType
实例,如下所示:
These values are defined in the schema as a GraphQLInputObjectType
instance as follows:
enum timeSeriesIntervalEnum {
YEAR
QUARTER
MONTH
WEEK
}
在客户端,我有 react-relay
片段,其定义如下:
On the client-side, I have react-relay
fragments defined of the following form:
BalanceSheet上的片段{收入 {#一些收入流税后 {值(时间间隔:$ samplingInterval)日期(间隔:$ samplingInterval)}}}
此变量值将作为单独菜单中下拉菜单的一部分进行填充,其中下拉菜单中的每个值都应对应于有效的 timeSeriesIntervalEnum
值.
This variable value will be populated as part of dropdown menu in a separate component where each value in the dropdown should correspond to a valid timeSeriesIntervalEnum
value.
虽然当然可以简单地对这些值进行硬编码,但底层API仍在经常更改,并且我想减少耦合,而是通过为给定下拉列表指定变量类型来动态填充这些字段(例如 timeSeriesIntervalEnum
),然后使用graphql客户端架构解析值并填充config json文件(运行前)或在运行时动态分配值.
Although it would certainly be possible to simply hardcode these values in, the underlying API is still being changed quite often and I would like to reduce coupling and instead populate these fields dynamically by specifying the variable type for a given dropdown (e.g. timeSeriesIntervalEnum
) and then use the graphql client schema to parse the values and populate either a config json file (pre-runtime) or assign the values dynamically at runtime.
注意:我已经在启动前做了一些查询字符串和片段转译,因此,如果需要的话,我不反对在此过程中创建json配置文件. >
推荐答案
可以使用内省查询.
在您的情况下,这是一种可能的解决方案:
In your case this is one possible solution:
{
__type(name: "timeSeriesIntervalEnum") {
name
enumValues {
name
}
}
}
响应包含所有可能类型的列表:
The response contains a list of all possible types:
{
"data": {
"__type": {
"name": "timeSeriesIntervalEnum",
"enumValues": [
{
"name": "YEAR"
},
{
"name": "QUARTER"
},
{
"name": "MONTH"
},
{
"name": "WEEK"
}
]
}
}
}
这篇关于给定一组GraphQL变量类型,是否可以使用客户端架构为该集中的每种类型创建所有有效值的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!