Make one of two fields required in GraphQL schema?
Asked Answered
L

2

13

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
  }
}
Lazurite answered 27/1, 2018 at 13:13 Comment(1)
You can use an union type, however this question is not solved. My preference goes to the @oneOf directive and the related TS equivalentDoradorado
D
7

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.

Douville answered 28/1, 2018 at 4:49 Comment(0)
M
4

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!
}
Matteroffact answered 19/6, 2020 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.