Evanss
Evanss

Reputation: 23173

Make one of two fields required in GraphQL schema?

Im using Graphcool but this may be a general GraphQL question. Is there a way to make one of two fields required?

For instance say I have a Post type. Posts must be attached to either a Group or to an Event. Can this be specified in the schema?

type Post {
  body: String!
  author: User!
  event: Event // This or group is required
  group: Group // This or event is required
}

My actual requirements are a bit more complicated. Posts can either be attached to an event, or must be attached to a group and a location.

type Post {
  body: String!
  author: User!
  event: Event // Either this is required, 
  group: Group // Or both Group AND Location are required 
  location: Location 
}

So this is valid:

mutation {
  createPost(
   body: "Here is a comment",
   authorId: "<UserID>",
   eventId: "<EventID>"
  ){
    id
  }
}

As is this:

mutation {
  createPost(
   body: "Here is a comment",
   authorId: "<UserID>",
   groupID: "<GroupID>",
   locationID: "<LocationID>"
  ){
    id
  }
}

But this is not:

As is this:

mutation {
  createPost(
   body: "Here is a comment",
   authorId: "<UserID>",
   groupID: "<GroupID>",
  ){
    id
  }
}

Upvotes: 13

Views: 9202

Answers (2)

bigsolom
bigsolom

Reputation: 2349

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!
}

Upvotes: 4

Daniel Rearden
Daniel Rearden

Reputation: 84857

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.

Upvotes: 7

Related Questions