Make one of two fields required in GraphQL schema?

There's no way you can define your schema to define groups of inputs as required -- each input is individually either nullable (optional) or non-null (required).

The only way to handle this type of scenario is within the resolver for that specific query or mutation. For example:

(obj, {eventId, groupID, locationID}) => {
  if (
    (eventID && !groupID && !locationID) ||
    (groupID && locationID && !eventID)
  ) {
    // resolve normally
  }
  throw new Error('Invalid inputs')
}

It looks like in order to do that with Graphcool, you would have to utilize a custom resolver. See the documentation for more details.


One way to represent this in the schema is to use unions , in your case it could be something like this:

type LocatedGroup {
  group: Group!
  location: Location!
}

union Attachable = Event | LocatedGroup

type Post {
  body: String!
  author: User!
  attachable: Attachable!
}