Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). I'm using Python and the Graphene library. Here's an snippet of the current input object that doesn't allow for the ID to be used instead of the number / catalog:

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). Is there a way to achieve this, or more generally to support this functionality in GraphQL schemas?

like image 589
Alec Avatar asked Mar 21 '26 20:03

Alec


1 Answers

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? One that requires an ID and one that requires the record and catalog numbers? 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.

like image 56
kaqqao Avatar answered Mar 23 '26 10:03

kaqqao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!