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

ola*_*nge 6 graphql graphql-js

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

假设我已经定义了以下ObservationPeriodGraphQLEnumType:

observationPeriodEnum = new GraphQLEnumType {
  name: "ObservationPeriod"
  description: "One of the performance metrics observation periods"
  values:
    Daily:
      value: '1D'
      description: "Daily"
    […]
}
Run Code Online (Sandbox Code Playgroud)

并将其用作查询参数的类型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?
      […]
}
Run Code Online (Sandbox Code Playgroud)

目前我正在使用实际的'1D'字符串值作为默认值; 这工作:

        period:
          type: observationPeriodEnum 
          defaultValue: '1D'
Run Code Online (Sandbox Code Playgroud)

但有没有办法可以使用Daily符号名称呢?我找不到在架构中使用名称的方法.有没有我忽略的东西?

我问,因为我期望枚举类型也表现为一组常量,并且能够在架构定义中使用它们:

        period:
          type: observationPeriodEnum 
          defaultValue: observationPeriodEnum.Daily
Run Code Online (Sandbox Code Playgroud)

天真的解决方法:

##
# 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:
    […]
Run Code Online (Sandbox Code Playgroud)

允许在模式定义中根据需要使用它:

        period:
          type: observationPeriodEnum 
          defaultValue: observationPeriodEnum.Daily
Run Code Online (Sandbox Code Playgroud)

当然,这种修改fullfils承诺,只是只要枚举名称不GraphQLEnumType现有的方法和变量名(这是目前干扰:name,description,_values,_enumConfig,_valueLookup,_nameLookup,getValues,serialize,parseValue,_getValueLookup,_getNameLookuptoString-见的定义GraphQLEnumType类围绕线687在https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L687)

Mat*_*bst 1

我刚刚遇到了这个。我的枚举:

const contributorArgs = Object.assign(
  {},
  connectionArgs, {
    sort: {
      type: new GraphQLEnumType({
        name: 'ContributorSort',
        values: {
          top: { value: 0 },
        },
      })
    },
  }
);
Run Code Online (Sandbox Code Playgroud)

在我的查询中,我正在做:

... on Topic {
  id
  contributors(first: 10, sort: 'top') {
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

事实证明,您只是没有引用该值(经过考虑后这是有道理的;它是枚举类型中的值,而不是实际值:

... on Topic {
  id
  contributors(first: 10, sort: top) {
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)