Alex Alex - 7 months ago 120
Javascript Question

How to set a Relay variable as an enum value?

Example fragment:

fragments: {
viewer: () => Relay.QL`
fragment on Viewer {
people(first: $limit orderBy: $orderBy) {
count
edges {
node {
id,
${PersonListItem.getFragment('person')},
},
},
}
}
`,
},


The
orderBy
argument accepts the following enum values:
firstNameASC
/
firstNameDESC
/
lastNameASC
/
lastNameDESC
.

When doing
this.setVariables({orderBy: 'firstName'})
the
orderBy
variable is passed as a string to the GraphQL server.

How do I pass any of these variables into Relay's setVariables without them being sent as strings?

Answer

You can use an enum variable as a string.

Example

Query (EventsConnectionOrder is an enum)

query($orderBy: [EventsConnectionOrder]){
  viewer {
    events(first:1 orderBy: $orderBy) {
      edges {
        node {
          id
        }
      }
    }
  }
}

Variables

{
  "orderBy": "dateASC"
}