πŸ‘€ See Sanity in action: Watch product demo now β†’

GraphQL Query - How to Access the Title Value for a String that has Predefined Options Specified

2 replies
Last updated: Jul 30, 2020
Hey all! I'm trying to figure out how to access the
title
value for a string that has predefined options specified. For example, if I had a
book
schema type with an author and a genre, where the genre is a string with a list of options:

export default {
  title: 'Book',
  name: 'book',
  type: 'document',
  fields: [
    {
      title: 'Author',
      name: 'author',
      type: 'string',
    }, {
      title: 'Genre',
      name: 'genre',
      type: 'string',
      options: {
        list: [
          { title: 'Non-Fiction', value: 'non_fiction' },
          { title: 'Sci-Fi', value: 'sci_fi' },
        ],
      },
    },
  ],
}
I'm executing this GraphQL query:


query {
  _id
  author
  genre
}
I want
genre
to come back as
Non-Fiction
or
Sci-Fi
, instead of
non_fiction
or
sci_fi
. How do I achieve that?
Jul 30, 2020, 2:58 AM
The list returns whatever you have in ``value``. If you change there ``non_fiction`` to ``Non-Fiction`` it should work as you expect :)
Jul 30, 2020, 6:06 AM
Doh -- that was an obvious one. Thanks!
Jul 30, 2020, 6:58 AM

Sanity.io – build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.