3

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?

Alec
  • 1,399
  • 4
  • 15
  • 27
  • 1
    I have the same question about queries. For example, when querying for a user, an ID or username should be provided - but not both. I am not sure if this type of validation is supported out of the box. It seems like a common pattern. – jfizz Feb 02 '17 at 14:59
  • @jfizz See my answer, it applies to your case as well. – kaqqao Mar 07 '18 at 18:47

1 Answers1

1

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.

kaqqao
  • 12,984
  • 10
  • 64
  • 118