如何使用GraphQLEnumType中的名称作为GraphQL查询参数的默认值?
在模式中定义查询时,如何引用前面声明的GraphQLEnumType的值,将其用作参数的默认值 假设我定义了以下如何使用GraphQLEnumType中的名称作为GraphQL查询参数的默认值?,graphql,graphql-js,Graphql,Graphql Js,在模式中定义查询时,如何引用前面声明的GraphQLEnumType的值,将其用作参数的默认值 假设我定义了以下ObservationPeriodGraphQLEnumType: observationPeriodEnum = new GraphQLEnumType { name: "ObservationPeriod" description: "One of the performance metrics observation periods" values: Dail
ObservationPeriod
GraphQLEnumType:
observationPeriodEnum = new GraphQLEnumType {
name: "ObservationPeriod"
description: "One of the performance metrics observation periods"
values:
Daily:
value: '1D'
description: "Daily"
[…]
}
并将其用作查询参数的类型period
:
queryRootType = new GraphQLObjectType {
name: "QueryRoot"
description: "Query entry points to the DWH."
fields:
performance:
type: performanceType
description: "Given a portfolio EID, an observation period (defaults to YTD)
and as-of date, as well as the source performance engine,
return the matching performance metrics."
args:
period:
type: observationPeriodEnum
defaultValue: observationPeriodEnum.Daily ← how to achieve this?
[…]
}
目前我使用实际的'1D'
字符串值作为默认值;这项工作:
period:
type: observationPeriodEnum
defaultValue: '1D'
但是有没有办法用每日
符号名来代替呢?我找不到在模式本身中使用名称的方法。有什么是我忽略的吗
我这样问是因为我希望枚举类型也可以作为一组常量,并且能够在模式定义中这样使用它们:
period:
type: observationPeriodEnum
defaultValue: observationPeriodEnum.Daily
period:
type: observationPeriodEnum
defaultValue: observationPeriodEnum.Daily
天真的解决方法:
##
# Given a GraphQLEnumType instance, this macro function injects the names
# of its enum values as keys the instance itself and returns the modified
# GraphQLEnumType instance.
#
modifiedWithNameKeys = (enumType) ->
for ev in enumType.getValues()
unless enumType[ ev.name]?
enumType[ ev.name] = ev.value
else
console.warn "SCHEMA> Enum name #{ev.name} conflicts with key of same
name on GraphQLEnumType object; it won't be injected for value lookup"
enumType
observationPeriodEnum = modifiedWithNameKeys new GraphQLEnumType {
name: "description: "Daily""
values:
[…]
允许在模式定义中根据需要使用它:
period:
type: observationPeriodEnum
defaultValue: observationPeriodEnum.Daily
period:
type: observationPeriodEnum
defaultValue: observationPeriodEnum.Daily
当然,只要枚举名不干扰GraphQLEnumType现有的方法和变量名,这个修饰符就实现了它的承诺(目前有:
名称
,说明
,值
,枚举配置
,值查找
,名称查找
,获取值
,序列化
,解析值
,获取值查找
,,获取名称查找
,以及toString
参见GraphQLEnumType
中第687行附近的类我刚刚遇到了这个。我的枚举:
const contributorArgs = Object.assign(
{},
connectionArgs, {
sort: {
type: new GraphQLEnumType({
name: 'ContributorSort',
values: {
top: { value: 0 },
},
})
},
}
);
在我的询问中,我做了:
... on Topic {
id
contributors(first: 10, sort: 'top') {
...
}
}
事实证明,您只是没有引用该值(仔细考虑后,这是有意义的;它是枚举类型中的一个值,而不是实际值:
... on Topic {
id
contributors(first: 10, sort: top) {
...
}
}
它是通过模式定义语言将枚举值声明为默认输入,但看起来您只是在使用JS库API。您可以通过将a作为工作示例并从JS代码生成的内容来获得解决方案
抱歉,这不是一个解决方案,但希望能有所帮助!我发现在枚举类型中添加了一个方法.getValue()
,该方法返回name
和value
。在您的情况下,此调用:
observationPeriodEnum.getValue('Daily');
将返回:
{
name: 'Daily',
value: '1D'
}
您好@Matthew Herbst,感谢您了解这一点并分享您的经验–这与在GQL查询中声明枚举文字值有关(即在«运行时»).然而,我的问题是关于模式声明的:如何在声明模式本身时使用符号而不是复制文字值来访问其中一个枚举值?–在«模式设计时»,而不是«查询时/运行时»。