如何在GraphQL类型中引用特定的ENUM值?

我有以下架构:

enum PaymentTypeName {
  PAYMENT_CARD
  PAYMENT_CARD_TOKEN
}

interface Payment {
  id: ID!
  type: PaymentTypeName!
}

type PaymentCardPayment implements Payment {
  id: ID!
  type: PaymentTypeName!
  card: PaymentCard!
}

type PaymentCardTokenPayment implements Payment {
  id: ID!
  type: PaymentTypeName!
  card: PaymentCard!
}

当Payment是PaymentCardPayment或PaymentCardTokenPayment由type的值确定时,即它是PAYMENT_CARD或PAYMENT_CARD_TOKEN.

如何在界面中表示PaymentCardPayment / PaymentCardTokenPayment是否继承了PaymentTypeName的特定值?

我尝试了各种组合:

type PaymentCardPayment implements Payment {
  id: ID!
  type: PaymentTypeName.PAYMENT_CARD!
  card: PaymentCard!
}

和:

type PaymentCardPayment implements Payment {
  id: ID!
  type: PaymentTypeName[PAYMENT_CARD]!
  card: PaymentCard!
}

但所有这些都提示语法错误,我无法找到相关文档.

最佳答案 您试图在类型模式中声明字段值,这不是模式的含义.您应该只在模式中声明您的字段类型,在这种情况下,它只是键入:PaymentTypeName.你在第一个代码块中正确使用它.

您的PaymentCardPayment的类型解析器函数应该返回枚举的值,在您的情况下,PAYMENT_CARD.

您的PaymentCardTokenPayment的类型解析器函数应返回PAYMENT_CARD_TOKEN的值.

点赞