如何使用GraphQLEnumType中的名称作为GraphQL查询参数的defaultValue?

时间:2016-04-16 16:19:51

标签: graphql graphql-js

在模式中定义查询时,如何引用先前声明的GraphQLEnumType的值,将其用作参数的默认值?

假设我已经定义了以下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'

但有没有办法可以使用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

当然,只要枚举名称不干扰GraphQLEnumType现有方法和变量名称(当前为namedescription_values,此修饰符就会满足其承诺。 },_enumConfig_valueLookup_nameLookupgetValuesserializeparseValue_getValueLookup_getNameLookuptoString - 请参阅https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L687中第687行的GraphQLEnumType类定义

3 个答案:

答案 0 :(得分:1)

我刚碰到这个。我的枚举:

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) {
    ...
  }
}

答案 1 :(得分:0)

通过模式定义语言将枚举值声明为默认输入是possible,但看起来您只使用JS库API。您可以通过look at the ASTs获取工作示例来获得解决方案,并从JS代码生成的comparing that with the AST中获取解决方案。

抱歉不是解决方案,但希望有所帮助!

答案 2 :(得分:0)

我发现a pull request向枚举类型添加了一种方法.getValue(),该方法返回了namevalue。在您的情况下,此呼叫:

observationPeriodEnum.getValue('Daily');

将返回:

{
  name: 'Daily',
  value: '1D'
}