简体   繁体   English

GraphQL - 多个必填输入字段的“非此即彼”

[英]GraphQL - 'either / or' for multiple required input fields

I'm working on a Relay-compliant GraphQL schema and would like to include a mutation that takes an input object that requires either one argument (the ID of a record) or two other arguments (a record number and a catalog number for the record).我正在研究符合 Relay 的 GraphQL 模式,并希望包含一个带有输入对象的变更,该对象需要一个参数(记录的 ID)或两个其他参数(记录编号和记录的目录编号) )。 I'm using Python and the Graphene library.我正在使用 Python 和Graphene库。 Here's an snippet of the current input object that doesn't allow for the ID to be used instead of the number / catalog:这是当前输入对象的一个​​片段,它不允许使用 ID 代替编号/目录:

class CreateRecord(relay.ClientIDMutation):
    "The parameters of a newly created record."
    # Relay-compliant mutations need to specify an input class.
    class Input:
        "The available inputs for the CreateRecord mutation."
        part_id = graphene.Int(description='The ID of the record.')
        part_number = graphene.String(description='The record number.',
                                      required=True)
        catalog = graphene.String(description='The catalog of the record.',
                                  required=True)

I've looked into interfaces and unions in order to make this happen, but haven't had any luck so far.为了实现这一点,我研究了接口和联合,但到目前为止还没有任何运气。 Without a more flexible input object, I'll likely need to create another mutation (like CreateRecordUsingID ).如果没有更灵活的输入对象,我可能需要创建另一个突变(如CreateRecordUsingID )。 Is there a way to achieve this, or more generally to support this functionality in GraphQL schemas?有没有办法实现这一点,或者更普遍地支持 GraphQL 模式中的这个功能?

There's no way to express conditional non-nullness in the type system.没有办法在类型系统中表达条件非空。 So you'd have to validate this in the resolution logic yourself.所以你必须自己在解析逻辑中验证这一点。

But, why wouldn't you split this into 2 separate explicit operations instead?但是,为什么不将其拆分为 2 个单独的显式操作呢? One that requires an ID and one that requires the record and catalog numbers?一种需要 ID,一种需要记录和目录号? Yes, you'll have to name them differently, but explicitness is a good thing and you'll be working with and not against the type system.是的,您必须以不同的方式命名它们,但明确性是一件好事,您将使用而不是反对类型系统。

There's a good article series on mutation design, and this entry in particular is pertinent to your case.有一个关于突变设计的好文章系列, 这个条目特别适合您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM